maven · 2024-01-31 0

flatten-maven-plugin 使用

前言

对于复杂的多个模块,要想通过变量统一版本号,可使用 flatten-maven-plugin。flatten-maven-plugin 插件需要的 maven 版本,要 3.5 以上。

flatten-maven-plugin 作用,用于生成精简的 pom 文件。然后在 install 和 deploy 阶段使用精简后的 pom.xml文件,替换原来的 pom.xm l文件:

1) 变量会被解析

下面演示未使用和使用 flatten-maven-plugin 对于发布的差别

一、未使用 flatten-maven-plugin

1.pom 文件

有模块 parent、child1、child2,child2 依赖 child1

parent 的 pom 文件:

<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>

<properties>
    <revision>1.0-SNAPSHOT</revision>
</properties>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>child1</artifactId>
            <version>${revision}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

child1 的 pom 文件:

<parent>
    <groupId>org.example</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>
    <relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.example</groupId>
<artifactId>child1</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>

child2 的 pom 文件:

<parent>
    <groupId>org.example</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>
    <relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.example</groupId>
<artifactId>child2</artifactId>
<version>${revision}</version>
<packaging>jar</packaging>

<dependencies>
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>child1</artifactId>
    </dependency>
</dependencies>

2.install

执行 mvn install,安装到本地仓库

mvn install

在本地仓库,查看生成 pom 文件

parent 的 pom 文件:

zxm@zxm-pc:~/.m2/repository/org/example/parent/1.0-SNAPSHOT$ cat parent-1.0-SNAPSHOT.pom 
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>parent</artifactId>
    <version>${revision}</version>
    <packaging>pom</packaging>

    <properties>
        <revision>1.0-SNAPSHOT</revision>
    </properties>

    <modules>
        <module>child1</module>
        <module>child2</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.example</groupId>
                <artifactId>child1</artifactId>
                <version>${revision}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

child2 的 pom 文件:

zxm@zxm-pc:~/.m2/repository/org/example/child2/1.0-SNAPSHOT$ cat child2-1.0-SNAPSHOT.pom 
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.example</groupId>
        <artifactId>parent</artifactId>
        <version>${revision}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>child2</artifactId>
    <version>${revision}</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>child1</artifactId>
        </dependency>
    </dependencies>

</project>

可以看出 child2 的 ${revision} 没有具体的值,这样导致使用者无法根据,gav 定位到这个 jar 包,也就是说,别的项目无法使用发布的这个 jar 包

二、使用 flatten-maven-plugin

1.pom 文件

在 parent 模块,配置 flatten-maven-plugin 插件

<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>${revision}</version>
<packaging>pom</packaging>

<properties>
    <revision>1.0-SNAPSHOT</revision>
</properties>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>child1</artifactId>
            <version>${revision}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>flatten-maven-plugin</artifactId>
            <version>1.5.0</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>

2.package

进行 packege 或 install 后,每个模块会生成 .flattened-pom.xml 文件,其中的 ${revision} 已经被替换成了具体的值

生成 parent 下 .flattened-pom.xml 文件:

<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
  <module>child1</module>
  <module>child2</module>
</modules>
<properties>
  <revision>1.0-SNAPSHOT</revision>
</properties>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.example</groupId>
      <artifactId>child1</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>
</dependencyManagement>
<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>flatten-maven-plugin</artifactId>
      <version>1.5.0</version>
      <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>
      <configuration>
        <updatePomFile>true</updatePomFile>
        <flattenMode>resolveCiFriendliesOnly</flattenMode>
      </configuration>
    </plugin>
  </plugins>
</build>

生成 child2 下 .flattened-pom.xml 文件:

<parent>
  <groupId>org.example</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
</parent>
<groupId>org.example</groupId>
<artifactId>child2</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
  <dependency>
    <groupId>org.example</groupId>
    <artifactId>child1</artifactId>
  </dependency>
</dependencies>

3.install

执行 mvn install,安装到本地仓库

mvn install

在本地仓库,查看生成 pom 文件

parent 的 pom 文件:

zxm@zxm-pc:~/.m2/repository/org/example/parent/1.0-SNAPSHOT$ cat parent-1.0-SNAPSHOT.pom 
<?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>org.example</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>child1</module>
    <module>child2</module>
  </modules>
  <properties>
    <revision>1.0-SNAPSHOT</revision>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.example</groupId>
        <artifactId>child1</artifactId>
        <version>1.0-SNAPSHOT</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>flatten-maven-plugin</artifactId>
        <version>1.5.0</version>
        <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>
        <configuration>
          <updatePomFile>true</updatePomFile>
          <flattenMode>resolveCiFriendliesOnly</flattenMode>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

child2 的 pom 文件:

zxm@zxm-pc:~/.m2/repository/org/example/child2/1.0-SNAPSHOT$ cat child2-1.0-SNAPSHOT.pom 
<?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>
  <parent>
    <groupId>org.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>
  <groupId>org.example</groupId>
  <artifactId>child2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.example</groupId>
      <artifactId>child1</artifactId>
    </dependency>
  </dependencies>
</project>

可以看出 child2 的 version 有具体的值

flatten-maven-plugin 的实现原理,其实就是在模块下生成 .flattened-pom.xml 里面是变量替换为具体版本的 pom 文件,在编译构建的时候,maven 不读 pom.xml 而去读取 .flattened-pom.xml 的内容完成构建。