In a Maven pom.xml
file, <modules>
is used to define the modules of a multi-module project. This is common in larger projects that are split into multiple sub-projects, each of which has its own pom.xml
. The parent pom.xml
(often in the root directory) lists these sub-projects under the <modules>
section.
Here is a basic example of how <modules>
can be used in a pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module-a</module>
<module>module-b</module>
</modules>
<dependencies>
<!-- Parent-level dependencies -->
</dependencies>
<build>
<plugins>
<!-- Parent-level plugins -->
</plugins>
</build>
</project>
Child pom.xml
for module-a
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module-a</artifactId>
<dependencies>
<!-- Module-specific dependencies -->
</dependencies>
</project>
Child pom.xml
for module-b
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>module-b</artifactId>
<dependencies>
<!-- Module-specific dependencies -->
</dependencies>
</project>
Key Points:
- Parent
<pom.xml>
: Contains the<modules>
section listing all sub-modules. - Child
<pom.xml>
: Each module has its ownpom.xml
file and refers to the parent project. - Hierarchy: The parent-child relationship helps manage dependencies, plugins, and build configurations centrally.
This setup allows you to manage complex projects more easily, sharing configurations and dependencies among multiple modules.