1. 程式人生 > >maven 自動編譯版本號 buildnumber-maven-plugin 1.4

maven 自動編譯版本號 buildnumber-maven-plugin 1.4

    最近新專案要需要更新發布版本號,用於標識釋出的版本,網上找了下外掛:buildnumber-maven-plugin 1.4 。

記錄下自己使用成功過程中遇到的各種問題,以供需要的朋友參考:

    外掛官方網站:http://www.mojohaus.org/buildnumber-maven-plugin/usage.html (官網內附詳細說明)

    一、在專案pom.xml中新增相關配置

1).<plugins>...</plugins>中新增buildnumber-maven-plugin外掛。

		      <!-- 自動更新版本號   -->
		      <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>
2).<build>...</build>中新增上面item:timestamp指定輸出的配置檔案的路徑:
我把timestamp輸出的版本號(時間戳格式的)放在 monitor.properties裡。
  <directory>...</directory>裡的內容為配置檔案的目錄路徑:src/main/resources
<resources>
   <resource>
             <directory>src/main/resources</directory>
               <filtering>true</filtering>
           </resource>
      	 </resources>
   3).timestamp輸出屬性定義:

      與.properties檔案中的相對應。

  <properties>
      <project.build.timestamp>${timestamp}</project.build.timestamp>
  </properties>
二、jave程式碼中讀取monitor.properties檔案中的project.build.timestamp屬性,將時間戳格式轉化成想要的日期格式,儲存到庫表。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

        遇到的問題:

1. 開始用的是<goal>create</goal>,但是編譯會報錯,替換成<goal>create-timestamp</goal>即可。

[ERROR] Failed to execute goal org.codehaus.mojo:buildnumber-maven-plugin:1.0:create (default) on project projectname: Execution default of goal org.codehaus.mojo:buildnumber-maven-plugin:1.0:create failed: The scm url cannot be null. -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:buildnumber-maven-plugin:1.0:create (default) on project projectname: Execution default of goal org.codehaus.mojo:buildnumber-maven-plugin:1.0:create failed: The scm url cannot be null.
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:225)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
    at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319)
    at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
    at org.apache.maven.cli.MavenCli.execute(MavenCli.java:534)
    at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
    at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)

2. eclipse裡excution報錯:

解決辦法:

參考http://blog.csdn.net/huweijun_2012/article/details/51073782

3. maven clean install 編譯工程報錯:

[ERROR] Failed to execute goal org.codehaus.mojo:buildnumber-maven-plugin:1.4:cr
eate-timestamp (default) on project gdws-svc: Execution default of goal org.code
haus.mojo:buildnumber-maven-plugin:1.4:create-timestamp failed: A required class
 was missing while executing org.codehaus.mojo:buildnumber-maven-plugin:1.4:crea
te-timestamp: org/apache/commons/lang/StringUtils
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.codehaus.mojo:buildnumber-maven-plugin:1.4
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/D:/IDEStrain/maven/repository/org/codehaus/mojo/buildnum
ber-maven-plugin/1.4/buildnumber-maven-plugin-1.4.jar
[ERROR] urls[1] = file:/D:/IDEStrain/maven/repository/backport-util-concurrent/b
ackport-util-concurrent/3.1/backport-util-concurrent-3.1.jar
[ERROR] urls[2] = file:/D:/IDEStrain/maven/repository/org/codehaus/plexus/plexus
-interpolation/1.11/plexus-interpolation-1.11.jar
[ERROR] urls[3] = file:/D:/IDEStrain/maven/repository/org/slf4j/slf4j-jdk14/1.5.
6/slf4j-jdk14-1.5.6.jar
[ERROR] urls[4] = file:/D:/IDEStrain/maven/repository/org/slf4j/slf4j-api/1.5.6/
slf4j-api-1.5.6.jar
[ERROR] urls[5] = file:/D:/IDEStrain/maven/repository/org/slf4j/jcl-over-slf4j/1
.5.6/jcl-over-slf4j-1.5.6.jar
[ERROR] urls[6] = file:/D:/IDEStrain/maven/repository/org/apache/maven/reporting
/maven-reporting-api/2.2.1/maven-reporting-api-2.2.1.jar
[ERROR] urls[7] = file:/D:/IDEStrain/maven/repository/org/apache/maven/doxia/dox
ia-sink-api/1.1/doxia-sink-api-1.1.jar
[ERROR] urls[8] = file:/D:/IDEStrain/maven/repository/org/apache/maven/doxia/dox
ia-logging-api/1.1/doxia-logging-api-1.1.jar
解決:

執行buildnumber-maven-plugin外掛時找不到commons-lang的StringUtils類。通過檢視 maven本地倉庫buildnumber-maven-plugin-1.4.pom檔案,發現沒有配置commons-lang包的依賴,新增配置如下依賴:

<dependency>
<groupId>commons-lang</groupId>
       <artifactId>commons-lang</artifactId>
       <version>2.3</version> </dependency>
重新執行 clean install 通過!