如何覆盖由属性 maven-plugin 的读取项目属性目标设置的 maven 属性值?



在我们的一个项目中,我们使用属性maven插件的读取项目属性目标从文件中读取属性值,然后用于筛选资源。有关此程序的一般目的的讨论,请参阅此帖子。

我们希望使用特定于开发人员的settings.xml中定义的适当配置文件来覆盖文件中的一些值(与覆盖POM中设置的属性相同)。

然而,这对属性maven插件设置的属性不起作用。

我们怎样才能实现我们的目标?

作为解决方案,我们目前正在使用properties maven插件注册一个额外的、特定于开发人员的文件,以实现这种效果,但使用常规方式(概要文件)会方便得多。

更一般地说,问题是:由属性maven插件的读取项目属性目标设置的属性如何与maven的属性定义优先级层次结构相关联,这在这篇非常有用的博客文章中进行了描述。

我将POM的相关元素提取到一个玩具项目中,该项目展示了我的问题。这是玩具项目的POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>xxx</groupId>
  <artifactId>MavenTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Maven Test</name>
  <description>A maven project to test filtering and the maven-properties-plugin</description>
  <build>
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <executions>
          <!-- Read properties from a file -->
          <execution>
            <id>load-filter-properties</id>
            <goals>
              <goal>read-project-properties</goal>
            </goals>
            <phase>initialize</phase>
            <configuration>
              <files>
                <file>filters/filterTest.properties</file>
              </files>
              <quiet>false</quiet>
            </configuration>
          </execution>
          <!-- The following execution is for debug purposes only -->
          <execution>
            <id>write-project-properties</id>
            <inherited>false</inherited>
            <goals>
              <goal>write-project-properties</goal>
            </goals>
            <phase>package</phase>
            <configuration>
              <outputFile>filters/project.properties</outputFile>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12-beta-1</version>
    </dependency>
  </dependencies>
</project>

我想你会在这里找到答案:

在项目生命周期中对项目属性的修改对有效的pom没有影响——只是为时已晚。此类修改的示例包括groovy脚本(通过gmaven插件)和通过maven属性插件从外部文件加载的属性。那么,我们为什么需要它们呢?由于它们可以在运行时由其他插件使用,因此在插件调用期间直接从属性集合读取它们。

由于属性是在通常的分辨率后读取的,因此它们只是覆盖配置文件中设置的内容。

最新更新