Angelina
Angelina

Reputation: 2265

Why can't I use Parent properties for child POM file

In parent.pom, I have same groupID as in child POM file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.testing</groupId>
    <artifactId>master</artifactId>
    <version>11.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Master</name>
    <description>The Master project with multiple sub-modules.</description>

    <properties>
        <!-- Project Versions -->
        <ear.jar.version>11.0-SNAPSHOT</ear.jar.version>
    </properties>

In child POM, I have:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>master</artifactId>
        <groupId>com.testing</groupId>
        <version>${ear.jar.version}</version>
    </parent>

It is erroring saying: "Project 'com.testing:master:${ear.jar.version}' not found

Does anyone see what I am doing wrong?

Upvotes: 2

Views: 10320

Answers (3)

schufty
schufty

Reputation: 141

Since Maven 3.5.0-beta-1 you can use the special CI-friendly "revision" property in the parent element. You need to make sure Maven can find your parent pom.xml, either by using the traditional directory structure or providing the relativePath element(s) as necessary.

Using the traditional multi project directory layout:

.
├── pom.xml
└── child
    └── pom.xml

Parent pom.xml:

<project>
  <groupId>ima</groupId>
  <artifactId>parent</artifactId>
  <version>${revision}</version>    <--- NEW CI-friendly property
  <packaging>pom</packaging>

  <properties>
    <revision>0.1.2-SNAPSHOT</revision> <--- Defined like a normal property
  </properties>

  <modules>
    <module>child</module>
  </modules>

  <!-- NOTE! flatten-maven-plugin is needed if you have other 
       projects depending on the child -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>flatten-maven-plugin</artifactId>
        <version>1.0.0</version>
        <configuration>
          <updatePomFile>true</updatePomFile>
        </configuration>
        <executions>
          <execution>
            <id>flatten</id>
            <phase>process-resources</phase>
            <goals>
              <goal>flatten</goal>
            </goals>
          </execution>
          <execution>
            <id>flatten.clean</id>
            <phase>clean</phase>
            <goals>
              <goal>clean</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Child pom.xml:

<project>
  <parent>
    <groupId>ima</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>  <--- Property can be used in parent element
  </parent>
  <artifactId>child</artifactId>
</project>

Upvotes: 4

Lima Mairante
Lima Mairante

Reputation: 84

The following directory structure:

.
├── pom.xml
└── example-child
    └── pom.xml

with the following parent POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example-parent</artifactId>
    <name>example-parent</name>
    <packaging>pom</packaging>
    <version>${example.version}</version>

    <properties>
        <example.version>1.0.0</example.version>
    </properties>

    <modules>
        <module>example-child</module>
    </modules>

</project>

and the following child POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.example</groupId>
        <artifactId>example-parent</artifactId>
        <version>${example.version}</version>
    </parent>

    <artifactId>example-child</artifactId>
    <name>example-child</name>
    <packaging>jar</packaging>

</project>

results in successful build with warnings in the top directory:

$ mvn compile
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:example-child:jar:1.7.7
[WARNING] 'version' contains an expression but should be a constant. @ com.example:example-parent:${example.version}, C:\devel\cgi\itte\itte-client\pom-test\pom.xml, line 10, column 12
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:example-parent:pom:1.7.7
[WARNING] 'version' contains an expression but should be a constant. @ com.example:example-parent:${example.version}, C:\devel\cgi\itte\itte-client\pom-test\pom.xml, line 10, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] example-child
[INFO] example-parent
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building example-child 1.7.7
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ example-child ---
[WARNING] Using platform encoding (Cp1257 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\devel\cgi\itte\itte-client\pom-test\example-child\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ example-child ---
[INFO] No sources to compile
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building example-parent 1.7.7
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] example-child ...................................... SUCCESS [  2.403 s]
[INFO] example-parent ..................................... SUCCESS [  0.000 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.483 s
[INFO] Finished at: 2017-01-12T18:56:23+02:00
[INFO] Final Memory: 10M/309M
[INFO] ------------------------------------------------------------------------

So, what you want to do is possible, but not recommended. Better use other means for consistently managing the version like Maven release plugin.

Upvotes: 2

user177800
user177800

Reputation:

Really think critically about what you have posted:

You are trying to refer to the parent pom in the child pom with dynamically populated version information from the parent pom.

How can the version of the parent pom be retrieved before it has been included?

${ear.jar.version} is information only in the file you are trying to reference using that information; this is illogical and impossible.

Upvotes: 2

Related Questions