How to inherit dependency from a parent pom to a child pom
In fact, you've got 2 ways to deal with the problem.
- 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. - 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.
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.