3

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

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

4

5 回答 5

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属性可用于定义最终名称。

或者,正如在 Maven 中所建议的,如何在运行时动态构建属性值?, 用于build-helper:regex-property转换字符串。

于 2013-08-08T13:39:39.577 回答
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>
于 2013-12-11T15:31:09.940 回答
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>
于 2013-08-09T13:58:45.363 回答
0

您是否尝试过使用Maven 发布插件?

于 2013-08-07T20:15:51.103 回答
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>
于 2019-09-11T20:28:53.740 回答