flatten-maven-plugin uses

Article directory

  • 1. Introduction
    • 1.1 Function
    • 1.2 goal introduction
  • 2. Use summary

1. Introduction

1.1 Function

The parent-child pom version of the pom project is proposed as a variable definition in properties.

In this way, only modifying the value of the variable (as specified when running the mvn command) can realize the overall switching of the version

1.2 Goal Introduction

  • flatten:clean

Delete the .flattened-pom.xml generated by the flatten plugin

The configuration parameters are:

  1. flattenedPomFilename: The name of the pom generated by the plugin, the default is .flattened-pom.xml

  2. outputDirectory: The directory where the plugin generates the pom, the default is ${project.basedir}

  • flatten: flatten

Generate .flattened-pom.xml in resources-process, and replace the original pom.xml when install/deploy

The main configuration parameters are:

  1. flattenedPomFilename: The name of the pom generated by the plugin, the default is .flattened-pom.xml

  2. outputDirectory: The directory where the plugin generates the pom, the default is ${project.basedir}

  3. updatePomFile: The module of packing=pom also performs reversion variable replacement, the default is false

  4. flattenMode: used to define the elements contained in the generated .flattened-pom.xml, common values are:

  • oss: Commonly used in open source software, except for repositories/pluginRepositories, all elements defined by FlattenDescriptor are generated

  • ossrh: All elements defined by FlattenDescriptor are generated

  • bom: add dependencyManagement and properties on the basis of ossrh

  • defaults: Except for repositories, all elements defined by FlattenDescriptor are not generated

  • clean: All elements defined by FlattenDescriptor are not generated

  • fatjar: All elements and dependencies defined by FlattenDescriptor are not generated

  • resolveCiFriendliesOnly: Only replace the revision, sha1 and changelist in the original pom, and keep the others as they are

Commonly used oss/ossrh/resolveCiFriendliesOnly

  • The pom.xml elements defined by FlattenDescriptor are:

modelVersion
groupId
artifactId
version
packaging
licenses
dependencies
profiles
name
description
url
inceptionYear
organization
scm
developers
contributors
mailingLists
pluginRepositories
issueManagement
ci Management
distribution Management
prerequisites
repositories
parents
build
dependency management
properties
modules
reporting

2. Usage summary

  • Without flatten-maven-plugin

1. The parent pom defines the version as the variable reversion and uses it as the version, and the child pom re-references the variable reversion as the version

2. As a result, the compile/test can be run normally, but the version or reversion variable in the parent-child pom is not replaced when installing or deploying

3. Others cannot quote your package without version

parent-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">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>
    <version>${reversion1}</version>
    <modules>
        <module>no-flatten-child</module>
    </modules>
    <groupId>com.wsl.my.maven</groupId>
    <artifactId>no-flatten-plugin</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <reversion1>1.1.0-SNAPSHOT</reversion1>
    </properties>
</project>

child pom.xml

<?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">
    <parent>
        <artifactId>no-flatten-plugin</artifactId>
        <groupId>com.wsl.my.maven</groupId>
        <version>${reversion1}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>no-flatten-child</artifactId>
</project>

${reversion1} in parent-child pom.xml is not replaced after install/deploy

  • Using the flatten-maven-plugin

1. The parent pom defines the version as the variable reversion and uses it as the version, and the child pom re-references the variable reversion as the version

2. Use flatten-maven-plugin and set updatePomFile=true, and bind goal to maven cycle

3. During the process-resources stage, .flattened-pom.xml will be generated in the parent-child project directory (version has been replaced with a specific value)

4. When running install or deploy, .flattened-pom.xml will replace the original pom.xml

original parent pom

<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>
    <version>${reversion2}</version>
    <packaging>pom</packaging>

    <artifactId>use-flatten-parent</artifactId>
<groupId>com.wsl.my.maven</groupId>
    <modules>
        <module>use-flatten-child</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <reversion2>1.2.0-SNAPSHOT</reversion2>
    </properties>

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