当前在Maven构建中遇到一个奇怪的问题,该构建正在调用ant,在父pom中定义了以下属性:-
<jboss.home>${env.JBOSS_HOME}</jboss.home>
当我运行Maven时,我试图通过传递-Djboss.home来覆盖这一点。
这个构建包含一个配置文件,当激活时调用Ant构建:-
<ant antfile="build.xml" inheritRefs="true">
<target name="all"/>
</ant>
Ant脚本使用属性:-
<property name="jboss.dir" value="${jboss.home}"/>
随后输出:-
<echo message="jboss dir is: ${jboss.dir}"/>
问题是它输出的值是${env.JBOSS_HOME}
我可以从命令行覆盖父pom中的其他属性。
即使是jboss.home属性,如果在Maven构建中的其他地方使用,也会被覆盖。
在尝试了各种命令组合后,传递给Ant的属性集似乎是从poms中解析出来的,然后再从命令行进行任何重写。如果我设置JBOSS_HOME环境变量,那么使用该变量的所有地方都具有正确的值。
在命令行上重写这个变量并在Ant脚本中使用重写的值,我是否缺少一些东西?
今天偶然发现了这个问题,并找到了答案。此链接提供了一些背景信息:http://technotes.khitrenovich.com/properties-resolution-maven-implications-antrun-plugin/
如果属性在<target>。例如:
<execution>
..
<configuration>
<target>
<echo message="Command-line override of external.property will work OK like this: ${external.property}"/>
</target>
</configuration>
</execution>
However, if you use an external ant file written like this, many/most properties will work, BUT command-line overrides will not be passed into the ant file. You will get the non-overridden value instead.
<execution>
..
<configuration>
<target>
<property name="external.property" value="${external.property}" />
<ant antfile="build.xml" target="all" />
</target>
</configuration>
</execution>
Instead, for external ant files, use this syntax. The command line value will be passed through correctly to the external antfile:
<execution>
..
<configuration>
<target>
<ant antfile="build.xml" target="all" >
<property name="external.property" value="${external.property}" />
<ant/>
</target>
</configuration>
</execution>
The problem is that ${env.JBOSS_HOME}
is a system environment variable and you are instead passing to the JVM a system property -Djboss.home=...
. These are two different things. Apart from that, any variables, args and so on in the Java world are case sensitive.
You seem do be doing all the right things, it works for me, I have made a working example with 2 pom.xml files. The parent pom looks like this:
<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>com.stackoverflow.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<name>Test</name>
<packaging>pom</packaging>
<modules>
<module>test-ant-properties</module>
</modules>
<properties>
<jboss.home>${env.JBOSS_HOME}</jboss.home>
</properties>
</project>
然后,我在子文件夹test-ant-properties中创建了一个模块pom.xml,如下所示:
<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>
<parent>
<groupId>com.stackoverflow.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
</parent>
<artifactId>test-ant-properties</artifactId>
<name>Test maven ant properties</name>
<profiles>
<profile>
<id>jboss</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>jboss-ant</id>
<phase>install</phase>
<configuration>
<target>
<property name="jboss.dir" value="${jboss.home}"/>
<echo message="jboss dir is: ${jboss.dir}"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
我没有安装JBOSS,所以出于测试目的,我将环境变量设置为test1234,如下所示:set JBOSS_HOME=test1234
当我使用jboss配置文件执行父pom时
mvn install -Pjboss
我得到以下结果:[echo]jboss-dir是:test1234
当我使用jboss.home设置执行相同的命令时
mvn install -Pjboss -Djboss.home=my_custom_variable
我得到以下结果:[echo]jboss-dir是:my_custom_variable