Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven managed dependencies - resolving ${project.version} from parent pom

How is placeholder ${project.version} resolved for managed properties from parent pom? I've expected that it is resolved globally, so when the parent pom has version 2, ${project.version} would also be resolved to version 2.

In parent pom I have:

<groupId>my.group</groupId> <artifactId>parent</artifactId> <version>2</version> <packaging>pom</packaging>  <dependencyManagement>     <dependencies>         <dependency>             <groupId>my.group</groupId>             <artifactId>dep</artifactId>             <version>${project.version}</version>         </dependency>     </dependencies> </dependencyManagement>      

In child I use

<parent>     <groupId>my.group</groupId>     <artifactId>parent</artifactId>     <version>2</version> </parent> <version>1</version> <artifactId>child</artifactId> 

But the artifact my.group.dep.1.jar is used, instead of my.group.dep.2.jar. So the placeholder is resolved to the version of the project using the managed dependency, and not those of the project defining the dependency.

Is that expected behaviour? I'm using maven 3.0.4.

like image 535
Danubian Sailor Avatar asked Jun 03 '13 11:06

Danubian Sailor


People also ask

How do you get dependency from parent POM?

Now child POM need to refer the parent POM using parent tag and specifying groupId/artifactId/version attributes. This pom file will inherit all properties and dependencies from parent POM and additionally can include extra sub-project specific dependencies as well.

How do I exclude a specific version of a dependency in Maven?

Multiple transitive dependencies can be excluded by using the <exclusion> tag for each of the dependency you want to exclude and placing all these exclusion tags inside the <exclusions> tag in pom. xml. You will need to mention the group id and artifact id of the dependency you wish to exclude in the exclusion tag.

How do I override a parent POM?

It sounds like A version 2.5 is being included transitively by another dependency. This puts both version 4.3 and 2.5 at the same length. By explicitly defining dependency of A 2.5 in your project it will then be the nearest and override any other versions.


1 Answers

You have to skip <version> tag in child, but keep the <parent><version> ... </parent> tag.

http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Project_Inheritance

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.

like image 152
Terafor Avatar answered Sep 30 '22 00:09

Terafor