How to inherit dependency from a parent pom to a child pom
Asked Answered
U

6

72

I am new in using maven and jenkins. I am trying to inherit the dependencies from parent pom to child pom it shows the following errors:

[ERROR] COMPILATION ERROR : 
[INFO] -------------------------------------------------------------
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/XMLConverters.java:[10,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/XMLConverters.java:[11,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/XMLConverters.java:[15,38] cannot find symbol
  symbol:   class AbstractRequestMessageData_Type
  location: class com.td.inv.wss.util.XMLConverters
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/XMLConverters.java:[26,23] cannot find symbol
  symbol:   class AbstractResponseMessageData_Type
  location: class com.td.inv.wss.util.XMLConverters
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/UsTermRateItemComparator.java:[5,42] package com.rpmtec.current.UsTermRate_Type does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/UsTermRateItemComparator.java:[7,61] cannot find symbol
  symbol: class UsTermRateItems
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/UsTermRateItemComparator.java:[9,28] cannot find symbol
  symbol:   class UsTermRateItems
  location: class com.td.inv.wss.util.UsTermRateItemComparator
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/UsTermRateItemComparator.java:[9,48] cannot find symbol
  symbol:   class UsTermRateItems
  location: class com.td.inv.wss.util.UsTermRateItemComparator
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[9,40] package com.fasterxml.jackson.annotation does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[10,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[11,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[12,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[13,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[14,42] package com.rpmtec.current.UsTermRate_Type does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[19,2] cannot find symbol
  symbol: class JsonIgnoreProperties
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[69,22] cannot find symbol
  symbol:   class ORCA_GETTERMHOLDINGRS_Type
  location: class com.td.inv.model.COIRQ
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[69,66] cannot find symbol
  symbol:   class RPM_GETPLANACCOUNTOVERVIEWRS_Type
  location: class com.td.inv.model.COIRQ
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/model/COIRQ.java:[70,25] cannot find symbol
  symbol:   class ORCA_GETTERMINSTRUCTIONRS_Type
  location: class com.td.inv.model.COIRQ
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[5,26] package javax.ws.rs.client does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[6,26] package javax.ws.rs.client does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[7,26] package javax.ws.rs.client does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[8,26] package javax.ws.rs.client does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[9,24] package javax.ws.rs.core does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[15,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[16,26] package com.rpmtec.current does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[23,57] cannot find symbol
  symbol:   class AbstractRequestMessageData_Type
  location: class com.td.inv.wss.util.RPMInvoker
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/util/RPMInvoker.java:[24,41] cannot find symbol
  symbol:   class AbstractResponseMessageData_Type
  location: class com.td.inv.wss.util.RPMInvoker
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/application/InvestmentAPI.java:[4,19] package javax.ws.rs does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/application/InvestmentAPI.java:[5,24] package javax.ws.rs.core does not exist
[ERROR] /D:/jenkins/workspace/CBAW/testP/WSW_Investment/src/main/java/com/td/inv/wss/application/InvestmentAPI.java:[9,36] cannot find symbol
  symbol: class Application

Here is my parent POM:

.....
<modelVersion>4.0.0</modelVersion>
<groupId>group1</groupId>
<artifactId>group1-artifact</artifactId>
<version>1.0.1</version>
<packaging>pom</packaging>

<modules>
     <module>child1</module>
</modules>
.......

Here is my child POM:

.....
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>group1</groupId>
    <artifactId>group1-artifact</artifactId>
    <version>1.0.1</version>
    <relativePath>(full url.....)/jenkins-parent-pom//pom.xml</relativePath>
</parent>
<groupId>group1</groupId>
<artifactId>child1</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>
......

Here is how I tried to inherit dependency in child POM from parent POM:

<dependencyManagement>  
  <dependencies>
    <dependency>
      <groupId>group1</groupId>
      <artifactId>group1-artifact</artifactId>
      <version>1.0.1</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

If I put those same dependencies in the child POM, it works perfectly. I do clean install for installing and deploy for deploying in nexus by using jenkins. I am using maven-3.3.9. In jenkins, I have read the parent and child poms in two different maven projects from git. I want to inherit all the dependencies and plugins from parent POM. Is it possible?

Urbain answered 10/8, 2016 at 19:44 Comment(0)
L
96

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.

Longstanding answered 10/8, 2016 at 19:52 Comment(6)
You mean that, in parent pom i should declare all the dependencies within <dependencies> tag, not within <depenedencyManagement> or in child pom? I have put all the dependencies within <depenedencyManagement> tags in parent pom. Is that a problem?Urbain
If you goal is to inherit particular dependencies to all child poms, without a need to decleare them in each of the children - then yes, you should declare them within <dependencies> and not <dependencyManagement>. On the other hand, a dependency declared within <dependencyManagement> can be used only when you need it in a child pom - you just write its group and artifact inside that child's <dependencies>. The benefit being that other properties (version) are declared only once in the parent.Longstanding
Thanks. yes, my goal is to inherit particular dependencies to child poms. Just to make sure, according to your reply I will put those particular dependencies in <dependencies> (not in <dependencyManagement> tag ) in parent pom, right?Urbain
And how can I do to use the classes of the parent project through the child project?Dene
RE: the comment "Be careful when declaring dependencies that every child pom will inherit. Very quickly.." note that @Longstanding is referring to the other option of not using <dependencyManagement> However, it is important to note both have their valid use cases, ...and I wish Maven used different/better names to identify these two mechanisms.Luttrell
so if we do not want to inherit a dependency from parent pom, we should add it into <dependencyManagement>? so we have to specify group-id and artifact-id in child pom if we want to inherit.Southern
S
54

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.

Swartz answered 10/8, 2016 at 19:51 Comment(5)
Thanks. But there is no way I can inherit the dependencies from parent pom without adding the dependency in child pom in the <dependencies /> node, without setting the version of the dependency, right?Urbain
I updated my answer as it was not accurate. It turns out you won't need to add dependencies in the child's pom if you add them in the <dependencies /> node of the parent pom. It is your choice.Swartz
While you can certainly do it, it's rarely a good idea to put <dependencies /> in a parent pom outside of <dependencyManagement />. All of your child modules will inherit all of these dependencies all of the time. Any other projects that might consume one of your modules will also transiently get all of these deps and possibly conflict or worse, particularly if this child module does not make use of all of them. The original solution by @ArthurNoseda is best.Bigwig
And how can I do to use the classes of the parent project through the child project?Dene
To this work, the child project must have the <parent> tag, such this answer illustrates https://mcmap.net/q/273289/-how-to-inherit-dependency-from-a-parent-pom-to-a-child-pomTwill
L
30

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.

Lakshmi answered 11/8, 2016 at 20:29 Comment(5)
And how can I do to use the classes of the parent project through the child project?Dene
Isn't the parent project of type pom? There shouldn't be any classes in it.Swartz
Yes, @ArthurNoseda is correct. Your parent pom is usually of type pom instead of a jar or a war. Ideally, the parent pom will not have any code. It's main purpose is to behave like an aggregator for all your child poms.Lakshmi
If I want an exclusion to happen in the dependencies when used in child poms, can I just excluded from the parent <dependencyManagement>...<dependency>?Ovid
If you want the jar to be excluded from any specific child, then it is better to add the exclusion to that child pom. Whereas, if you want the jar to be excluded from all the child poms, then you can add the exclusion in the parent pom.Lakshmi
V
5

parent pom.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>

    <groupId>com.ll</groupId>
    <artifactId>parent-module</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <!-- child will inherit all content -->
    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.11.0</version>
        </dependency>
    </dependencies>

    <!-- child will inherit only version-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.5.7</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <modules>
        <module>sub-module1</module>
        <module>sub-module2</module>
    </modules>
</project>

child pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         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>
    <parent>
        <groupId>com.ll</groupId>
        <artifactId>parent-module</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.ll</groupId>
    <artifactId>sub-module1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
    </dependencies>
</project>
1.child will use zookeeper:3.5.7, the version is from parent pom.xml <dependencyManagement>:<dependencies>:<dependency>. 

2.child will use protobuf-java:3.11.o, it is from parent pom.xml <dependencies>:<dependency>.
Vauban answered 13/10, 2020 at 2:25 Comment(0)
G
1

Whatever is added in <dependencyManagement> will be automatically added into child's pom. The only thing needed to be taken care of that in child's pom we need not to add the version explicitly otherwise that that will be other dependency only available for the child module.

parent_pom.xml

    <dependencyManagement>
      <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
     </dependencies>
  </dependencyManagement>

child_pom.xml

        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
        </dependency>
Gush answered 10/4, 2021 at 21:7 Comment(0)
S
1

In addition to other replies, I want add few summarized points here regarding dependency declaration. There are 2 sections related to dependencies,

  • dependencymanagement : just listing the definition of dependencies (nothing included in actual build or build output). We can define the version here.
  • dependency : what ever dependency included here goes into build and also included in the output (as per scope). If we are referring to any dependency declared in dependencymanagement section, we dont need to mention the version here. But if we mention version here, it dominates.

So both parent and child can have these 2 sections,

  • parent.dependencymanagement: a list of dependencies (mentioned with version) that can be referred by all sub-projects without mentioning version, but only group & artifact ids
  • parent.dependency: a list of dependencies that are automatically inherited by all sub projects (can be used for common dependencies like junit)
  • child.dependencymanagement: a list of dependencies (without mentioning version) that can be reffered by its sub-projects (if any)
  • child.dependency: list of dependencies that comes with this sub-module

In summary we mention the version in parent's dependency management, we can use that dependency with group & artifact id everywhere else unless we want to use some other version explicitely

Solemn answered 6/7, 2023 at 6:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.