我想在我的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
${proj.version}
<assembly>
<id>asm</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>target/classes</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
<?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>
不是最性感的解决方案,但效果很好。这种方法不使用上面提到的自动增量插件,也不使用version.txt
的资源过滤;相反,Groovy控制着一切。另外一个好处是,版本信息从pom
外部化到build.properties
,这有助于在这种情况下使用Groovy。
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)
}
<?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>
答案 0 :(得分:0)
在包中更改阶段插件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>