首页 > 解决方案 > Maven buildnumber,替换文件不起作用

问题描述

我想buildNumber在 XML 文件中包含 from buildnumber-maven-plugin。

我配置了内部版本号插件。我可以看到它有效,因为 finalName 是使用内部版本号设置的。然后我配置了资源过滤。我可以看到资源deployment.xml已被过滤,因为它替换了${project.version}. 但是它并不能取代${buildNumber}. 我必须采取哪些不同的措施才能使其发挥作用?我查看了Maven 内部版本号插件,如何将内部版本号保存在文件中?,但它不像那里那样工作,尽管几乎相同。

pom.xml:

    <!-- build number -->
        <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>buildnumber-maven-plugin</artifactId>
           <version>1.4</version>
           <executions>
             <execution>
               <phase>validate</phase>
               <goals>
                 <goal>create</goal>
               </goals>
             </execution>
           </executions>
           <configuration>
             <format>{0,number}</format>
             <items>
                <item>buildNumber</item>
             </items>
             <doCheck>false</doCheck>
             <doUpdate>false</doUpdate>
           </configuration>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}-${project.version}-r${buildNumber}</finalName>
    <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
          <includes>
            <include>**/deployment.xml</include>
          </includes>
        </resource>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>false</filtering>
          <excludes>
            <exclude>**/deployment.xml</exclude>
          </excludes>
        </resource>
    </resources>
</build>
<!-- dummy scm for build number plugin -->
<scm>
   <connection>scm:git:http://127.0.0.1/dummy</connection>
   <developerConnection>scm:git:https://127.0.0.1/dummy</developerConnection>
   <tag>HEAD</tag>
   <url>http://127.0.0.1/dummy</url>
</scm>

src/main/resources/deployment.xml

<root>
    <build>
        <buildNumber>${buildNumber}</buildNumber>
        <buildNumber>${project.version}</buildNumber>
        <buildNumber>${project.finalName}</buildNumber>
    </build>
</root>

目标/类/deployment.xml

<root>
    <build>
        <buildNumber>${buildNumber}</buildNumber>
        <buildNumber>1.0.0-BUILD-SNAPSHOT</buildNumber>
        <buildNumber>${project.finalName}</buildNumber>
    </build>
</root>

我现在注意到一些奇怪的事情:在 maven 安装期间,内部版本号首先被替换为 XML 文件,然后 XML 文件再次被上面${buildNumber}没有替换的 XML 文件替换。

我尝试关闭额外的复制步骤,但它似乎没有达到我期望的效果(我仍然有同样的问题):

[INFO] --- maven-resources-plugin:3.1.0:copy-resources (copy-resources) @ rator ---
[INFO] Using 'Cp1252' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 83 resources
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ rator ---
[INFO] Skipping the execution.
[INFO] 
.. compiling ..
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ rator ---
[INFO] Not copying test resources
[INFO] 

我在看目标/课程。当我查看target/xxx-1.0.0-BUILD-SNAPSHOT-r36/WEB-INF/classes/deployment.xml. 我想我可以忍受这一点,尽管我仍然不明白为什么target/classes/deployment.xml在替换 buildNumber 后文件会重新创建。

更奇怪的是:当我编辑时src/main/resources/deployment.xml,文件target/classes/deployment.xml也会更新。它必须是由eclipse IDE完成的吗?

我将 deployment.xml 移至resources-filtered/deployment.xml但覆盖行为仍然存在。

标签: eclipsemavenpom.xmlversioning

解决方案


在您的 xml 中有一个属性部分。这样您就可以告诉 Maven 您的自定义属性,这些属性将在构建时被替换。

<project>
  <properties>
      <buildNumber>YOUR_BUILD_NUMBER</buildNumber>
      <anotherProperty>foo</anotherProperty>
  </properties>
</project>

https://maven.apache.org/pom.html#Properties

这里是预定义 Maven 属性的非官方列表:https ://github.com/cko/predefined_maven_properties/blob/master/README.md


推荐阅读