问题
I'm working with Maven to management the dependencies of my projects. I have my parent POM and I'm using Dependency Management to avoid write the common dependencies in each project.
Now, I need change the version of one dependency in my child POM.
I have this of my parent POM
<dependencyManagement>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
</dependencyManagement>
My child POM has a reference to the parent POM
<parent>
<groupId>com.myproject</groupId>
<artifactId>root-parent-pom</artifactId>
<version>1.1.0.22</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
And in this child POM I want to override the version of the same dependency that I wrote in the parent POM, something like this.
<dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependency>
I did that, but when I generate my project, in this case is a WAR, the final version of the dependency that is included in my project is the version that is described on the parent POM.
Is possible override the version in child POM?
回答1:
Maybe. If you specify a distinct version, then this version will override the one in the dependencyManagement element.
But only for this single POM. It doesn't magically distribute to the next POM in the reactor build unless the next POM has this POM as parent. So if you have this setup:
parent
- mod1
- mod2
- war
and you put this into mod1, then the war won't notice since war uses parent as parent POM. The dependency mod1 says "please use 2.6" but that's the same as any other pom which uses 2.1 - there is no reason to prefer that over the other. That's the power of dependencyManagement: You get a single place where you can control the versions of all transitive dependencies.
Try mvn help:effective-pom to see what Maven will use in each part of your build.
To make the WAR pick up the overwritten version, you need to specify it in the parent POM of the WAR or in the WAR's POM itself.
The usual solution is to have a parent POM for all projects which sets the default versions (2.1). Then you have a parent POM per project which inherits from the global parent POM. Here you can set the version to 2.6. Since all modules of the project inherit from it, the per-project parent POM takes precedence.
回答2:
You need to define that dependency in a war project.
来源:https://stackoverflow.com/questions/25019211/is-possible-override-the-version-of-a-dependency-in-child-pom