CosminO
CosminO

Reputation: 5226

Maven - changing defaulted argument to other pom.xml defined

I have a test distribution for testNG running with maven. Whenever I need the default test distribution, I can do so and whenever I need just a few or only one test, I can change something like the -DoverallParams in command line (although names have been changed).

<properties>
    <param1>x</param1>
    <param2>y</param2>
    <param3>z</param3>
    <defaultParams>${param1},${param2}</defaultParams>
    <defaultParams2>${param1},${param3}</defaultParams2> <-This does not exist yet!
    <overallParams>${defaultParams}</overallParams>
</properties>

I now need to use a different set of tests for a different platform, without duplicating or branching the project. So the idea would be adding a defaultPrams2 and somehow selecting it in the command line.

Question:

Is there any way to have something in the command line which would make me select defaultParams2 as the overallParams? Simpler yet, is there a way to reference a pom property in command line? Do you have another idea on how to do this?

Upvotes: 0

Views: 768

Answers (1)

A_Di-Matteo
A_Di-Matteo

Reputation: 27862

Is there any way to have something in the command line which would make me select defaultParams2 as the overallParams?

Yes, Maven profiles can help you on that.

You could have in your pom the following:

<profiles>
    <profile>
        <id>meaningful-name-here</id>
        <properties>
            <overallParams>${defaultParams2}</overallParams>
        </properties>
    </profile>
</profiles>

Then you could invoke Maven from command line as following:

mvn clean install -Pmeaningful-name-here

Basically, the -P<id> option will activate the profile above which will then override the value of the overallParams property and as such switch to the defaultParams2 value at runtime and on demand.

This approach is less error prone than the following:

mvn clean install -DoverallParams=params

Where you would need to type each and every time the required parameters (and as such override on demand the value of overallParams). Just choose a better (and shorter) id than meaningful-name-here :)

is there a way to reference a pom property in command line?

Yes, via explicit override (-D) or via profiles (-P) as described above.

Upvotes: 1

Related Questions