maven nested modules

following up with https://lwpro2.dev/2021/12/14/microservies-in-monorepo/, there are times it needs a set up of nested modules.

where for example, module A could contain module A1, module A2 for example.

the needed changes/set up for nested maven module is similar to the multi module set up. however, now module A needs to be with

<packaging>pom</packaging>

now.

similarly, the submodule should be included in module A.

<parent>
    <groupId>org.xxx</groupId>
    <artifactId>ParentModule</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>ModuelA</artifactId>
<packaging>pom</packaging>

<modules>
    <module>ACore</module>
    <module>A1</module>
    <module>A2</module>
</modules>

among the submodules, if A1 needs to use the code from ACore, the dependency scope should be compile, so that it would build A1 shaded with the classes from ACore.

<artifactId>A1</artifactId>

<properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.xxx</groupId>
        <artifactId>ACore</artifactId>
        <version>1.0-SNAPSHOT</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

this will work out within IDE. as for build or packaging, the command to build the nested jars are

mvn clean package -pl ModuleA-am -amd

-am will include upstream dependency for example module Core

where -amd will include the nested modules, for example ACore, A1 and A2.

One thought on “maven nested modules

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s