Shush
Shush

Reputation: 77

Maven cannot resolve dependency because parent jar not found

I am starting a Maven project with inherited poms.

The parent pom of the project is like:

<artifactId>service-parent</artifactId>
<groupId>group.parent</groupId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child-of-parent</module> ...
</modules>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    ...
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
        ...
    </dependencies>
</dependencyManagement>

So in the child module, if I understand well, the dependencies in part will be inherited automatically, and those in the part will have to be declared in child's pom. And I write this in child's pom:

<parent>
    <artifactId>service-parent</artifactId>
    <groupId>group.parent</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>service-child</artifactId>
<groupId>group.child</groupId>
<packaging>pom</packaging>

<modules>
    <module>child-of-child</module>
</modules>

<dependencies>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>
</dependencies>

This child module has its own children modules. The third-level pom is thus as blow:

<parent>
    <artifactId>service-child</artifactId>
    <groupId>group.child</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

<artifactId>service-child-child</artifactId>

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>service-child</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

It's a little wired like this as in the beginning of pom it already has tag which is the same thing as its dependency. That is because if I copy the dependencies in its parent there is a duplicate warning. And when I run maven clean install, it says

Failed to execute goal on project service-child: Could not resolve dependencies for project service-parent:service-child:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: group.child:service-child:jar:1.0-SNAPSHOT, group.parent:service-parent:jar:1.0-SNAPSHOT: Could not find artifact group.child:service-child:jar:1.0-SNAPSHOT

I know that it's an aggregation way instead of inheritance but I prefer this way in the future I would like to test and run module by module (I tried btw the inheritance way by removing the modules declaration in parent poms and it did work). Do you have any idea why this happen?

Upvotes: 0

Views: 3146

Answers (1)

Illya Kysil
Illya Kysil

Reputation: 1746

That's because the type (packaging) of service-child is pom and not jar.

It is not possible to have a project with jar packaging with submodules.

If you still need to declare that dependency, use the following declaration - note type element with pom value:

<dependencies>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>service-child</artifactId>
        <version>${project.version}</version>
        <type>pom</type>
    </dependency>
</dependencies>

That declaration does not bring any new dependencies since service-child-child inherits all dependencies from it's parent service-child already.

Check out Introduction to the POM - Project Inheritance for more information on inheritance and aggregation in Maven.

Upvotes: 1

Related Questions