当前位置: 代码迷 >> java >> 如何在聚合器POM中指定Maven属性?
  详细解决方案

如何在聚合器POM中指定Maven属性?

热度:105   发布时间:2023-08-02 10:18:10.0

我有一个如下的项目结构。在聚合器中,POM project1和project 2被定义为模块。现在,如果我想添加一个可以在所有项目中访问的maven属性,我该怎么做? 在这种情况下,最佳的项目结构是什么?

如果我在聚合器POM中定义<temp.dir>data</temp.dir>类的属性,则在project1 pom.xml和project2 POM.xml中不可用。我必须在两个POM中都复制相同的属性。

project
- project1
- - pom.xml  ---- has parent POM as project A
- project2
- - pom.xml  --- has parent POM as project B
- pom.xml (Aggregator POM)

更新:为了澄清更多的project1 pom和project 2具有不同的父POM,因此无法将聚合器POM设置为父POM。

您需要将聚合器pom设置为project1project2的父project2 例如。 project1project2添加:

<parent>
    <groupId>com.agr</groupId>
    <artifactId>project-aggregator</artifactId>
    <version>1.0.0</version>
</parent>

编辑:

我知道project1和project2具有不同的父poms。

我知道要实现所需的唯一解决方案是将属性存储在父pom中。

我只能想到一个解决方案,以建立一个继承链:

Project2Parent:

<parent>
    <groupId>com.test</groupId>
    <artifactId>Project1Parent</artifactId>
    <version>0.0.1</version>
</parent>
<artifactId>Project2Parent</artifactId>
<version>0.0.1</version>

聚合器:

<parent>
    <groupId>com.test</groupId>
    <artifactId>Project2Parent</artifactId>
    <version>0.0.1</version>
</parent>
<artifactId>Aggregator</artifactId>
<version>0.0.1</version>

项目1

<parent>
    <groupId>com.test</groupId>
    <artifactId>Aggregator</artifactId>
    <version>0.0.1</version>
</parent>
<artifactId>project1</artifactId>
<version>0.0.1</version>

项目2

<parent>
    <groupId>com.test</groupId>
    <artifactId>Aggregator</artifactId>
    <version>0.0.1</version>
</parent>
<artifactId>project2</artifactId>
<version>0.0.1</version>

这是聚合器的pom:

<groupId>y</groupId>
<artifactId>y</artifactId>
<version>0.0.1</version>
<packaging>pom</packaging>
<modules>
    <module>y1</module>
    <module>y2</module>
</modules>
<properties>
    <x>1</x>
</properties>

这是y1有效pom(由Eclipse生成):

  <parent>
    <groupId>y</groupId>
    <artifactId>y</artifactId>
    <version>0.0.1</version>
  </parent>
  <groupId>y1</groupId>
  <artifactId>y1</artifactId>
  <version>0.0.1</version>
  <properties>
    <x>1</x>
  </properties>

如您所见,在父级中声明的属性x由子级继承。