在我们的一个项目中,我们使用 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>
最佳答案
我想你会找到答案here :
Modifications of project properties that happen during project lifecycle have no effect on the effective pom – it is just too late. Examples of such modifications include groovy scripts (via gmaven-plugin) and properties loaded from external files via maven-properties-plugin. So, why do we need them at all? Since they can be used by other plugins in runtime, when they are read directly from properties collection during plugin invocation.
由于属性是在通常的分辨率之后读取的,因此它们只会覆盖配置文件中设置的任何内容。
关于maven - 如何覆盖由 properties-maven-plugin 的 read-project-properties 目标设置的 maven 属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31056985/