Maven多模块项目版本统一管理

如图所示,项目中定义了这样几个模块:

  • pdd-workflow-build :定义项目版本,及全局配置
  • pdd-workflow-dependencies :外部依赖管理,统一管理所有用到的外部依赖的版本
  • pdd-workflow-service :项目service模块
  • pdd-workflow-web :项目web模块
  • pdd-parent :聚合模块

模块之间的继承依赖关系如下图所示:

网上都说用${revision}这样的占位符,而且必须叫“revision”这个名字。但是,我自己实践过后发现,这个变量叫什么都可以(比如:common.version),关键在于要有一个聚合模块将所有引用了${revision}的模块都聚合进来,不然打包的时候还是会找不到版本。

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
        <artifactId>pdd-workflow-build</artifactId>
        <version>${revision}</version>
        <relativePath>./pdd-workflow-build/pom.xml</relativePath>
    </parent>

    <artifactId>pdd-workflow-parent</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>pdd-workflow-build</module>
        <module>pdd-workflow-dependencies</module>
        <module>pdd-workflow-service</module>
        <module>pdd-workflow-web</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>pdd-workflow-dependencies</artifactId>
                <version>${project.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

众所周知,<parent>中的版本必须是常量,不能是变量,不然就找不到版本。但是,只要我们增加一个聚合模块就可以解决这个问题,在聚合模块中引用变量是可以的。所以,叫不叫revision根本无所谓,变量名而已,关键在于<modules>。

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
        <artifactId>pdd-workflow-build</artifactId>
        <version>${revision}</version>
        <relativePath>../pdd-workflow-build/pom.xml</relativePath>
    </parent>

    <artifactId>pdd-workflow-dependencies</artifactId>
    <packaging>pom</packaging>

    <properties>
        <dubbo.version>3.2.5</dubbo.version>
        <spring-boot.version>3.2.0</spring-boot.version>
        <druid.version>1.2.20</druid.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
        <artifactId>pdd-workflow-parent</artifactId>
        <version>${revision}</version>
    </parent>
    <artifactId>pdd-workflow-web</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>pdd-workflow-service</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

</project>

参考Seata,看看Seata是如何统一管理版本的

代码地址:https://gitee.com/chengjiansheng/pdd-workflow

 

posted @ 2023-12-06 15:50  废物大师兄  阅读(1659)  评论(0编辑  收藏  举报