To avoid editing 100 project POM files individually and achieve a centralized configuration

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:

  1. Centralized Configuration: All projects inherit the same dependency versions from the parent POM.
  2. Scalable: Any changes to dependencies or profiles only need to be made in the parent POM.
  3. Consistency: Ensures that all projects use the same versions of dependencies unless explicitly overridden.
  4. Flexible Switching: You can easily switch between dependency versions using profiles.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值