多模块的项目,怎么方便管理 模块的版本号呢?
可以使用
${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.1000128-2
这就很奇怪,没找到问题的原因。
如果使用快照版本,还是写死吧。