Maven项目聚合和变量 [英] Maven Project Aggregation and Variables

查看:134
本文介绍了Maven项目聚合和变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总体项目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>

总体项目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>

带有来自集合pom的变量的relativePath部分不起作用

Section relativePath with variable from aggregate pom not work

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

但是我需要在汇总项目中使用不同的$ {lib.version}.如果我从中删除继承部分 单元1:

But I need diffrent ${lib.version} in aggregate projects. If I delete inheritance part from Module 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>

变量未传递到子模块,并且出现错误:

Variables are not passed to the child modules and i got error:

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

如何配置聚合项目并将变量传输到模块中?

How can I configure aggregate project and transfer variables into 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>

模块1:

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

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

总体项目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>

当执行builder-v1目标时,激活配置文件 project.type = v1 :

When execute builder-v1 goals activate profile project.type=v1:

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

推荐答案

可以定义一个具有2个配置文件的公共父对象,这些配置文件为$ {lib.version}定义不同的值,并自动激活聚合" pom中的一个配置文件.您所有的模块都将从父模块继承

maybe define a common parent with 2 profile defining different values for ${lib.version} and automatically activate one profile in your "aggregates" poms. All your module will inherit from the parent one

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)

似乎唯一可以自动激活聚合" poms中的一个配置文件以避免在maven命令中指定属性的窍门是

[edit] Seems the only trick to automatically activate one profile in your "aggregates" poms to avoid to specify a property in the maven command is

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

因为基于属性的激活仅适用于系统属性(不适用于pom中定义的属性),并且您无法覆盖继承的配置文件配置(默认情况下处于活动状态).

because the property based activation works only with system properties (not the one defined in pom) and you can't override inherited profile configuration (active by default).

这篇关于Maven项目聚合和变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆