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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章