0

I have a maven project and in the parent POM and I have declared some dependencies inside this parent POM. This POM is used within the child POM as shown in the code snippet. When compiling the Child project, IntelliJ complains that it cannot find an interface used within the parent project.This interface is declared within the dependency "anotherApp" in the parent POM.

My understanding about Maven dependencies is that, since I have declared the dependency in Parent POM, it should get inherited in the child POM as well and child should be able to access these dependencies and their classes / interfaces without an issue.

I tried adding the dependency inside dependencyManagement tag in parent POM. But cannot achieve what I expect to get. I also looked into the dependency trees of both parent and child projects. In parent, dependency tree shows anotherApp as its dependency, but in child, only parent is shown as a dependency, anotherApp is not shown as a dependency came through parent.

This is how my parent POM looks like

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

This is how my child POM looks like

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

There is an interface Foo declared inside "anotherApp". This interface is accessible by the parent project. But child project cannot access this interface. IntelliJ complains that it cannot access this interface.

4

1 回答 1

3

Two things that need to be corrected:

  • The parent POM should have <packaging>pom</packaging>.
  • The child project should not use the parent POM as dependency, but add it in the <parent> tag.
于 2019-10-14T17:30:28.213 回答