To avoid editing 100 project POM files individually and achieve a centralized configuration, you can use a parent POM with dependency management and have all projects inherit from it. This way, you can control dependencies and profiles globally from one place.
Solution: Centralized Dependency Management with Parent POM
Step 1: Create a Parent POM The parent POM acts as a base for all your projects. Define profiles for different versions of dependencies in this parent POM.
Parent POM Example (parent-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-pom</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<profiles>
<!-- Profile for dependency version 1.0.0 -->
<profile>
<id>version1</id>
<activation>
<activeByDefault>true</activeByDefault> <!-- Default profile -->
</activation>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-library</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
<!-- Profile for dependency version 2.0.0 -->
<profile>
<id>version2</id>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-library</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
</profiles>
</project>
Step 2: Update Child Projects to Inherit the Parent POM Each child project only needs to reference the parent POM.
Child Project POM (child-project/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>
<parent>
<groupId>com.example</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>child-project</artifactId>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-library</artifactId>
</dependency>
</dependencies>
</project>
- The version of
example-library
is not specified in the child POM; it inherits the version from the parent POM's active profile.
Step 3: Build Projects with Specific Profiles
You can now switch dependency versions for all projects by activating a profile in the parent POM.
- Use Version 1.0.0:
mvn clean install -Pversion1
- Use Version 2.0.0:
mvn clean install -Pversion2
This command will apply the selected profile to all child projects without modifying their individual POM files.
Advantages of This Approach:
- Centralized Configuration: All projects inherit the same dependency versions from the parent POM.
- Scalable: Any changes to dependencies or profiles only need to be made in the parent POM.
- Consistency: Ensures that all projects use the same versions of dependencies unless explicitly overridden.
- Flexible Switching: You can easily switch between dependency versions using profiles.