Maven surefire: append to argLine

做~自己de王妃 提交于 2019-11-30 11:50:50

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

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.

blackbuild

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!