I have a Jackson with version 2.3 in the parent pom and in my child pom I require version 2.9, is there any way to exclude the parent pom dependency?
Asked
Active
Viewed 1.1k times
2 Answers
2
You can override the dependency's version using the dependencyManagement section:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.8.8</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
</dependency>
</dependencies>
This is an example of parent:
<?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>test</groupId>
<artifactId>test-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>test-child</module>
</modules>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.14</version>
</dependency>
</dependencies>
And this is an example of child that overwrite the dependency version:
<?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>test-parent</artifactId>
<groupId>test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-child</artifactId>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
</dependencies>
This are the dependencies resolved for the child
mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] --------------------------< test:test-child >---------------------------
[INFO] Building test-child 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ test-child ---
[INFO] test:test-child:jar:1.0-SNAPSHOT
[INFO] \- org.slf4j:slf4j-api:jar:1.7.25:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.703 s
[INFO] Finished at: 2018-07-23T18:08:48+02:00
[INFO] ------------------------------------------------------------------------

gdegani
- 846
- 6
- 17
-
Hi, i tried this but it didn't work :( still resolving parent pom version while loading. – Sushil Ks Jul 23 '18 at 13:36
-
Maybe you should post your poms. With a simple test it works as expected: – gdegani Jul 23 '18 at 16:05
-
Thanks, @gdegani this thing is working fine, I was trying to instantiate the child module class inside parent module through reflection and hence the class loader was loading the parent module version for the dependency class. I guess we can't avoid that other than writing a custom class loader right? – Sushil Ks Jul 24 '18 at 05:16
-
Classloader... reflection... How are you using maven in your project ? When you ask for help you should provide more details about the question and its context. – gdegani Jul 24 '18 at 07:45
-
Apologies, for not showing an example pom, however your example is pretty much the same I had and your solution worked for my use-case once I did relocating classes through maven shade plugin. Thanks. – Sushil Ks Jul 25 '18 at 03:16
-
1@gdegani I think you don't need to use dependencyManagement in child pom..we can just directly add our inherited dependency in
tag with newer version number.This newer version will overwrite its parent version in child pom. – Udit Kumawat Jun 19 '19 at 04:44
0
As long as it is a simple replacement with the same groupId
and artifactId
, you can simply add the dependency to the child pom. The version number in the child pom will be the one that is effective.
Parent POM:
<dependency>
<groupId>com.MyProjectName</groupId>
<artifactId>core-config</artifactId>
<version>1.00</version>
</dependency>
Child POM:
(a different version, and also exclude the XML parser that comes bundled with the parent)
<dependency>
<groupId>com.MyProjectName</groupId>
<artifactId>core-config</artifactId>
<version>0.99</version>
<exclusions>
<exclusion>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
</exclusions>
</dependency>

JonathanDavidArndt
- 2,518
- 13
- 37
- 49