Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to have the parent version as Property to give to the children?

It is about Maven POM's

If i would like to have the Version of my Parent also to be the Version of my Dependencies i must set a Property thats value is ${project.parent.version}.

The Problem then happen when a Child of my Main POM (Which has the ${project.parent.version} Property inside of it because it's Parent it some Project I don't Administrate) recalculate the property and think that the value of the created Property is now the version of my Main POM.

--SuperParent (not in my Administration) | Version = 1.2.3

----MainPom | Version = 1.0.0 | Property <test>${project.parent.version}</test> -> 1.2.3

------Child Pom | Version 1.0.0 | Property ${test} is now 1.0.0

<project>
<!-- Super Pom -->
 <groupId>groupId</groupId>
    <artifactId>artifactId</artifactId>
    <version>1.2.3</version>
</project>

<project>
<!-- MainPom -->
    <groupId>othergroupId</groupId>
    <artifactId>otherartifactId</artifactId>
    <version>1.0.0</version>
    <parent>
         <groupId>groupId</groupId>
         <artifactId>artifactId</artifactId>
         <version>1.2.3</version>
     </parent>
     <properties>
     <dependency.version>${project.parent.version}</dependency.version>
     </properties>
     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>dependencyGroupId<groupId>
                <artifactId>dependency</artifactId>
                <version>${dependency.version}</version>
            </dependency>
        </dependencies>
     </dependencyManagement>
</project>
<project>
<!-- ChildPom -->
    <groupId>childGroupId</groupId>
    <artifactId>childArtifactId</artifactId>
    <version>1.0.0</version>
    <parent>
         <groupId>othergroupId</groupId>
         <artifactId>otherartifactId</artifactId>
         <version>1.0.0</version>
     </parent>
        <dependencies>
            <dependency>
                <groupId>dependencyGroupId<groupId>
                <artifactId>dependency</artifactId>
            </dependency>
        </dependencies>
</project>

In the End is the Property ${dependency.version} in the Child Pom 1.0.0 instead of 1.2.3. Is this a wanted behavior of Maven? And what could I to do make it work?

Things that can't be changed:

  • SuperPom
  • Main Pom Version
like image 516
Serverfrog Avatar asked Nov 01 '16 12:11

Serverfrog


1 Answers

Maven first process inheritance to build the effective pom and then process variables expansion.

In others words, the parent and child pom contents are processed as a single merged file for each child pom. So when your child pom is being processed the ${project.parent.version} is 1.0.0, not 1.2.3.

I couldn't find a way to reference the "grandparent" of a pom, so it seems that the only solution is to put the version as a static number both in the parent.version and properties.dependency.version.

like image 198
Vinicius Avatar answered Oct 20 '22 01:10

Vinicius