16

I have 2 profiles that may or may not be used together to run a group of tests. They each require different vmargs to run, but if they are used together it's ok to have them appended to each other.

What I'm looking for is a way to set argLine to be the concatenation of its current value plus what I set.

I was hoping it would as simple as

<argLine>${argLine} -DnewVMArg</argLine>

Is there something similar I can do to make this happen?

I made an attempt at fixing it which results in maven getting stuck in a recursive cycle. It's documented below.

My Most recent attempt was to define a property <my.argLines></my.argLines> globally, and then to modify this within the profiles.

In each profile, in a properties block, I set overrode the property to:

<my.argLines>${my.argLines} -myUniqueToProfileArgs</my.argLines>

In each surefire configuration for the profiles, I set <argLines> to be:

<argLines>${my.argLines}</argLines>

This logically fits for me, but the way it evalutes is apparently not going to mesh.

user321605
  • 846
  • 2
  • 7
  • 20
  • 1
    Is it working when you set the arguments explicitly, instead of using the global var? Put your entire pom.xml file here, please. – Pmt Oct 28 '13 at 18:34
  • Does it help to write as `@{argLine} -DnewVMArg`? This Hint is related to surefire / jacoco interplay, see https://github.com/jacoco/jacoco/issues/964 – leo Feb 04 '22 at 11:37

4 Answers4

9

Define your default arguments -DnewVMArg inside argLine like below:

<properties>
    <customArg/>
    <argLine>${customArg} -DnewVMArg</argLine>
</properties>

Define profiles arguments

<profiles>
    <profile>
        <id>profile1</id>
        <properties>
            <customArg>-DmyUniqueToProfile1Args</customArg>
        </properties>
    </profile>
    <profile>
        <id>profile2</id>
        <properties>
            <customArg>-DmyUniqueToProfile2Args</customArg>
        </properties>
    </profile>
</profiles>

Additional plugin configuration is not required

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration/>
        </plugin>
....

I have tested this configuration, my results below.

Default

mvn surefire:test -X 

Result

(...)java -jar -DnewVMArg (...) 

Goal with profile

mvn surefire:test -X -Pprofile1

Result

(...)java -DmyUniqueToProfile1Args -DnewVMArg -jar (...) 
MariuszS
  • 30,646
  • 12
  • 114
  • 155
  • That way, only profile1 OR profile2 could be actively contributing to the argLine, however in the original question, both profiles could be used together. – blackbuild Feb 04 '14 at 14:01
2

If you are dealing only with -D system properties, you could use <systemPropertyVariables> instead of <argLine> and then they will be combined naturally. One of the profiles could have:

<systemPropertyVariables>
    <propertyFromProfile1>value1</propertyFromProfile1>
</systemPropertyVariables>

and the second profile:

<systemPropertyVariables>
    <propertyFromProfile2>value2</propertyFromProfile2>
</systemPropertyVariables>

Also, it's worth mentioning that this approach allows you to override in child poms individual properties from parent poms.

Bogdan Calmac
  • 7,993
  • 6
  • 51
  • 64
  • I think this not work for JVM options like `-Xmx1024m`, only for standard `-Dfoo=bar`, yes? – MariuszS Jan 11 '14 at 17:46
  • There's some language confusion here. I just said that your statement is correct. Also, I mentioned this detail in the beginning of my answer: "If you are dealing only with -D system properties ..." – Bogdan Calmac Jan 12 '14 at 16:04
0

As you found out, a property cannot reference itself.

You need to define different properties for each profile and finally concatenate them in your surefire call:

<properties>
  <!-- it is a good idea not to use empty or blank properties -->
  <first.props>-Dprofile1Active=false</first.props>
  <second.props>-Dprofile2Active=false</second.props>
</properties>
...
    <!-- surefire configuration -->
    <argLine>${first.props} ${second.props}</argLine>    
...
<profile>
  <id>first</id>
  <properties>
    <first.props>-myUniqueToProfile1Args</first.props>
  </properties>
</profile>
<profile>
  <id>second</id>
  <properties>
    <second.props>-myUniqueToProfile2Args</second.props>
  </properties>
</profile>

Also note the not-empty default value. Maven has some surprising way of handling those. In order to be on the safe side, use harmless non-blank default values (see “Null” versus “empty” arguments in Maven)

Community
  • 1
  • 1
blackbuild
  • 5,026
  • 1
  • 23
  • 35
-1

Eclipse: Window -> Preferences -> TestNG -> Maven Uncheck the 'argLine'.

Sats
  • 1,061
  • 9
  • 12