垫圈编号与Maven中的零

时间:2013-08-07 20:02:57

标签: maven

有没有办法在POM中使用maven pad数值(例如:内部版本号)?我一直在谷歌上搜索这个话题,还没有想出任何东西。

我的用例如下。 maven构建过程通过Jenkins提供构建号,需要将其作为生成的WAR名称的一部分包含在内。因此,如果我提供12作为内部版本号,那么我希望WAR文件名为myWar ## 000012.war。 ## 000012部分名称是Tomcat使用的版本标识符。

5 个答案:

答案 0 :(得分:1)

最简单的解决方案可能是在您的构建中嵌入脚本语言。例如,使用Groovy,如果您有buildNumber属性:

   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
     <execution>
     <phase>validate</phase>
     <goals><goal>execute</goal></goals>
     <configuration>
      <source>
       project.properties['nameSuffix'] = "##" + String.format("%06d", project.properties['buildNumber'].toLong());
      </source>
     </configuration>
     </execution>
    </executions>
   </plugin>

之后,nameSuffix属性可用于定义最终名称。

或者,如In Maven, how can I dynamically build a property value at runtime?中所述,使用build-helper:regex-property转换字符串。

答案 1 :(得分:0)

您是否尝试过使用maven release插件?

答案 2 :(得分:0)

根据@Joe的建议,我查看了build-helper-maven-plugin,并能够提出以下内容,它可以满足我的需求。我无法一步确定如何完成所有操作,所以我在2中完成。第一步用零填充左边的值。第二步修剪数值,使其仅为7位数。请注意,$ {build.env.version}作为参数传递给maven构建过程,并且我在POM文件中将其默认为0,以便在不传递时。如果未提供默认值,则即使将failOnError设置为false,构建也会失败。

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <id>stage1--padNumber</id>
        <goals>
          <goal>regex-property</goal>
        </goals>
        <configuration>
          <name>build.env.version.padded</name>
          <value>${build.env.version}</value>
          <regex>^([\d]{0,})$</regex>
          <replacement>000000$1</replacement>
          <failIfNoMatch>false</failIfNoMatch>
          <failOnError>false</failOnError>
        </configuration>
      </execution>
      <execution>
        <id>stage2--leftTrimToXcharacters</id>
        <goals>
          <goal>regex-property</goal>
        </goals>
        <configuration>
          <name>build.env.version.padded</name>
          <value>${build.env.version.padded}</value>
          <regex>^([\d]*)([\d]{7})$</regex>
          <replacement>$2</replacement>
          <failIfNoMatch>false</failIfNoMatch>
          <failOnError>false</failOnError>
        </configuration>
      </execution>
    </executions>
  </plugin>

答案 3 :(得分:0)

根据@ jwmajors81建议,我需要填写主要版本,因为特定原因......

由于我们已经在使用build-helper-maven-plugin,因此使用build helper的parse-version目标很容易获得主要版本。 (我们只需要3个字符):

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>parse-version</id>
                    <goals>
                        <goal>parse-version</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stage1--padNumber</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>build.env.version.padded</name>
                        <value>${parsedVersion.majorVersion}</value>
                        <regex>^([\d]{0,})$</regex>
                        <replacement>00$1</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                        <failOnError>false</failOnError>
                    </configuration>
                </execution>
                <execution>
                    <id>stage2--leftTrimToXcharacters</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>build.env.version.padded</name>
                        <value>${build.env.version.padded}</value>
                        <regex>^([\d]*)([\d]{3})$</regex>
                        <replacement>$2</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                        <failOnError>false</failOnError>
                    </configuration>
                </execution>
            </executions>
        </plugin>

答案 4 :(得分:0)

但是,在这种情况下,如果您还需要生成一个buildNumber,buildnumber-maven-plugin可能是最直接的解决方案:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <format>{0, number,000000}</format>
                <items>
                    <item>buildNumber</item>
                </items>
            </configuration>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>create</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>myWar##${buildNumber}</finalName>
相关问题