【问题标题】:Best approach for maven multi module projectMaven多模块项目的最佳方法
【发布时间】:2018-01-27 02:37:15
【问题描述】:

当我需要指定与父模块的子模块依赖关系时,我遇到了多模块 maven 项目的问题

这是我对 pom.xml 的配置

父 POM:

<groupId>com.parent</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
    <module>childA</module>
    <module>childB</module>
</modules>

childA POM

<parent>
    <groupId>com.parent</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>childA</artifactId>

   <dependencies>
    <dependency>
        <groupId>com.parent</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    </dependencies>

ChildB POM

<parent>
    <groupId>com.parent</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>childB</artifactId>

在 childB 中运行 mvn clean install 时,我能够构建 childB。

我在构建 Parent 和 ChildA 时遇到错误消息

ERROR] Failed to execute goal on project parent: Could not resolve dependencies for project com.parent:childA:jar:1.0-SNAPSHOT: Could not find artifact com.parent:parent:jar:1.0-SNAPSHOT -> [Help 1]

由于父包上有一些类,我需要在 childA pom.xml 中添加对父包的依赖项

我应该如何解决这个问题?

【问题讨论】:

  • 首先注释掉所有 module build only parent 然后取消注释并构建它会起作用。它通常会在你第一次做的时候发生。
  • 依赖你的父母是没有意义的。您的父级是 pom 而不是工件(jar,war,...),因此您不能将任何子模块中的依赖项放在父级
  • 这是这里唯一有效的答案!

标签: java maven


【解决方案1】:

childA pom 毫无意义。

它与依赖父项和构建依赖项具有相同的工件:

<parent>
    <groupId>com.parent</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

和:

<dependencies>
  <dependency>
    <groupId>com.parent</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>

首先:依赖关系解析永远不会成功,因为它是先有鸡还是先有蛋的问题。
生成 pom 的父模块仅在反应器构建终止时可用,但仅当 childA 中的所有模块终止其构建时才会终止。

第二:多模块maven项目和父模块被设计成pom而不是jar
将其声明为 childA 的依赖项的兴趣是什么?
只需将其删除并将其保留为 childA 的父级。

我需要在 childA pom.xml 中添加依赖项到父包,因为 父包中提供了一些类

您无需将其指定为依赖项即可实现它。
子项目在依赖项之间从父项目继承了许多东西。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-23
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 2011-01-04
    • 2017-10-20
    相关资源
    最近更新 更多