How to inherit dependency from a parent pom to a child pom How to inherit dependency from a parent pom to a child pom jenkins jenkins

How to inherit dependency from a parent pom to a child pom


You should declare dependencies you want to inherit under a <dependencies> section to achieve this. <dependencyManagement> is used for definitions that must be referenced later, whenever needed, within the <dependencies> of a particular child to become effective.

UPDATE: Be careful when declaring dependencies that every child pom will inherit. Very quickly you can end up having dependencies you don't really need just because they are declared in the parent. As mentioned by other commenters, <dependencyManagement> may be a better choice, although it isn't what you wanted originally.


In fact, you've got 2 ways to deal with the problem.

  1. Either you factor the dependencies in the parent pom under the <dependencyManagement /> node and in each child that requires it, add the dependency in the <dependencies /> node. You can choose not to set the version of the dependency.
  2. Or you declare the dependencies in the parent pom in the <dependencies /> node, and each child will benefit from the dependency.

So for example, if you declare this in the parent pom:

<dependencies>    <dependency>        <groupId>org.slf4j</groupId>        <artifactId>slf4j-api</artifactId>        <version>1.7.21</version>    </dependency></dependencies><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-simple</artifactId>            <version>1.7.21</version>            <scope>runtime</scope>        </dependency>    </dependencies></dependencyManagement>

Then slf4j-api will be a dependency for all children. However, you will have to add a dependency on slf4j-simple in the child's pom, should it require it:

<dependencies>    <dependency>        <group>org.slf4j</group>        <artifactId>slf4j-simple</artifactId>    </dependency></dependencies>

For the plugins, it works the same, but with the <pluginManagement /> and <plugins /> nodes. All configuration can go in the parent pom's definition of the plugin, and you simply declare the plugin in the <build /> section of your child pom.


Below is the example of how you should use the parent and child poms.

The parent pom is as follows:

.....<modelVersion>4.0.0</modelVersion><groupId>group1</groupId><artifactId>group1-artifact</artifactId><version>1.0.1</version><packaging>pom</packaging><modules>     <module>child1</module>     // add more childs here</modules><dependencyManagement>    <dependencies>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-simple</artifactId>            <version>1.7.21</version>        </dependency>        <dependency>            <groupId>org.abc</groupId>            <artifactId>xyz</artifactId>            <version>1.0.0</version>        </dependency>    </dependencies></dependencyManagement>.......

If you specify a dependency in the dependencyManagement tag, it simply means that you are making this jar available for the child pom. It would NOT actually download the jar at this point. The child pom will have to provide the groupId and the artifactId explicitly to download and use the jar to compile its classes. Note: you don't have to include the version of the dependency in the child poms.

The child pom will be as follows:

.....<modelVersion>4.0.0</modelVersion><parent>            // this is how you will inherit from parent pom    <groupId>group1</groupId>    <artifactId>group1-artifact</artifactId>    <version>1.0.1</version></parent><groupId>child1</groupId>    <dependencies>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-simple</artifactId>            <scope>runtime</scope>            // no version needed as it would inherit from the parent pom        </dependency>        <dependency>            <groupId>org.abc</groupId>            <artifactId>xyz</artifactId>            // no version needed as it would inherit from the parent pom        </dependency>    </dependencies>.......

It is a good practice to put dependencies common to all the childs in the dependencyManagement tag of the parent pom. This way you can manage the versions of these dependencies from one single place.