Maven多模块项目中,如何在反应堆POM与父POM间共享插件版本?
统一管理Maven多模块项目中插件版本的方案
针对你这种反应堆POM和父POM都需要配置同一插件、但用途不同,又想避免重复修改版本的场景,我给你几个靠谱的解决思路:
方案一:用父POM的<pluginManagement>统一管控版本(最推荐)
Maven的<pluginManagement>天生就是用来统一管理插件版本和默认配置的,完美适配你的场景:
- 在父POM里添加
<pluginManagement>节点,定义好插件的版本:
<build> <pluginManagement> <plugins> <plugin> <groupId>你的插件groupId</groupId> <artifactId>你的插件artifactId</artifactId> <version>1.2.3</version> <!-- 这里只写一次版本 --> <!-- 可以加一些所有地方通用的默认配置,如果有的话 --> </plugin> </plugins> </pluginManagement> <!-- 父POM自身的插件配置,直接引用pluginManagement里的版本 --> <plugins> <plugin> <groupId>你的插件groupId</groupId> <artifactId>你的插件artifactId</artifactId> <!-- 不用重复写版本! --> <configuration> <!-- 父POM专属的插件配置 --> </configuration> </plugin> </plugins> </build>
- 让反应堆POM继承父POM(如果还没继承的话),然后配置插件时同样不用写版本:
<parent> <groupId>你的项目groupId</groupId> <artifactId>父POM的artifactId</artifactId> <version>父POM版本</version> <relativePath>父POM的相对路径</relativePath> <!-- 比如如果父POM在parent子模块里,就是./parent/pom.xml --> </parent> <build> <plugins> <plugin> <groupId>你的插件groupId</groupId> <artifactId>你的插件artifactId</artifactId> <!-- 同样不用写版本,自动用pluginManagement里的 --> <configuration> <!-- 反应堆POM专属的插件配置 --> </configuration> </plugin> </plugins> </build>
这样以后要改版本,只需要修改父POM里<pluginManagement>中的版本号,反应堆POM和所有子模块都会自动同步,完全不用手动改多个文件。
方案二:用自定义属性统一版本
如果因为某些原因不能用<pluginManagement>,可以用Maven的自定义属性来管理版本:
- 在父POM的
<properties>里定义插件版本属性:
<properties> <my-plugin.version>1.2.3</my-plugin.version> <!-- 统一版本在这里 --> </properties>
- 父POM里配置插件时引用这个属性:
<build> <plugins> <plugin> <groupId>你的插件groupId</groupId> <artifactId>你的插件artifactId</artifactId> <version>${my-plugin.version}</version> <configuration> <!-- 父POM的配置 --> </configuration> </plugin> </plugins> </build>
- 反应堆POM继承父POM后,同样引用这个属性配置插件:
<build> <plugins> <plugin> <groupId>你的插件groupId</groupId> <artifactId>你的插件artifactId</artifactId> <version>${my-plugin.version}</version> <configuration> <!-- 反应堆POM的配置 --> </configuration> </plugin> </plugins> </build>
这个方案也能实现版本统一管理,改属性值就行,只是比<pluginManagement>少了统一配置的能力,适合只需要统一版本的场景。
方案三:用外部properties文件共享版本(适合复杂场景)
如果反应堆POM和父POM无法通过继承共享属性,可以用properties-maven-plugin加载外部的properties文件:
- 在项目根目录创建一个
version.properties文件,写入插件版本:
my-plugin.version=1.2.3
- 在父POM里配置插件加载这个文件:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0.0</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <!-- 注意路径,如果父POM在子目录,比如parent/,就写../version.properties --> <file>${project.basedir}/version.properties</file> </files> </configuration> </execution> </executions> </plugin> <plugin> <groupId>你的插件groupId</groupId> <artifactId>你的插件artifactId</artifactId> <version>${my-plugin.version}</version> <configuration> <!-- 父POM的配置 --> </configuration> </plugin> </plugins> </build>
- 在反应堆POM里同样配置这个插件加载同一个文件,然后引用版本属性:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0.0</version> <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> <file>${project.basedir}/version.properties</file> </files> </configuration> </execution> </executions> </plugin> <plugin> <groupId>你的插件groupId</groupId> <artifactId>你的插件artifactId</artifactId> <version>${my-plugin.version}</version> <configuration> <!-- 反应堆POM的配置 --> </configuration> </plugin> </plugins> </build>
这个方案适合反应堆POM和父POM没有继承关系的场景,不过需要额外配置插件,稍微麻烦一点。
内容的提问来源于stack exchange,提问作者Eddie
相关产品推荐
相关产品推荐

