Maven 在 POM 中增加项目版本,但过滤的资源获取旧版本
Posted
技术标签:
【中文标题】Maven 在 POM 中增加项目版本,但过滤的资源获取旧版本【英文标题】:Maven increments project version in POM, but filtered resource gets old version 【发布时间】:2016-02-16 07:26:28 【问题描述】:我想在我的 maven 构建中包含一个版本文件,这样每个构建都会自动增加构建号并在我的程序集中包含一个版本文件:
<major>.<minor>.<buildNo> 1.0.2 --> 1.0.3 //include this in version.txt
目前,我正在使用插件autoincrement-versions-maven-plugin
来自动增加 pom 文件中的版本;该部分有效,但是当我过滤version.txt
时,最终在我的程序集中和target/classes
中的文件具有旧版本号。
Maven 不允许动态更新<properties>
中定义的值,所以我理解为什么这不起作用(我认为),那么还有什么替代方法?我可以用 GMaven 插件破解它,但必须有一个更简单的方法。
项目结构
pom.xml
/src
/main
assembly.xml
version.txt
version.txt
$proj.version
程序集.xml
<assembly>
<id>asm</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>version</artifactId>
<packaging>pom</packaging>
<version>1.0.32</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.build.timestamp.format>MMM-dd-YYYY HH:mm:ss</maven.build.timestamp.format>
<build.timestamp>$maven.build.timestamp</build.timestamp>
<proj.version>$version</proj.version
</properties>
<pluginRepositories>
<pluginRepository>
<id>autoincrement-versions-maven-plugin</id>
<name>autoincrement-versions-maven-plugin</name>
<url>http://autoincrement-versions-maven-plugin.googlecode.com/svn/repo</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<build>
<resources>
<resource>
<directory>src/main</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>autoincrement-versions-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
<executions>
<execution>
<id>update-pom-versions</id>
<goals>
<goal>increment</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<autoIncrementVersion>true</autoIncrementVersion>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>filter-resources</id>
<goals>
<goal>resources</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>package-custom-assembly</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptor>src/main/assembly.xml</descriptor>
<finalName>finalfile</finalName>
<appendAssemblyId>false</appendAssemblyId>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2015 年 11 月 15 日更新 - 使用 GMaven Groovy 插件的解决方案
不是最性感的解决方案,但效果很好。这种方式没有使用上面提到的自增插件,也没有对version.txt
使用资源过滤;相反,Groovy 控制着一切。另一个好处是,版本信息从pom
外部化到build.properties
,这有助于在这种情况下使用 Groovy。
build.properties
fullVersionNumber=1.0.6
versionNumber=1.0
buildNumber=6
时髦
import java.nio.file.*
def version = project.properties['proj.version']
def build = project.properties['proj.build']
if(build?.isNumber())
build = build as Integer
build++
Properties buildProps = new Properties()
buildProps.setProperty('buildNumber', build.toString())
buildProps.setProperty('fullVersionNumber', version + '.' + build)
buildProps.setProperty('versionNumber', version.toString())
Path buildFile = Paths.get('./build.properties')
Path assemblyBuildFile = Paths.get('./target/classes/build.properties')
File propsFile = new File(buildFile.toUri())
Writer writer = null
try
//update build.properties
writer = propsFile.newWriter()
buildProps.store(writer, null)
//copy for assembly
Files.copy(buildFile, assemblyBuildFile)
catch(Exception e)
throw new RuntimeException(e)
finally
if(writer) writer.close()
else
throw RuntimeException('Invalid build number: ' + build)
带有 groovy 的完整 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>version</artifactId>
<packaging>pom</packaging>
<version>$versionNumber</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<proj.version>$versionNumber</proj.version>
<proj.build>$buildNumber</proj.build>
</properties>
<pluginRepositories>
</pluginRepositories>
<build>
<resources>
<resource>
<directory>src/main</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>./build.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>filter-resources</id>
<goals>
<goal>resources</goal>
</goals>
<phase>generate-resources</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.3</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
import java.nio.file.*
def version = project.properties['proj.version']
def build = project.properties['proj.build']
if(build?.isNumber())
build = build as Integer
build++
Properties buildProps = new Properties()
buildProps.setProperty('buildNumber', build.toString())
buildProps.setProperty('fullVersionNumber', version + '.' + build)
buildProps.setProperty('versionNumber', version.toString())
Path buildFile = Paths.get('./build.properties')
Path assemblyBuildFile = Paths.get('./target/classes/build.properties')
File propsFile = new File(buildFile.toUri())
Writer writer = null
try
//update build.properties
writer = propsFile.newWriter()
buildProps.store(writer, null)
//copy for assembly
Files.copy(buildFile, assemblyBuildFile)
catch(Exception e)
throw new RuntimeException(e)
finally
if(writer) writer.close()
else
throw RuntimeException('Invalid build number: ' + build)
</source>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>package-custom-assembly</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
<configuration>
<descriptor>src/main/assembly.xml</descriptor>
<finalName>finalfile</finalName>
<appendAssemblyId>false</appendAssemblyId>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
【问题讨论】:
不确定如何使用自动增量插件执行此操作,但我只是在 CI 脚本中的主要构建目标之前调用versions:set
。
【参考方案1】:
在包中更改阶段插件 autoincrement-versions-maven-plugin :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>autoincrement-versions-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
<executions>
<execution>
<id>update-pom-versions</id>
<goals>
<goal>increment</goal>
</goals>
<phase>package</phase>
<configuration>
<autoIncrementVersion>true</autoIncrementVersion>
</configuration>
</execution>
</executions>
</plugin>
【讨论】:
仍然在target/classes
和汇编文件中获取旧版本
抱歉 autoincrement-versions-maven-plugin => 包阶段而不是 maven-resources-plugin
您是否使用已发布的 pom 验证此作品?我在authoincrement
中设置了<phase>package</phase>
,但仍然是以前的版本。以上是关于Maven 在 POM 中增加项目版本,但过滤的资源获取旧版本的主要内容,如果未能解决你的问题,请参考以下文章