Multi Module


https://maven.apache.org/guides/mini/guide-multiple-modules.html
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html

1. Important Points#

aggregator:
    root POM lists modules and controls reactor build order

parent:
    inherited POM that centralizes versions and plugin config

one POM can be both aggregator and parent.

2. Layout#

order-platform
├── pom.xml
├── order-api
│   └── pom.xml
├── order-service
│   └── pom.xml
└── order-client
    └── pom.xml

3. Root POM#

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>order-platform</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>order-api</module>
    <module>order-service</module>
    <module>order-client</module>
  </modules>

  <properties>
    <java.version>21</java.version>
  </properties>
</project>

4. Child POM#

<parent>
  <groupId>com.example</groupId>
  <artifactId>order-platform</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <relativePath>../pom.xml</relativePath>
</parent>

<artifactId>order-service</artifactId>

5. Reactor Commands#

./mvnw clean verify
./mvnw -pl order-api test
./mvnw -pl order-api -am test
./mvnw -pl order-api -amd test
-pl:
    selected projects

-am:
    also make required dependencies

-amd:
    also make dependents

6. Internal Dependency#

<dependency>
  <groupId>com.example</groupId>
  <artifactId>order-service</artifactId>
  <version>${project.version}</version>
</dependency>

7. Plugin Rule#

root parent:
    put versions in pluginManagement

application module:
    enable spring-boot-maven-plugin

library module:
    do not repackage as executable jar

8. Common Mistakes#

root POM package is not pom:
    modules will not work correctly

plugin runs in every module:
    check inherited flag and plugin placement

module cannot resolve sibling:
    build from root or use -am