【问题标题】:How can I override maven property values set by properties-maven-plugin's read-project-properties goal?如何覆盖由 properties-maven-plugin 的 read-project-properties 目标设置的 maven 属性值?
【发布时间】:2015-09-12 10:50:31
【问题描述】:

在我们的一个项目中,我们使用 properties-maven-plugin 的 read-project-properties 目标从文件中读取属性值,然后用于过滤资源。请参阅this post,了解有关此过程的一般用途的讨论。

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

但是,这不适用于 properties-maven-plugin 设置的属性。

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

作为一种解决方法,我们目前正在向 properties-maven-plugin 注册一个额外的开发人员特定文件以实现此效果,但使用常规方式(配置文件)会更方便。

更笼统地说,问题是:properties-maven-plugin 的 read-project-properties 目标设置的属性如何与 maven 的属性定义优先级层次结构联系起来,这在 very helpful blog post 中有描述。

我将 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.xml,这样我们就可以看到你目前拥有的东西并可能给你建议。
  • @carlspring:刚刚做了,希望它能让问题更清楚。我目前的怀疑是,来自 POM 和 settings.xml 的属性值的解析已经发生在我可以将 read-project-properties 目标绑定到的任何生命周期阶段之前,并且这个目标只是覆盖了任何已经定义的属性。

标签: maven


【解决方案1】:

我想你会找到答案here

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

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

【讨论】:

  • 已经有一段时间了,但迟到总比没有好:非常感谢您指出这一点,这很清楚。
猜你喜欢
  • 2018-09-30
  • 1970-01-01
  • 2015-07-21
  • 1970-01-01
  • 2011-02-09
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多