【发布时间】:2020-02-11 08:30:28
【问题描述】:
我有一个 maven 项目,在父 POM 中,我在这个父 POM 中声明了一些依赖项。这个 POM 在子 POM 中使用,如代码 sn-p 所示。在编译 Child 项目时,IntelliJ 抱怨找不到父项目中使用的接口。该接口在父 POM 的依赖项“anotherApp”中声明。
我对 Maven 依赖项的理解是,由于我在 Parent POM 中声明了依赖项,它也应该在子 POM 中继承,并且子应该能够毫无问题地访问这些依赖项及其类/接口。
我尝试在父 POM 中的 dependencyManagement 标记中添加依赖项。但无法达到我期望得到的。我还查看了父项目和子项目的依赖关系树。在 parent 中,依赖树显示 anotherApp 作为其依赖项,但在 child 中,仅显示 parent 作为依赖项,anotherApp 不显示为依赖项来自 parent。
这就是我的父 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>
在“anotherApp”中声明了一个接口 Foo。父项目可以访问此接口。但子项目无法访问该接口。 IntelliJ 抱怨它无法访问这个接口。
【问题讨论】:
标签: java maven intellij-idea dependencies