如何在另一个属性中引用Maven属性? [英] How to reference a maven property within another property?
问题描述
假设我从某个地方获得了一个属性文件,该文件具有以下定义:
Assume I get a properties file from somewhere, that has this define:
dog=POODLE
运行maven时,我传递一个带有属性名称的参数来查找:
And when running maven, I pass a parameter with the property name to look up:
mvn clean install -animal=dog
我需要能够在pom.xml中检索值"POODLE"
,而无需知道要查找的属性(我还不知道我正在寻找狗",但仅我正在寻找动物").
I need to be able to retrieve in the pom.xml the value "POODLE"
without knowing what's the property to look up for (I don't know yet that I'm looking up for a "dog", but only that I'm looking up for an "animal").
可以做到吗?
我可以在pom ${animal}
中引用它,而它会被dog
代替,但是我需要查一下.
I can reference in the pom ${animal}
which will be replaced with dog
, but then I need to look that up.
我很无辜,可以尝试以下操作,但是它行不通:
I was innocent enough to try the following, but it won't work:
${${animal}}
谢谢!
推荐答案
如果使用-Danimal=${dog}
,它应该可以工作.这是我的例子
It should work if you use -Danimal=${dog}
. Here is my example
<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>nested-property</groupId>
<artifactId>nested-property</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<dog>POODLE</dog>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>maven-echo-plugin</artifactId>
<version>0.1</version>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>echo</goal>
</goals>
</execution>
</executions>
<configuration>
<echos>
<echo>Animal: ${animal}</echo>
</echos>
</configuration>
</plugin>
</plugins>
</build>
</project>
运行:mvn -Danimal=${dog} install
产生
[INFO] --- maven-echo-plugin:0.1:echo (default) @ nested-property ---
[INFO] Animal: POODLE
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
这篇关于如何在另一个属性中引用Maven属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!