Maven版本统一管理

多模块的项目,怎么方便管理 模块的版本号呢?

可以使用 

${revision}

配合 flatten-maven-plugin插件

           <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>flatten-maven-plugin</artifactId>
                <version>1.1.0</version>
                <configuration>
                    <updatePomFile>true</updatePomFile>
                    <flattenMode>resolveCiFriendliesOnly</flattenMode>
                    <pomElements>
                        <build>remove</build>
                    </pomElements>
                </configuration>
                <executions>
                    <execution>
                        <id>flatten</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>flatten</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

父模块配置

<!-- 父模块 pom.xml -->
<groupId>com.example</groupId>
<artifactId>parent-project</artifactId>
<version>${revision}</version> <!-- 使用 revision 属性 -->
<packaging>pom</packaging>

<properties>
    <revision>1.0.0</revision> <!-- 定义 revision 属性 -->
</properties>

<modules>
    <module>child-module</module>
</modules>

子模块配置

<!-- 子模块 pom.xml -->
<parent>
    <groupId>com.example</groupId>
    <artifactId>parent-project</artifactId>
    <version>${revision}</version> <!-- 继承父模块的 revision -->
    <relativePath>../pom.xml</relativePath>
</parent>

<artifactId>child-module</artifactId>
<!-- 不写 <version>,自动继承父模块的 revision -->

打包之后,会生成一个 .flattened-pom.xml,这个文件 中 替换了 ${revision}的具体值,然后 打包到jar里。

如果 使用了快照版本,发现有一个问题。

因为快照版本打包后,会有一个时间戳,比如 生成的版本是 2.0-20250000.100127-2

但是,远程仓库中的 maven-metadata.xml中的版本号是 2.0-20250000.1000128-2

拉取快照版本是根据 maven-metadata.xml中的版本号来拉取的,所以 拉取不到2.0-20250000.1000128-2,因为仓库中存在的实际的版本号是2.0-20250000.1000127-2

这就很奇怪,没找到问题的原因。

如果使用快照版本,还是写死吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值