【发布时间】: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