这里给大家看个例子:
<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> <groupId>com.github.zhuangjiaju</groupId> <artifactId>easytools</artifactId> <version>${revision}</version> <relativePath>../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>easytools-tools</artifactId> <packaging>pom</packaging> <version>1.0.0-SNAPSHOT</version> <modules> <module>easytools-tools-base</module> <module>easytools-tools-common</module> </modules></project><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> <groupId>com.github.zhuangjiaju</groupId> <artifactId>easytools</artifactId> <version>${revision}</version> <relativePath>../pom.xml</relativePath></parent><modelVersion>4.0.0</modelVersion><artifactId>easytools-domain</artifactId><packaging>pom</packaging><version>1.0.0-SNAPSHOT</version><modules> <module>easytools-domain-course</module> <module>easytools-domain-demo</module></modules></project>
可以看到每个model都都配置了<version>1.0.0-SNAPSHOT</version> ,升级版本的的时候一个改过去非常麻烦。有没有上面办法能配置到一个地方呢?
最佳实践思路就是使用 flatten-maven-plugin 插件,这个插件可以配置配置一个变量,然后在 maven 的 process-resources 阶段生成一个临时是.flattened-pom.xml,这个文件已经将所有的版本改好了,然后maven真正处理的pom 用的就是.flattened-pom.xml ,这样就可以在一个地方配置版本,所有pom共享了。

案例地址:easytools/pom.xml at main · zhuangjiaju/easytools · GitHub
配置信息在父pom.xml 加入如下配置

<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.github.zhuangjiaju</groupId> <artifactId>easytools</artifactId> <packaging>pom</packaging> <!-- 配置当前项目的版本使用${revision}变量来替换 --> <version>${revision}</version> <name>easytools</name> <properties> <!-- 当前项目的版本 用了flatten 仅修改这一个地方即可--> <revision>1.0.0-SNAPSHOT</revision> ..... </properties> <modules> ..... </modules> <dependencyManagement> <dependencies> <!-- 在maven管理包的时候 也可以使用${revision}来定义版本 --> <dependency> <groupId>com.github.zhuangjiaju</groupId> <artifactId>easytools-tools-common</artifactId> <version>${revision}</version> </dependency> ..... </dependencies> </dependencyManagement> <build> <plugins> <!-- 支持maven revision 来配置统版本--> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>1.6.0</version> <configuration> <updatePomFile>true</updatePomFile> <flattenMode>oss</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></project>
子pom.xml配置如下:
<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> <groupId>com.github.zhuangjiaju</groupId> <artifactId>easytools</artifactId> <version>${revision}</version> <relativePath>../pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>easytools-tools</artifactId> <packaging>pom</packaging> <modules> <module>easytools-tools-base</module> <module>easytools-tools-common</module> </modules></project>
自动生成的.flattened-pom.xml 如下,已经给我们加好版本了。
<?xml version="1.0" encoding="UTF-8"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.github.zhuangjiaju</groupId> <artifactId>easytools-tools</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging></project>
总结
通过以上案例,我们就只要修改 revision 一个值,全局的版本都会变了,是不是很方便呀?可以到自己的项目里面实战时试试吧。
写在最后给大家推荐一个非常完整的Java项目搭建的最佳实践,也是本文的源码出处,由大厂程序员&EasyExcel作者维护,地址:GitHub - zhuangjiaju/easytools: java卓越工程实战, 让你的代码变得更加优雅。