当前位置: 首页 > 知识库问答 >
问题:

如何从父pom继承依赖到子pom

苏翰学
2023-03-14

我是使用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>
.......

这是我的孩子波姆:

.....
<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中放置这些相同的依赖项,它会完美地工作。我使用jenkins在nexus中进行安装和部署。我使用的是maven-3.3.9。在jenkins中,我阅读了git中两个不同maven项目中的父和子POM。我想从父POM继承所有依赖项和插件。有可能吗?

共有3个答案

令狐宣
2023-03-14

下面是如何使用父和子POM的示例。

父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标签中指定了一个依赖项,那么这仅仅意味着您正在使这个jar可用于子pom。此时它实际上不会下载jar。子pom必须显式地提供groupId和artifactId,以下载并使用jar编译其类。注意:您不必在子POM中包含依赖项的版本。

儿童pom如下:

.....
<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标记中放置所有子级通用的依赖项是一种很好的做法。通过这种方式,您可以从一个地方管理这些依赖项的版本。

南宫才艺
2023-03-14

事实上,你有两种方法来解决这个问题。

  1. 要么在<代码>

例如,如果您在父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>

对于插件,它的工作原理是相同的,但与<代码>

尹庆
2023-03-14

您应该在声明要继承的依赖项

更新:声明每个子pom都将继承的依赖项时要小心。很快,您就会拥有您并不真正需要的依赖项,因为它们是在父级中声明的。正如其他评论者所提到的,

 类似资料:
  • 问题内容: 我是使用Maven和Jenkins的新手。我试图继承从父pom到子pom的依赖关系,它显示以下错误: 这是我的父母POM: 这是我的孩子POM: 这是我尝试从父POM继承子POM中的依赖项的方法: 如果我将相同的依赖项放在子POM中,则效果很好。我使用jenkins在nexus中进行安装和部署。我正在使用maven-3.3.9。在詹金斯,我从git中阅读了两个不同的Maven项目中的父

  • 在我的父pom xml文件中,我声明了一些在大多数孩子的pom中使用的依赖项。 然而,在一个子pom中,我不使用这些依赖项,我想找到一种方法来排除它们(为了不被视为未使用的依赖项)。 现在,在子pom的依赖部分,我什么都没有!有什么想法吗?

  • 我在父pom中有一个版本为2.3的Jackson,在我的子pom中我需要版本2.9,是否有任何方法排除父pom依赖?

  • 我有一个maven工件(),它依赖于某个工件()。 有一个父pom,其中包含某些其他依赖项。 是否可以使用这些依赖项,而无需在其自身pom的依赖项部分中明确添加它们?

  • 我试图理解从根pom文件到中级pom文件的继承指令。说明书上写着: 定义父pom文件继承和groupid、artifactid(但不是版本),从中间级父级到根pom文件。

  • 我有一个带有子pom B、C和D的父pom A,在B、C和D中,我有相同类型的依赖项。 ie父Pom 儿童Pom 到目前为止,我已经尝试过编辑插件surefire,当试图排除子pom的测试范围时,它不起作用。 我的问题是,是否有一种方法可以将概要文件添加到父pom中,以排除所有子pom的类型测试jar的所有依赖项。谢谢