2014-02-19 21 views
2

我看到這個問題被多次詢問,但是沒有令人滿意的答案:讓我們假設你有maven項目生產一些jar(java桌面應用程序)。 如何在pom.xml中定義版本號,在適當的時候(例如每個版本)自動遞增(甚至手動,無關緊要),但可以將該版本加載到應用程序中? 目標是向用戶顯示他當前使用的應用程序的版本。自動增加pom.xml中的版本號並將其顯示在應用程序中

回答

3

有特別四個選項,你可以去:

  1. 使用其默認情況下Maven的創建pom.properties文件。
  2. 使用由MANIFST.MF文件提供的信息。有幾種方法可以獲取這些信息。
  3. 創建一個在構建過程中被過濾並將由應用程序讀取的屬性。
  4. 使用包含適當信息的生成類。

第一個選項可以通過Java class like this處理:

second option is to use the MANIFEST.MF file

public class TheVersionClass { 
    public TheVersionClass() { 
     System.out.println(" Implementation Title:" + this.getClass().getPackage().getImplementationTitle()); 
     System.out.println(" Implementation Vendor:" + this.getClass().getPackage().getImplementationVendor()); 
     System.out.println("Implementation Version:" + this.getClass().getPackage().getImplementationVersion()); 
     System.out.println(" Specification Tile:" + this.getClass().getPackage().getSpecificationTitle()); 
     System.out.println(" Specification Vendor:" + this.getClass().getPackage().getSpecificationVendor()); 
     System.out.println(" Specification Version:" + this.getClass().getPackage().getSpecificationVersion()); 
    } 
} 

不幸的是,這些信息通常不被放入MANIFEST.MF文件所以你必須改變你的配置。

第三個選項是創建一個文件,在構建過程中將其作爲資源進行過濾,第四個選項是使用templating-maven-plugin創建適當的類。以上所有內容都可以查看github project

當然你可以使用buildnumber-maven-plugin來將你的版本控制系統的信息添加到你的MANIFEST.MF文件中,通過使用下面的代碼片段來增強你的任何示例你的模塊或好得多到公司的pom文件,該文件添加到爲每次構建執行的:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>buildnumber-maven-plugin</artifactId> 
    <version>1.2</version> 
    <configuration> 
     <revisionOnScmFailure>UNKNOWN</revisionOnScmFailure> 
     <getRevisionOnlyOnce>true</getRevisionOnlyOnce> 
     <providerImplementations> 
     <svn>javasvn</svn> 
     </providerImplementations> 
    </configuration> 
    <executions> 
     <execution> 
     <goals> 
      <goal>create</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 

,如果您有隻是一個新的版本,我不會更改版本。將Jenkins的buildnumber或你使用的任何CI解決方案添加到MANIFEST.MF文件中可能是有用的,但是我會使用Maven的版本,在發佈版本的情況下,它將從1.0-SNAPSHOT更改爲1.0

+1

謝謝你的答案 - 最後,我結束了過濾的屬性文件和autoincrement-versions-maven-plugin(buildnumber-maven-plugin也可以)。 MANIFEST.MF根本不適用於我 - 我使用的是OpenJDK,出於某種原因,它拒絕加載ImplementationVersion和類似的標籤(它們始終爲空)。我沒有時間進一步調試。 – gurbi

相關問題