Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a maven property within another property?

Assume I get a properties file from somewhere, that has this define:

dog=POODLE

And when running maven, I pass a parameter with the property name to look up:

mvn clean install -animal=dog

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").

Can this be done?

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}}

Thanks!

like image 777
Tomer K Avatar asked Feb 25 '13 19:02

Tomer K


1 Answers

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>

run with: mvn -Danimal=${dog} install

results in

[INFO] --- maven-echo-plugin:0.1:echo (default) @ nested-property ---
[INFO] Animal: POODLE
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
like image 167
FrVaBe Avatar answered Nov 15 '22 05:11

FrVaBe