无法访问 Maven 嵌套依赖项



我有一个maven项目,在父POM中,我已经在这个父POM中声明了一些依赖项。此 POM 在子 POM 中使用,如代码片段所示。编译子项目时,IntelliJ 抱怨它找不到父项目中使用的接口。此接口在父 POM 的依赖项"otherApp"中声明。

我对 Maven 依赖项的理解是,由于我已经在父 POM 中声明了依赖项,因此它也应该在子 POM 中继承,并且子应该能够毫无问题地访问这些依赖项及其类/接口。

我尝试在父 POM 的依赖项管理标签中添加依赖项。但无法实现我期望得到的。我还研究了父项目和子项目的依赖树。在父级中,依赖关系树将另一个应用显示为其依赖项,但在子项中,只有父项显示为依赖项,另一个应用不显示为通过父级的依赖项。

这就是我的父 POM 的样子

<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>it.app</groupId>
<artifactId>commonParent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../CommonParent/pom.xml</relativePath>
</parent>
<groupId>com.app</groupId>
<artifactId>parent</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.myapp</groupId>
<artifactId>anotherApp</artifactId>
<version>2.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

这就是我的孩子POM的样子

<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>it.app</groupId>
<artifactId>commonParent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../CommonParent/pom.xml</relativePath>
</parent>
<groupId>com.app</groupId>
<artifactId>child</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.app</groupId>
<artifactId>parent</artifactId>
</dependency>
</dependencies>
</project>

在"otherApp"中声明了一个接口Foo。父项目可以访问此接口。但子项目无法访问此接口。IntelliJ 抱怨它无法访问此接口。

需要更正的两件事:

  • 父 POM 应具有<packaging>pom</packaging>
  • 子项目不应使用父 POM 作为依赖项,而应将其添加到<parent>标记中。

最新更新