Maven项目聚合和变量

Maven项目聚合和变量,maven,maven-3,pom.xml,Maven,Maven 3,Pom.xml,Agregate项目1: <groupId>com.mycompany</groupId> <artifactId>builder</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <modules> <module>../module-1&l

Agregate项目1:

    <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>
    <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>
如何配置聚合项目并将变量转换为模块

更新@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>

如果我正确理解你的问题,你希望你的模块有多个家长。这是不可能的。看这个


你可以试试@Gab的建议。基本上定义了两个-一个用于builder-v1,另一个用于builder-v2。您几乎可以在配置文件中执行任何操作,包括指定不同的模块集和不同的属性/版本。

可以使用两个配置文件定义一个公共父级,为${lib.version}定义不同的值,并在聚合POM中自动激活一个配置文件。您的所有模块都将从父模块继承

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)
[编辑] 在聚合POM中自动激活一个概要文件以避免在maven命令中指定属性的唯一技巧是

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

因为基于属性的激活仅适用于系统属性,而不是pom中定义的系统属性,并且默认情况下,您不能覆盖继承的配置文件配置活动。

您是否建议为所有模块创建具有公共父级的hier,但不包括2个聚合项目?这很有帮助,谢谢,我找到了一个有效的配置:在公共父pom中创建2个配置文件并设置属性。按属性激活配置文件。在执行mvn:mvn clean package-DsomeProperty=v1>时设置属性无需在maven命令中指定属性我们无法在项目pom中使用设置:Unrecogned标记:“settings”,我们只能在maven设置中激活配置文件。这不是我想做的。我尝试了两个自动构建聚合项目,这项使用maven命令activate的工作忽略了设置;activeByDefault如何帮助我选择配置文件?在off docs中,这些技巧并没有任何意义……你们是对的,我认为可以覆盖继承的配置文件的激活,但事实并非如此。
'dependencies.dependency.version' for com.somelib:some-lib:jar must be a valid version but is '${lib.version}'.
    <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>
    <parent>
        <groupId>com.mycompany</groupId>
        <artifactId>parent</artifactId>
        <version>1.0.0</version>
    </parent>

    <groupId>com.mycompany</groupId>
    <artifactId>module-1</artifactId>
    <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>
mvn clean package --file=pom_v1.xml -Dproject.type=v1 -Dproject.main.module=module-1
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)
<activation>
  <file>
    <exists>relative_path_of_file_specific_to_project</exists>
  </file>
</activation>