如何继承从父pom到子pom的依赖关系

时间:2016-08-10 19:44:07

标签: maven jenkins parent-pom

我是使用maven和jenkins的新手。我试图继承父pom到子pom的依赖关系,它显示以下错误:

[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

这是我的父母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>
.......

这是我的孩子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>
......

以下是我尝试从父POM继承子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>

如果我将这些相同的依赖项放在子POM中,它就可以完美地运行。我clean install进行安装,deploy使用jenkins在nexus中进行部署。我正在使用maven-3.3.9。在詹金斯,我在git的两个不同的maven项目中读过父母和子女的poms。我想从父POM继承所有依赖项和插件。可能吗?

5 个答案:

答案 0 :(得分:47)

您应该声明要在<dependencies>部分下继承的依赖项来实现此目的。 <dependencyManagement>用于定义,必要时,必须在特定子项的<dependencies>内引用,才能生效。

UPDATE :声明每个子pom将继承的依赖项时要小心。您很快就会因为它们在父级中声明而最终不具备您真正不需要的依赖项。正如其他评论者所提到的,<dependencyManagement>可能是更好的选择,尽管它不是你原本想要的。

答案 1 :(得分:29)

事实上,你有2种方法可以解决这个问题。

  1. 要么在<dependencyManagement />节点下的父pom中以及需要它的每个子节点中考虑依赖关系,请在<dependencies />节点中添加依赖关系。您可以选择不设置依赖项的版本。
  2. 或者在<dependencies />节点中声明父pom中的依赖项,并且每个子项都将从依赖项中受益。
  3. 例如,如果你在父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>
    

    然后slf4j-api将成为所有孩子的依赖。但是,如果需要,您必须在孩子的pom中添加对slf4j-simple的依赖:

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

    对于插件,它的工作方式相同,但<pluginManagement /><plugins />节点。所有配置都可以在父pom的插件定义中进行,您只需在孩子pom的<build />部分声明插件。

答案 2 :(得分:14)

以下是您应该如何使用父母和子女的例子。

父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>
     // 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>
.......

如果您在dependencyManagement标记中指定了依赖关系,则仅表示您正在为子pom 创建此jar。此时 NOT 实际下载jar。子pom必须提供groupIdartifactId 显式来下载并使用jar来编译其类。 注意:您不必在子poms 中包含依赖项的版本。

儿童节将如下:

.....
<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>

.......

最好将所有子项中的依赖项放在父pom的dependencyManagement标记中。这样,您就可以从一个地方管理这些依赖项的版本。

答案 3 :(得分:0)

父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>

子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>.

答案 4 :(得分:0)

<dependencyManagement> 中添加的任何内容都会自动添加到孩子的 pom.xml 中。唯一需要注意的是在 child 的 pom 中,我们不需要显式添加版本,否则这将是仅适用于 child 模块的其他依赖项。

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>