flatten-maven-plugin使用教程

 更新时间:2023年07月13日 14:44:26   作者:2021不再有雨  
这篇文章主要介绍了flatten-maven-plugin使用,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

一、简介

1.1 作用

将pom工程父子pom的版本,提出作为变量定义在properties。

这样仅修改变量的值(如在运行mvn命令时指定) 即可实现版本整体切换。

1.2 goal介绍

  • flatten:clean

删除flatten插件生成的 .flattened-pom.xml

配置参数有:

flattenedPomFilename: 插件生成的pom的名字,默认为 .flattened-pom.xml

outputDirectory:插件生成pom的目录,默认为 ${project.basedir}

  • flatten:flatten

resources-process生成 .flattened-pom.xml,并在install/deploy时替换原始pom.xml

主要配置参数有:

flattenedPomFilename: 插件生成的pom的名字,默认为 .flattened-pom.xml

outputDirectory:插件生成pom的目录,默认为 ${project.basedir}

updatePomFile: packing=pom的module也进行reversion变量替换,默认为false

flattenMode:用来定义生成 .flattened-pom.xml所包含的元素,常用值有:

oss:开源软件常用,除了repositories/pluginRepositories外其他所有FlattenDescriptor定义的元素都生成

ossrh:所有FlattenDescriptor定义的元素都生成

bom:在ossrh基础上增加dependencyManagement和properties

defaults:除了repositories其他所有FlattenDescriptor定义的元素都不生成

clean:所有FlattenDescriptor定义的元素都不生成

fatjar:所有FlattenDescriptor定义的元素和dependencies都不生成

resolveCiFriendliesOnly:只替换原始pom中的revision, sha1 and changelist,其他否保持原样

常用oss/ossrh/resolveCiFriendliesOnly

  • FlattenDescriptor定义的pom.xml元素有:

modelVersion
groupId
artifactId
version
packaging
licenses
dependencies
profiles
name
description
url
inceptionYear
organization
scm
developers
contributors
mailingLists
pluginRepositories
issueManagement
ciManagement
distributionManagement
prerequisites
repositories
parent
build
dependencyManagement
properties
modules
reporting

二、使用总结

  • 不用flatten-maven-plugin

1.父pom定义版本为变量reversion并作为version,子pom复引用变量reversion作为version

2.结果能正常运行compile/test, 但install或deploy时父子pom中的version还是reversion变量未被替换

3.没有version别人无法引用你的包

父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>

子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>

install/deploy后父子pom.xml中的${reversion1}没有被替换

  • 使用了flatten-maven-plugin

1.父pom定义版本为变量reversion并作为version,子pom复引用变量reversion作为version

2.使用flatten-maven-plugin并设置updatePomFile=true,并绑定goal到maven周期

3.在process-resources阶段时会在父子project目录下生成.flattened-pom.xml(version已替换为具体值)

4.运行install或deploy时会将.flattened-pom.xml替换原来的pom.xml

原始父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>

到此这篇关于flatten-maven-plugin使用的文章就介绍到这了,更多相关flatten-maven-plugin使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • MyBatis传入参数为List对象的实现

    MyBatis传入参数为List对象的实现

    这篇文章主要介绍了MyBatis传入参数为List对象的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • 详解SpringBoot之访问静态资源(webapp...)

    详解SpringBoot之访问静态资源(webapp...)

    这篇文章主要介绍了详解SpringBoot之访问静态资源(webapp...),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • java8 Future异步调用实现方式

    java8 Future异步调用实现方式

    文章介绍了同步与异步调用的区别,Java中通过Future和CompletableFuture实现异步任务,后者提供更简洁的API,比较了流顺序执行、并行及自定义异步执行的效率,指出并行和自定义异步显著提升性能
    2025-09-09
  • 原理分析SonarQube中IdentityProvider账户互斥现象

    原理分析SonarQube中IdentityProvider账户互斥现象

    这篇文章主要为大家介绍分析SonarQube中IdentityProvider账户互斥现象原理,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2022-02-02
  • Java Stream 的 forEachOrdered 与 forEach的区别与适用场景

    Java Stream 的 forEachOrdered 与 forE

    在Java Stream API中,forEach和forEachOrdered是两个常用的终止操作,用于对流中的元素执行迭代处理,本文将从多个维度深入分析Java Stream的forEachOrdered与forEach的区别与适用场景,感兴趣的朋友一起看看吧
    2025-08-08
  • Ehcache简介_动力节点Java学院整理

    Ehcache简介_动力节点Java学院整理

    这篇文章主要介绍了Ehcache简介,使用Spring的AOP进行整合,可以灵活的对方法的返回结果对象进行缓存
    2017-07-07
  • java用list集合存储学生信息并算出成绩平均值操作

    java用list集合存储学生信息并算出成绩平均值操作

    这篇文章主要介绍了java用list集合存储学生信息并算出成绩平均值操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-08-08
  • SpringBoot集成ZXing实现二维码的生成与读取功能

    SpringBoot集成ZXing实现二维码的生成与读取功能

    本教程将详细讲解如何在 Spring Boot 项目中集成 ZXing 库实现二维码的生成(返回 Base64 编码)和读取(解析图片的二维码)功能,并覆盖常见异常处理、参数优化等实战要点,适合 Java 开发新手快速上手,需要的朋友可以参考下
    2026-03-03
  • Java上传文件进度条的实现方法(附demo源码下载)

    Java上传文件进度条的实现方法(附demo源码下载)

    这篇文章主要介绍了Java上传文件进度条的实现方法,可简单实现显示文件上传比特数及进度的功能,并附带demo源码供读者下载参考,需要的朋友可以参考下
    2015-12-12
  • IDEA整合Dubbo+Zookeeper+SpringBoot实现

    IDEA整合Dubbo+Zookeeper+SpringBoot实现

    初学者,想自己动手做一个简单的demo,本文主要介绍了IDEA整合Dubbo+Zookeeper+SpringBoot实现,需要的朋友们下面随着小编来一起学习学习吧
    2021-06-06

最新评论