maven使用${revision}管理多模块版本

在maven多模块项目中,可配合插件flatten-maven-plugin及${revision}属性进行全局版本号管理。

注: 示例项目源码可参见:https://gitee.com/luoex/maven-package-demo

flatten-maven-plugin插件示例:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>flatten-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <!-- 避免IDE将 .flattened-pom.xml 自动识别为功能模块 -->
                <flattenedPomFilename>pom-xml-flattened</flattenedPomFilename>
                <updatePomFile>true</updatePomFile>
                <flattenMode>resolveCiFriendliesOnly</flattenMode>
            </configuration>
            <executions>
                <execution>
                    <id>flatten</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>flatten</goal>
                    </goals>
                </execution>
                <execution>
                    <id>flatten.clean</id>
                    <phase>clean</phase>
                    <goals>
                        <goal>clean</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

父pom示例:

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.luo.demo</groupId>
    <artifactId>maven-package-demo</artifactId>
    <packaging>pom</packaging>
    <version>${revision}</version>
    <modules>
        <module>module-1</module>
        <module>module-2</module>
    </modules>

    <name>maven-package-demo</name>

    <properties>
        <revision>1.0.0-SNAPSHOT</revision>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--
            推荐使用${project.version}管理子模块依赖版本(兼容不使用${revision}的模式),
            亦可直接使用${revision}。
            -->
            <dependency>
                <groupId>com.luo.demo</groupId>
                <artifactId>module-1-1</artifactId>
                <version>${project.version}</version>
            </dependency>
            <dependency>
                <groupId>com.luo.demo</groupId>
                <artifactId>module-1-2</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                ...
            </plugin>        
        </plugins>
       
    </build>
</project>

子模块pom示例;

<?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">
    <parent>
        <artifactId>maven-package-demo</artifactId>
        <groupId>com.luo.demo</groupId>
        <version>${revision}</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>module-1</artifactId>

</project>

注:
父子模块需遵循父子目录层次,

关于子模块中parent.relativePath使用:

  • 默认值,不设置relativePath即为默认值,等价于<relativePath>../pom.xml</relativePath>,即遵循父子目录层次
    • 即优先查找上层目录../pom.xml
    • 然后查找本地仓库
    • 最后查找远程仓库
    • 推荐自定义的开发项目遵循此种方式(即父子模块需遵循父子目录层次,且保持parent.relativePath的默认值)
  • 空值<relativePath/>,即跳过本地文件目录查找
    • 直接查找本地仓库
    • 最后查找远程仓库
    • 适用于父依赖为第三方公有仓库中的依赖,如spring-boot-starter-parent
  • 其他值,可根据目录层次自行定义(推荐使用相对目录层次),如<relativePath>../module-1/pom.xml</relativePath>

不可混合使用${revision}和明确字符串版本号

若出现父子模块版本号混合使用${revision}和明确字符串形式如1.0.0.-SNAPSHOT,在mvn package会出现类似如下错误:

[FATAL] Non-resolvable parent POM for com.luo.demo:module-1:[unknown-version]:
Could not find artifact com.luo.demo:maven-package-demo:pom:1.0.0-SNAPSHOT and ‘parent.relativePath’ points at wrong local POM @ line 5, column 13

若使用插件flatten-maven-plugin及${revision},切记要全局(父pom及所有子pom)都使用${revision}来管理版本号(即<version>${revision}</version>


参考:
https://www.baeldung.com/maven-relativepath

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

罗小爬EX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值