简体   繁体   English

Maven在POM中增加项目版本,但过滤后的资源变为旧版本

[英]Maven increments project version in POM, but filtered resource gets old version

I'm want to include a version file in my maven build such that each build auto-increments the build no and includes a version file in my assembly: 我想在我的maven构建中包含一个版本文件,这样每个构建都会自动增加构建号,并在我的程序集中包含一个版本文件:

<major>.<minor>.<buildNo>   1.0.2 --> 1.0.3   //include this in version.txt

Currently, I'm using the plugin autoincrement-versions-maven-plugin to auto-increment the version in the pom file; 目前,我正在使用插件autoincrement-versions-maven-plugin来自动增加pom文件中的版本; that part works, but when I filter version.txt , the file that ends up in my assembly, and in target/classes , has the old version number. 该部分工作,但当我过滤version.txt ,在我的程序集和target/classes中结束的文件具有旧的版本号。

Maven doesn't allow dynamic updates for values defined in <properties> , so I understand why this isn't working (I think), so what's the alternative? Maven不允许动态更新<properties>定义的值,所以我理解为什么这不起作用(我认为),那么替代方案是什么? I can hack this with GMaven plugin, but there's gotta be a simpler way. 我可以用GMaven插件破解它,但是必须有一个更简单的方法。

Project structure 项目结构

   pom.xml
      /src
        /main
           assembly.xml
           version.txt

version.txt version.txt

${proj.version}

assembly.xml assembly.xml

<assembly>
    <id>asm</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>target/classes</directory>
            <outputDirectory>/</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

pom.xml 的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>

Update 11/15/2015 - Solution using GMaven Groovy Plugin 2015年11月15日更新 - 使用GMaven Groovy插件的解决方案

Not the sexiest solution, but it works well. 不是最性感的解决方案,但效果很好。 This approach doesn't use auto-increment plugin mentioned above, nor does it use resource filtering for version.txt ; 这种方法不使用上面提到的自动增量插件,也不使用version.txt资源过滤; instead, Groovy controls everything. 相反,Groovy控制着一切。 An added benefit, version info is externalized from pom to build.properties , which facilitates the use of Groovy in this case. 另外一个好处是,版本信息从pom外部化到build.properties ,这有助于在这种情况下使用Groovy。

build.properties build.properties

fullVersionNumber=1.0.6
versionNumber=1.0
buildNumber=6

Groovy Groovy的

 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)
 }

Full pom.xml with groovy 完整的pom.xml与groovy

<?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>

Change phase plugin autoincrement-versions-maven-plugin at package : 在包中更改阶段插件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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM