Maven全局plugin配置传递子pom

前言

我们现在有一个多模块的 maven 项目,项目中模块一般会有一个顶级的 parent ,我们想通过 parent 中配置全局的 version,但一般在 <parent> 标签中配置 ${} 变量是会报错的 。

所以这里引入一个 flatten-maven-plugin

比如我们的模块结构为

1
2
gt-boot-parent
└── gt-base

pom.xml 文件配置

gt-boot-parentpom.xml 配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.gt.boot</groupId>
<artifactId>gt-boot-parent</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>
<modules>
<module>gt-base</module>
</modules>
<properties>
<revision>1.0.0-SNAPSHOT</revision>
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${flatten-maven-plugin.version}</version>
<configuration>
<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>
</project>

gt-basepom.xml 配置

1
2
3
4
5
6
7
8
9
10
11
12

<project>
<parent>
<artifactId>gt-boot-parent</artifactId>
<groupId>com.gt.boot</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gt-base</artifactId>
<version>${revision}</version>
</project>

flatten 插件带来的好处

  • 只需要修改 parentrevision 即可完成整个多个项目的版本修改.
  • 如果我们项目有 Nexus,那么我们依赖中的 ${},都会自动改为实际的版本号,并且 dependence.scope 未填写时,自动填写默认的scope compile
  • 如果我们使用 Intellij IDEA,那我们还会享有 view -> tool windows -> maven 窗口的树形结构展示。
  • 构建特定元素被删除
  • 默认情况下会删除开发特定的元素
  • 它仅包含您的工件用户所需的元素

这里就没有考虑 mvn versions:set -DnewVersion= 命令了,当前命令也可以完成一键修改版本号
官方介绍
官方案例

配置案例

官方案例

项目中查看预览效果可以直接对比查询 pom.xml 与同目录下的 ..flattened-pom.xml

Flatten可选配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>flatten-maven-plugin</artifactId>
<version>${flatten-maven-plugin.version}</version>
<configuration>
<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>

configuration

  • flattenMode 不做详细解释,一般使用 resolveCiFriendliesOnly,它可以:

    1. 自动从parent获取并插入子pom

      • BuildCiManagementContributorsDependenciesDependencyManagementDescription
      • DevelopersDistributionManagement
      • InceptionYearIssueManagementMailingLists
      • ModulesNameOrganizationPluginManagementPluginRepositoriesPrerequisites
      • ProfilesPropertiesReportingRepositoriesScmUrl
    2. 会自动处理子 pomParentVersion

    3. 其它选择

    4. | | | |
      | ————- | ————————- | ———————————————————— |
      | flattenMode | minimum | 弃用 |
      | flattenMode | oss | 保留 repositoriespluginRepositories 外的所有pom 元素 |
      | flattenMode | ossrh | Keeps all {@link FlattenDescriptor optional POM elements} that are required for OSS Repository-Hosting. |
      | flattenMode | bom | 像 ossrh,会额外保留 dependencyManagementProperties |
      | flattenMode | defaults | 删除所有 pom 节点,除了 repositories |
      | flattenMode | clean | 删除所有 pom 节点 |
      | flattenMode | fatjar | 删除所有 pom 节点 和 dependencies |
      | flattenMode | resolveCiFriendliesOnly | 仅解析 revisionsha1changelist, |

  • updatePomFile 一般设置为 true

    • true or false

    •  /**
           * The flag to indicate if the generated flattened POM shall be set as POM file to the current project. By default
           * this is only done for projects with packaging other than <code>pom</code>. You may want to also do this for
           * <code>pom</code> packages projects by setting this parameter to <code>true</code> or you can use
           * <code>false</code> in order to only generate the flattened POM but never set it as POM file. If
           * <code>flattenMode</code> is set to bom the default value will be <code>true</code>.
           */
      
      1
      2
      3
      4
      5
      6
      7
      8
      9

      - `pomElements`元素的处理

      - ```xml
      <pomElement>
      <!-- 以 developers 元素举例-->
      <!--ElementHandling:flatten,expand,resolve,interpolate,keep,remove -->
      <developers>remove</developers>
      </pomElement>
    • flatten 平铺,会删除所有 developers

    • expandpom.xml 继承第一个 parentdevelopers

    • resolvepom.xml 继承第一个 parentdevelopers

    • interpolate 插入

    • keeppom.xml 不会继承 parentdevelopers

    • remove 删除所有 developers

  • 下面一般不配置

  • embedBuildProfileDependencies

  • flattenDependencyMode

  • flattenedPomFilename

  • localRepository

  • mojoExecution

  • outputDirectory

  • project

  • session

本文地址: https://github.com/maxzhao-it/blog/post/c314b6f9/