Ashish Tyagi
Ashish Tyagi

Reputation: 191

Maven - Get non-inheritable pom version.

Assume a parent pom with version 1.0 and a child pom with version 2.0.

Now I define a dependency like this in the parent.

<dependency>
   <groupId>somedep</groupId>
   <artifactId>somedep</artifactId>
   <version>${project.version}</version>
</dependency>

In the parent, the version evaluates to 1.0, but in child it evaluates to 2.0. Is there a way, in which I can make it evaluate to 1.0 in child too (Ofcourse no version harcoding in parent is permitted)?

Upvotes: 0

Views: 69

Answers (1)

Nikola Yovchev
Nikola Yovchev

Reputation: 10226

EDIT:

If we look in http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance, we can see that:

One factor to note is that these variables are processed after inheritance as outlined above. This means that if a parent project uses a variable, then its definition in the child, not the parent, will be the one eventually used.

Which means that you can't rely on variables which get dynamically resolved in parent and child and are of the type `${project.version} if your child and parent have different versions, which is generally not what you'd want but very specific to your case. I guess what you are left over with is to hardcode the dependency in the parent using something like depedencyManagement:

Parent:

<project>
   <groupId>example</artifactId>
   <artifactId>masta</artifactId>
   <version>1.0</version>
   <dependencyManagement> 
      <dependencies>
          <dependency>
              <groupId>someDep</groupId>
              <artifactId>someDep</artifactId>
              <version>1.0</version>
          </dependency>
      </dependencies>
   </dependencyManagement>
   <dependencies>
      <dependency>
         <groupId>someDep</groupId>
         <artifactId>someDep</artifactId>
       </dependency>
   </dependencies>
</project>

And child:

<project>
   <parent>
      <groupId>example</artifactId>
      <artifactId>masta</artifactId>
      <version>1.0</version>
   </parent>
   <groupId>example.masta</artifactId>
   <artifactId>child</artifactId>
   <version>2.0</version>
   <dependencies>
      <dependency>
         <groupId>someDep</groupId>
         <artifactId>someDep</artifactId>
       </dependency>
   </dependencies>
</project>

or, indeed, use ${parent.version} explicitly in the child. One thing to note is I'd generally not have aggregator projects like parent introduce any dependencies but have only dependencyManagement.

This is the second approach, without dependency management:

Parent:

  <project>
       <groupId>example</artifactId>
       <artifactId>masta</artifactId>
       <version>1.0</version>
       <dependencies>
          <dependency>
             <groupId>someDep</groupId>
             <artifactId>someDep</artifactId>
             <version>${project.version}</version>
           </dependency>
       </dependencies>
    </project>

And child:

   <project>
       <parent>
          <groupId>example</artifactId>
          <artifactId>masta</artifactId>
          <version>1.0</version>
       </parent>
       <groupId>example.masta</artifactId>
       <artifactId>child</artifactId>
       <version>2.0</version>
       <dependencies>
          <dependency>
             <groupId>someDep</groupId>
             <artifactId>someDep</artifactId>
             <version>${parent.version}</version>
           </dependency>
       </dependencies>
    </project>

But I guess none of the approaches actually fully satisfies what you want.

Upvotes: 1

Related Questions