Maven: POM modules and submodule hierarchy

My project is structured like this:

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

      

POM (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 launched maven install

in a project or POM, the project runs successfully. But when run in a submodule, this error:

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

Why can't my submodule find the POM project? The relative path is specified.

+3


source to share


2 answers


The first thing I noticed is that every submodule that has a parent contains the line:

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

      

which is useless, called by default in maven or in some other word, just remove it.

Also, in a multimode build, you don't have to define a version. If the groupId is always the same, you can also omit the groupId, since the current module inherits the version from the parent.

module: pom.xml

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

    <name>Module</name>

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

      



Also, you cannot enter the submodule and call

mvn install

      

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

mvn -amd -pl submodule install

      

which will do what you like, but usually you need to install the full mulit-module build if you don't know exactly what you are doing. The -amd options are an abbreviation for -also-make-dependents. -pl is an abbreviation for -projects to define the list of projects to be done at call time.

+4


source


First you need to run mvn install

in the root project, it will create the artifact in your local maven repository. The second time in the wards, you can only run the add-on module. If you do not run on the root project, maven will not generate any artifact for your project, so when you run on a helper module it cannot find the artifact from the maven repo.



+1


source







All Articles