【问题标题】:Pad Number With Zeros in Maven在 Maven 中用零填充数字
【发布时间】:2013-08-09 09:11:39
【问题描述】:

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

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

【问题讨论】:

  • 为什么需要这样的内部版本号?你有一个独一无二的版本。此外,我希望您使用的版本控制系统具有应用作参考的标签/标签等。
  • 我们拥有所有变更控制系统的好东西(maven、CI、持续部署等)

标签: maven


【解决方案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 转换字符串。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我刚刚查看了网站上有关 maven 发布插件的一些文档,但我看不出它如何解决我的问题。我应该调查插件的某个特定功能吗?
    • 查看我链接的页面中标题为“覆盖默认标签名称格式”的部分。
    【解决方案3】:

    根据@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>
    

    【讨论】:

      【解决方案4】:

      根据@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>
      

      【讨论】:

        【解决方案5】:

        然而,在这种特殊情况下,如果您还需要生成 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>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-19
          • 1970-01-01
          • 2010-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-29
          相关资源
          最近更新 更多