2014-03-12 38 views
1

Agregate項目1:Maven項目聚集和變量

<groupId>com.mycompany</groupId> 
    <artifactId>builder</artifactId> 
    <version>1.0.0</version> 
    <packaging>pom</packaging> 
    <modules> 
     <module>../module-1</module> 
     <module>../module-2</module> 
    </modules> 
    <properties> 
     <project.parent.pom>../builder-v1/pom.xml</project.parent.pom> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <lib.version>2.5.1</lib.version> 
    </properties> 

Agregate項目2:

<groupId>com.mycompany</groupId> 
    <artifactId>builder</artifactId> 
    <version>1.0.0</version> 
    <packaging>pom</packaging> 
    <modules> 
     <module>../module-1</module> 
     <module>../module-4</module> 
     <module>../module-6</module> 
    </modules> 
    <properties> 
     <project.parent.pom>../builder-v2/pom.xml</project.parent.pom> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <lib.version>2.6.0</lib.version> 
    </properties> 

模塊1:

<parent> 
     <groupId>com.mycompany</groupId> 
     <artifactId>builder</artifactId> 
     <version>1.0.0</version> 
     <relativePath>../builder-v1/pom.xml</relativePath> 
     <!--<relativePath>${project.parent.pom}</relativePath> not work--> 
    </parent> 

    <groupId>com.mycompany</groupId> 
    <artifactId>module-1</artifactId> 

節relativePath與骨料POM可變不起作用

<relativePath>../builder-v1/pom.xml</relativePath> 
<!--<relativePath>${project.parent.pom}</relativePath> not work--> 

但我需要diffrent $ {lib.version}在聚合項目。如果我從 模塊1刪除繼承部分:

<!--<parent> 
     <groupId>com.mycompany</groupId> 
     <artifactId>builder</artifactId> 
     <version>1.0.0</version> 
     <relativePath>../builder-v1/pom.xml</relativePath> 
    </parent>--> 

    <groupId>com.mycompany</groupId> 
    <artifactId>module-1</artifactId> 

變量不傳遞給子模塊和我得到的錯誤:

'dependencies.dependency.version' for com.somelib:some-lib:jar must be a valid version but is '${lib.version}'. 

如何配置骨料項目和轉讓變量放入模塊?

UPDATE與@Gab評論

父POM:

<groupId>com.mycompany</groupId> 
    <artifactId>parent</artifactId> 
    <version>1.0.0</version> 
    <packaging>pom</packaging> 

    <profiles> 
     <profile> 
     <id>v1</id> 
      <activation> 
       <property> 
        <name>project.type</name> 
        <value>v1</value> 
       </property> 
      </activation> 
     <properties> 
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    
      <lib.version>2.5.1</lib.version> 
     </properties> 
     </profile> 
     ... 
    </profiles> 

模塊1:

<parent> 
     <groupId>com.mycompany</groupId> 
     <artifactId>parent</artifactId> 
     <version>1.0.0</version> 
    </parent> 

    <groupId>com.mycompany</groupId> 
    <artifactId>module-1</artifactId> 

Agregate項目1:

<groupId>com.mycompany</groupId> 
    <artifactId>builder-v1</artifactId> 
    <version>1.0.0</version> 
    <packaging>pom</packaging> 
    <modules> 
     <module>../module-1</module> 
     <module>../module-2</module> 
    </modules> 

當執行建設者-V1的目標啓動配置文件project.type = V1

mvn clean package --file=pom_v1.xml -Dproject.type=v1 -Dproject.main.module=module-1 

回答

0

也許定義一個共同的父母,其中有2個配置文件,爲$ {lib.version}定義不同的值,並自動激活您的「聚合」poms中的一個配置文件。您所有的模塊從父繼承一個

parent-pom (defining 2 different profiles) 
| 
+ aggregate 1 (inherit from parent-pom - activate profile 1) 
| | 
| + module 1 (inherit from parent-pom) 
    | 
| + module 2 (inherit from parent-pom) 
+ aggregate 2 (inherit from parent-pom - activate profile 2) 
    | 
    + module 1 (inherit from parent-pom) 
    | 
    + module 3 (inherit from parent-pom) 

[編輯] 似乎是唯一的技巧自動激活一個配置文件在您的「集合」勁歌,以避免在Maven命令來指定一個屬性是

<activation> 
    <file> 
    <exists>relative_path_of_file_specific_to_project</exists> 
    </file> 
</activation> 

因爲基於屬性的激活僅適用於系統屬性(不是在pom中定義的屬性),並且您無法覆蓋繼承的配置文件配置(默認情況下處於活動狀態)。

+0

這很有幫助,謝謝 我發現一個工作配置: 在常見的父pom中創建2個配置文件並設置屬性。按屬性激活配置文件。在執行mvn時設置屬性:mvn clean package -DsomeProperty = v1 –

+0

>無需在maven命令中指定屬性 我們無法在項目pom中使用設置:無法識別的標記:'設置',我們只能激活配置文件maven設置。這不是我想要做的。我嘗試conf兩個自動構建聚合項目,並通過maven命令激活此工作 –

+0

錯過了設置;) – Gab

0

如果我理解你的問題吧,你希望你的模塊有多個家長。這不可能。看到這個discussion

你可以試試@Gab的建議。基本上定義兩個profiles - 一個用於builder-v1,另一個用於builder-v2。您可以在配置文件中執行幾乎任何操作,包括指定不同的模塊集和不同的屬性/版本。

+0

您是否建議爲所有模塊創建一個具有共同父項的hier,但是需要2個聚合項目? –