Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven cannot build child module from child directory

Tags:

java

maven

I have a maven project with a parent pom and child modules, structured like this:

root/pom.xml
root/parent/pom.xml
root/child1/pom.xml
root/child2/pom.xml
root/child2/child21/pom.xml

Every child pom and the root pom declare root/parent as their parent. child21 has child1 as a dependency.

When I try to execute mvn install from the child1 directory, maven successfully builds and installs the child1 jar. But when i execute mvn install from either the child2 or child21 directories, maven errors out with the following:

[ERROR] Failed to execute goal on project child21: Could not resolve dependencies for project groupId:child21:jar:0.0.1-SNAPSHOT: Failed to collect dependencies at groupId:child1:jar:0.0.1-SNAPSHOT: Failed to read artifact descriptor for groupId:child1:jar:0.0.1-SNAPSHOT: Could not find artifact groupId:parent:pom:0.0.1-SNAPSHOT in jme-repo (http://updates.jmonkeyengine.org/maven/) -> [Help 1]

I have tried googling for what might be wrong with my project structure with no luck. Can anyone help suggest ways to get child21 to build?


My poms contain:

Root

<parent>
    <groupId>groupId</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>./parent/pom.xml</relativePath>
</parent>
<artifactId>root</artifactId>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

Parent

<groupId>groupId</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>groupId</groupId>
            <artifactId>child1</artifactId>
            <version>${project.version}</version>
        </dependency>
        ...
    </dependencies>
<dependencyManagement>

<repositories>
    <repository>
        <id>jme-repo</id>
        <name>JME3 Official Maven Repo</name>
        <url>http://updates.jmonkeyengine.org/maven/</url>
    </repository>
</repositories>

<build>
    <plugins>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
            <source>${java.source.version}</source>
            <target>${java.target.version}</target>
        </configuration>
    </plugins>
</build>

Child 1

<parent>
    <groupId>groupId</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../parent/pom.xml</relativePath>
</parent>

<artifactId>child1</artifactId>
<packaging>jar</packaging>

<dependencies>...</dependencies>

Child 2

<parent>
    <groupId>groupId</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>child2</artifactId>
<packaging>pom</packaging>

<modules>
    <module>child21</module>
</modules>

Child 21

<parent>
    <groupId>groupId</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>../../parent/pom.xml</relativePath>
</parent>
<artifactId>child21</artifactId>
<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>groupId</groupId>
        <artifactId>child1</artifactId>
    </dependency>
</dependencies>

UPDATE WITH SOLUTION Based on the accepted answer below, I added the parent project as a module of the root pom:

<parent>
    <groupId>groupId</groupId>
    <artifactId>parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <relativePath>./parent/pom.xml</relativePath>
</parent>
<artifactId>root</artifactId>
<packaging>pom</packaging>

<modules>
    <module>parent</module>
    <module>child1</module>
    <module>child2</module>
</modules>

Then when developers build the project for the first time, the parent pom is installed. Subsequently they will be able to build the child2 and child21 projects independently.

like image 376
Jpnh Avatar asked Oct 18 '25 10:10

Jpnh


1 Answers

This is a pretty awkward module structure, but it can still work. The problem is that when you are building module child21 (or child2), the child1 dependency is not a part of the current build, but is looked up in the repository. So is the parent of that dependency, which is the parent module. However, I assume, parent was never installed there - if you run mvn clean install on root, it does not build the parent, which is only referenced in the child modules by relative paths.

To fix it you must first install the parent POM into the repository. Run mvn install in the parent module. After that, build child1 - it will be installed into the repository as well. Now everything what is needed for building module child21 is available in the repository.

like image 200
Adam Michalik Avatar answered Oct 20 '25 23:10

Adam Michalik