Java Web 项目自动生成编译版本号

maven项目打包发布的时候,使用插件实现自动生成一个版本号,用于标识发布的版本。

插件网站参考:http://www.mojohaus.org/buildnumber-maven-plugin/usage.html

一、在项目pom.xml文件中的project节点内添加buildnumber-maven-plugin插件

<build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.4</version>
                <configuration>
                    <format>{0,date,yyyy-MM-dd HH:mm:ss}</format>
                    <items>
                        <item>timestamp</item>
                    </items>
                </configuration>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create-timestamp</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

二、在pom.xml文件properties节点的添加属性

  ${timestamp},与插件configuration中的item名称一致

 <project.build.timestamp>${timestamp}</project.build.timestamp>

 

三、在项目的src/main/resources资源目录*.properties文件添加属性

  在步骤“一”之中的resources实现编译时给src/main/resources的*.properties 文件属性赋值。当编译完成后,打开war包会发现${project.build.timestamp}会被赋值。

project.build.timestamp=${project.build.timestamp}

   如果是时间戳模式,给properties 文件中的赋值是时间戳,在读取值之后再讲时间戳转换为日期。

四、在项目中读取*.properties文件中的值,将后台格式化之后的版本号写到Jsp页面

<!--PROJECT VERSION : 5.1.1 build-1709140938 -->
<script type="text/javascript">
    var $_project_version="5.1.1 build-1709140938";
</script>
<script type="text/javascript" src="/js/plugins/jquery/jquery.js?v=1709140938"></script>

 

posted @ 2017-09-14 10:13  taony  阅读(8070)  评论(0编辑  收藏  举报