flatten-maven-plugin插件实现打包替换占位符${}

问题背景

开发者平台工程引入用户中心模块依赖,其中用户中心模块内引入了一个国际化组件,国际化组件的version是一个占位符,版本号定义在了祖父pom的中,对用户中心打包后,国际化组件的version仍然是一个占位符,这导致开发者平台工程打包报错:因为版本号是一个占位符,所以找不到国际化组件的依赖。

如图:

执行mvn clean install打包异常,

打包异常信息:

[ERROR] Failed to execute goal on project nti-developer-platform-server: Could not resolve dependencies for project com.nti56.cloud:nti-developer-platform-server:jar:1.0.0-SNAPSHOT: Failed to collect dependencies at com.nti56.cloud:nti-platform-ucenter:jar:1.2.1-RELEASE -> com.nti56.cloud:nti-common-i18n-core:jar:${nti-common-i18n-core.version}: Failed to read artifact descriptor for com.nti56.cloud:nti-common-i18n-core:jar:${nti-common-i18n-core.version}: Could not transfer artifact com.nti56.cloud:nti-common-i18n-core:pom:${nti-common-i18n-core.version} from/to nti56-release (http://192.168.0.111:8300/repository/maven-public/): Failed to transfer file http://192.168.0.111:8300$%7Bnti-common-i18n-core.version%7D/nti-common-i18n-core-$%7Bnti-common-i18n-core.version%7D.pom with status code 400 -> [Help 1]

解决方案

引入flatten-maven-plugin插件,配置,加入对元素的解析,让flatten插件将所有依赖元素内的占位符都解析并替换为实际值:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>flatten-maven-plugin</artifactId>
    <version>1.4.0</version>
    <executions>
        <execution>
            <id>flatten</id>
            <phase>process-resources</phase>
            <goals>
                <goal>flatten</goal>
            </goals>
            <configuration>
                <updatePomFile>true</updatePomFile>
                <flattenMode>resolveCiFriendliesOnly</flattenMode>
                <pomElements>
                    <!-- 关键配置:对所有依赖中使用的占位符进行解析和替换为具体值 -->
                    <dependencies>resolve</dependencies>
                </pomElements>
            </configuration>
        </execution>
        <execution>
            <id>flatten.clean</id>
            <phase>clean</phase>
            <goals>
                <goal>clean</goal>
            </goals>
        </execution>
    </executions>
    <inherited>true</inherited>
    <configuration>
        <updatePomFile>true</updatePomFile>
        <flattenMode>resolveCiFriendliesOnly</flattenMode>
    </configuration>
</plugin>

flatten插件作用和配置

这个插件的作用是,生成一个压缩版的pom.xml文件,然后在install和deploy阶段使用压缩后的pom.xml文件,替换原来的pom.xml文件,具体压缩策略如下:

  • 和构建有关的元素会被删除;
  • 和开发有关的元素默认会被删除;
  • 只包含构件的使用者必须的一些信息;
  • 变量会被解析;
  • 上级关系会被解析,然后被压缩删除;
  • 构建时实际使用的profile会被评估,视情况处理;
  • 由JDK或者OS驱动的profile会被保留,需要时可以动态地控制依赖。

主要配置项说明

属性名

类型

描述

embedBuildProfileDependencies

Boolean

由OS或者JDK的不同而触发的profile,可能根据环境的不同而产生不同的依赖,但是由属性等触发的profile,就不确定了,如果属性设置为true,profile中的依赖会直接写入生成的pom中,如果设置为false,所有的profile信息都会保留,默认是false。

flattenMode

FlattenMode

插件预定义了若干种压缩模式,下面会详述。

flattenedPomFilename

String

生成的压缩后的

pom.xml

文件的文件名,默认为

.flattened-pom.xml

outputDirectory

File

生成的压缩后的

pom.xml

文件的存放位置,默认为

${project.basedir}

pomElements

FlattenDescriptor

该元素定义了如何处理额外的元素,如果可能,尽量使用

flattenMode

,这个元素仅仅用于进一步提高灵活性,它可以控制具体的某个元素是保留还是删除,比如要指定删除repositories,可以这样:

flatten

updatePomFile

Boolean

插件默认只会处理packaging属性为非pom的,如果要处理packaging为pom的,可将本属性值设置为true。

核心配置flattenMode详细说明

flattenMode配置对pom文件的压缩模式,包含以下几种类型:

oss:给那些想要保留所有可选POM元素的开源软件项目使用,这个选项会保留所有可选的POM元素,除了model# getrepository()远程仓库配置 和 model# getPluginRepositories() 插件仓库配置。

ossrh:保留OSS 以下Repository-Hosting所需的所有可选POM元素:jhttps://docs.sonatype.org/display/Repository/Sonatype+OSS+Maven+Repository+Usage+Guide

bom:与ossrh类似,但另外保留了model# getDependencyManagement() dependencyManagement和model# getProperties()属性。特别是,它将保持model# getDependencyManagement() dependencyManagement原样,而不解决父影响和导入范围的依赖关系。

defaults:默认模式,移除 除model# getrepository()存储库之外的所有可选POM元素。

clean:移除所有可选的POM元素。

fatjar:移除所有可选的POM元素和所有model# getDependencies()依赖项。

resolveCiFriendliesOnly:只解析变量revision, sha1和changelist。保留其他所有东西。即 对${revision}、${sha1}、${changelist}这3个占位符进行解析,并替换为具体值。

以上说明参考flatten-mavn-plugin插件xml说明文件。

核心配置pomElements详细说明

插件文档对这个配置的描述:”model定义了如何处理额外的POM元素。如果可能,请优先使用flattenMode。这个参数只是为了获得最大的灵活性。“

也就是说pomElements的作用是扩展flattenMode的功能,flattenMode给定了几种压缩pom.xml的类型,但这不够灵活,比如dependencies标签内的占位符在只配置flattenMode的情况下不会被解析和替换。

pomElements内可以添加的元素是maven pom文件中的所有元素,如:

<pomElements>
    <dependencyManagement>resolve</dependencyManagement>
    <!-- keep everything else -->
    <parent>keep</parent>
    <build>keep</build>
    <distributionManagement>keep</distributionManagement>
    <repositories>keep</repositories>
    <pluginRepositories>keep</pluginRepositories>
    <profiles>keep</profiles>
    ...
</pomElements>

标签值则是pomElements提供的处理方式。pomElements的处理方式有以下几种:

pomElement处理方式

描述

expand

Take the element from the effective POM.

flatten

Flatten the element.

interpolate

Take the element from the interpolated POM (original POM with variables interpolated).

keep

Take the element untouched from the original POM.

remove

Remove the element entirely so it will not be present in flattened POM.

resolve

Take the element from the resolved POM.本例中使用的处理类型,解析并替换占位符。

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值