Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: POM modules and submodules hierarchy

My project is structured like this:

.
 |--module
 |  `-- pom.xml
 |  --submodule
 |    `-- pom.xml
 `-- pom.xml

The POM's (simplified):

  • Project:
<project>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>project</artifactId>
    <name>Project</name>
    <groupId>org.myorg</groupId>
    <version>1.0.6-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>module</module>     
    </modules>
    (...)
</project>
  • Module:
<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.myorg</groupId>
        <artifactId>project</artifactId>
        <version>1.0.6-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>module</artifactId>
    <name>Module</name>
    <groupId>org.myorg</groupId>
    <version>1.0.6-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>submodule</module>      
    </modules>
    (...)
</project>
  • Submodule:
<project>
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.myorg</groupId>
        <artifactId>module</artifactId>
        <version>1.0.6-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>submodule</artifactId>
    <name>Submodule</name>
    <groupId>org.myorg</groupId>
    <version>1.0.6-SNAPSHOT</version>
    <packaging>jar</packaging>
    (...)
</project>

When run maven install in POM's project or module the project is built sucessfully. But, when run in submodule occours this error:

Failed to execute goal on project submodule: Could not find artifact org.myorg:project:pom:1.0.6-SNAPSHOT

Why my submodule not find the POM project? The relative path is specified.

like image 357
Rodolfo Barbeiro Avatar asked Dec 27 '22 09:12

Rodolfo Barbeiro


1 Answers

The first thing which i noticed is that every sub-module which has a parent contains the line:

<relativePath>../pom.xml</relativePath>

which is useless, cause it's default in maven or in other word just remove it.

Furthermore in a multimodule build you shouldn't define the version. In case if the groupId is always the same you can omit the groupId as well, cause the current module inherits the version from it's parent.

module: pom.xml

<project>
    <parent>...
    </parent>    
    <artifactId>module</artifactId>
    <packaging>pom</packaging>

    <name>Module</name>

    <modules>
        <module>submodule</module>      
    </modules>
    (...)
</project>

Apart from that you can't go into a sub-module and call

mvn install

If you like to install a separate module of a multi-module build you should use a thing like this:

mvn -amd -pl submodule install

which will do what you like to do, but usually you should install a full mulit-module build unless you exactly know what you are doing. The options -amd is an abbrevation for --also-make-dependents. The -pl is an abbreviation for --projects to define a list of project which should be made during the call.

like image 51
khmarbaise Avatar answered Jan 10 '23 23:01

khmarbaise