2012-10-12 15 views
2

我有一個多模塊版本,其中包含模塊,可以爲Java 5或Java 6的目標。我想允許模塊選擇加入Java 6,並將默認值爲5。不同的插件配置爲每個子模塊

設置Java 5作爲目標我需要進行以下配置:

  • maven-compiler-plugin:源和目標設定爲1.5
  • maven-bundle-plugin:配置捆綁-RuntimeExecutionEnvironment到J2SE-1.5

設置Java 6作爲目標我需要進行以下配置:

  • maven-compiler-plugin:源和目標設定爲1.6
  • maven-bundle-plugin:配置捆綁-RuntimeExecutionEnvironment到JavaSE的-1.6

我認爲有兩個屬性:java.compiler.sourceosgi.bree它可以由每個模塊定義,但是這留下了錯誤的地方。

如何用單個開關覆蓋每個模塊的這兩個插件的配置?

回答

2

如何讓子模塊設置一個my.java.version屬性(或任何你想要它命名的)並嵌入Groovy腳本來設置編譯器和包插件的版本屬性?像這樣的父母pom:

<project ...> 
    ... 
    <properties> 
     <my.java.version>1.5</my.java.version>  <!-- default Java version --> 
    </properties> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.codehaus.groovy.maven</groupId> 
       <artifactId>gmaven-plugin</artifactId> 
       <version>1.0</version> 
       <executions> 
        <execution> 
         <!-- set up properties in an early lifecycle phase --> 
         <phase>initialize</phase> 
         <goals> 
          <goal>execute</goal> 
         </goals> 
         <configuration> 
          <!-- this can be as simple or complex as you need it to be --> 
          <source> 
           if (project.properties['my.java.version'] == '1.6') { 
            project.properties['my.compiler.version'] = '1.6' 
            project.properties['my.execution.environment.version'] = 'JavaSE-1.6' 
           } 
           else { 
            project.properties['my.compiler.version'] = '1.5' 
            project.properties['my.execution.environment.version'] = 'J2SE-1.5' 
           } 
          </source> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
     <!-- now use the properties from above in the plugin configurations --> 
     <!-- assume that both of these plugins will execute in a phase later than 'initialize' --> 
     <pluginManagement> 
      <plugins> 
       <plugin> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <configuration> 
         <source>${my.compiler.version}</source> 
         <target>${my.compiler.version}</target> 
        </configuration> 
       </plugin> 
       <plugin> 
        <groupId>org.apache.felix</groupId> 
        <artifactId>maven-bundle-plugin</artifactId> 
        <configuration> 
         <!-- sorry if this part isn't correct; never used this plugin before --> 
         <instructions> 
          <Bundle-RuntimeExecutionEnvironment>${my.execution.environment.version}</Bundle-RuntimeExecutionEnvironment> 
         </instructions> 
        </configuration> 
       </plugin> 
      </plugins> 
     </pluginManagement> 
    </build> 
</project> 
+0

這似乎是最接近我想要的,所以接受。謝謝! –

3

我會親自構建您的項目,以便Java 5模塊從一個父POM和另一個父POM的Java 6模塊下降。

Global Parent (majority of global settings) 
    Java5 parent (just define source/bundle) 
    module A 
    module B 
    Java 6 parent (just define source/bundle) 
    module C 
+0

感謝您的答案;我不會爲此改組一個100多個項目。我同意它會解決一個新項目的問題,而不是我的。 –

+0

我沒有意識到你有這麼多的項目。稍微簡單一些的替代方法是讓您的Java 6項目使用獨立的父項,以覆蓋全局父項中的Java 5設置。 –

+0

看起來像一個正確和值得回答給我。 – Torsten

2

我不認爲這是一個優雅的Maven的方式來解決這個複雜的情況下,無論是你的還是鄧肯的提出的解決方案易於維護IMO,當子模塊的數量變得巨大。如果Maven無法很好地完成這項工作,例如,set-version.sh(和set-version.bat)會循環所有子模塊並重置,爲了最大可維護性,我會編寫shell腳本(和/或Windows上的批處理文件)默認java.compiler.sourceosgi.bree屬性基於version-feed.txt,version-feed.txt爲您提供了一箇中心位置來操作您的版本變化。正如你所看到的,缺點是這不是一個Maven解決方案,它需要在每次需要版本定製時在mvn ...之前運行set-version.sh

此外,對於構建/發佈規範化,我會用maven-enforcer-plugin基於屬性version.set(由set-version.sh標記)播放/暫停構建過程,並促使一些警告/錯誤消息,如果開發商沒有遵循正確的做生成時的過程。如果您更喜歡使用在每個子模塊中定義的默認值,而不是運行set-version.sh,那麼version.set還提供了靈活性,只需在父pom.xml中或通過命令行參數直接將其設置爲true即可。

樣品目錄結構:

parent/ 
    module-a/ 
    module-b/ 
    module-c/ 
    ... ... 
    pom.xml 
    set-version.sh 
    set-version.bat 
    version-feed.txt 

希望這是有意義的。

+0

有趣的解決方案,謝謝。 –

+0

根據我自己的經驗,shell腳本經常與Ant和Maven一起使用,以解決構建/發佈/部署大型企業項目的複雜場景。 – yorkw

相關問題