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>