gpt4 book ai didi

java - Maven 增加 POM 中的项目版本,但过滤资源得到旧版本

转载 作者:行者123 更新时间:2023-12-01 14:36:00 28 4
gpt4 key购买 nike

我想在我的 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

版本.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>

更新 11/15/2015 - 使用 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>

最佳答案

更改阶段插件 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>

关于java - Maven 增加 POM 中的项目版本,但过滤资源得到旧版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33714349/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com