【maven插件】flatten-maven-plugin : 处理版本占位符

本文介绍如何在Maven项目中使用版本占位符如${revision}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

  • Apache Maven 3.5.0
  • org.codehaus.mojo:versions-maven-plugin 1.1.0
  • https://www.mojohaus.org/flatten-maven-plugin/

版本占位符

自 Maven 3.5.0-beta-1 开始,可以使用 ${revision}, ${sha1} and/or ${changelist} 这样的变量作为版本占位符。

像这样:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache.maven.ci</groupId>
  <artifactId>ci-parent</artifactId>
  <name>First CI Friendly</name>
  <version>${revision}</version>

  <properties>
	<revision>1.0</revision>
  </properties>
  ...
</project>

或者像这样:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache.maven.ci</groupId>
  <artifactId>ci-parent</artifactId>
  <name>First CI Friendly</name>
  <version>${revision}${sha1}${changelist}</version>
  ...
  <properties>
    <revision>1.0</revision>
    <changelist>-SNAPSHOT</changelist>
    <sha1/>
  </properties>
</project>

可以使用这样的命令

mvn -Drevision=2.7.8 -Dchangelist=-RELEASE -Dsha1=ssbd clean package

缺点来了

Install / Deploy 时,版本占位符将不能被替换。这将导致 Install / Deploy 后, maven 不能识别。
使用 flatten-maven-plugin 解决这个问题。

flatten-maven-plugin

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.apache.maven.ci</groupId>
  <artifactId>ci-parent</artifactId>
  <name>First CI Friendly</name>
  <version>${revision}</version>
  ...
  <properties>
    <revision>1.0.0-SNAPSHOT</revision>
  </properties>

 <build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>flatten-maven-plugin</artifactId>
      <version>1.1.0</version>
      <configuration>
        <updatePomFile>true</updatePomFile>
        <flattenMode>oss</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>
  <modules>
    <module>child1</module>
    ..
  </modules>
</project>

eclipse 中提示 lifecycle 错误

嗯, eclipse 中会提示 lifecycle 错误,需要添加 lifecycle 。

<?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">
    ...
	<build>
        ...
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.eclipse.m2e</groupId>
					<artifactId>lifecycle-mapping</artifactId>
					<version>1.0.0</version>
					<inherited>true</inherited>
					<configuration>
						<lifecycleMappingMetadata>
							<pluginExecutions>
								<pluginExecution>
									<pluginExecutionFilter>
										<groupId>org.codehaus.mojo</groupId>
										<artifactId>flatten-maven-plugin</artifactId>
										<versionRange>[1.0.0,)</versionRange>
										<goals>
											<goal>flatten</goal>
										</goals>
									</pluginExecutionFilter>
									<action>
										<ignore />
									</action>
								</pluginExecution>
							</pluginExecutions>
						</lifecycleMappingMetadata>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>
	</build>
</project>

参考

https://my.oschina.net/liyuj/blog/874929
https://www.cnblogs.com/jonath/p/7729903.html

### Maven 打包 `flatten-maven-plugin` 插件版本1.4.1 报错解决方案 当遇到 `flatten-maven-plugin` 版本1.4.1在执行 `flatten` 目标时报错的情况,可以采取以下措施来解决问题。 #### 确认Maven版本 确保使用的 Maven 版本不低于3.5。较低版本可能不完全兼容该插件的功能需求[^1]。可以通过命令行输入 `mvn -v` 来查看当前安装的Maven版本号。 #### 验证本地仓库中的插件状态 检查本地Maven仓库内是否有 `flatten-maven-plugin` 的缓存文件存在。如果不存在,则可能是下载失败或其他网络原因造成的缺失。尝试清理并重新加载依赖项: ```shell mvn clean install -U ``` 此操作会强制更新过期或未找到的远程资源到本地存储库中。 #### 修改POM配置 对于特定版本(如1.4.1),可以在项目的pom.xml文件里指定更精确的配置参数以适应不同的环境设置。例如: ```xml <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>flatten-maven-plugin</artifactId> <version>1.4.1</version> <configuration> <!-- 自定义配置选项 --> <updatePomFile>true</updatePomFile> <flattenMode>resolveCiFriendliesOnly</flattenMode> </configuration> <executions> <execution> <id>flatten</id> <phase>validate</phase> <goals> <goal>flatten</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 上述XML片段展示了如何调整插件的行为模式以及绑定生命周期阶段等细节[^2]。 通过以上几个方面的排查与修正,应该能够有效处理因 `flatten-maven-plugin` 导致的打包过程中出现的各种异常情况。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值