简体   繁体   中英

How to specify maven properties in a aggregator POM?

I have a project structure like below.In aggregator POM project1 and project 2 are defined as modules.Now if i want to add a maven property that can be accessed in all the projects how can i do it?. What is the best project structure in this case.?

If i define property like <temp.dir>data</temp.dir> in the aggregator POM then it is not available in project1 pom.xml and project2 POM.xml.I have to duplicate the same property in both the POM's.

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

UPDATE:To clarify more project1 pom and project 2 has different parent POM's.So aggregator POM cannot be set as parent.

You need to set the aggregator pom as the parent of project1 and project2 . eg. in project1 and project2 poms add:

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

EDIT:

I understand that project1 and project2 have different parent poms.

The only solution i know about to achieve what You want is to store the properties in parent pom.

I can think only of one solution, to make an inheritance chain:

Project2Parent:

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

Aggregator:

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

Project1

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

Project2

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

Here is aggregator's 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>

This is y1 effective pom (generated by 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>

As you can see property x declared in parent is inherited by child.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM