16

我有 2 个配置文件可能会或可能不会一起用于运行一组测试。它们每个都需要不同的 vmargs 才能运行,但如果它们一起使用,则可以将它们相互附加。

我正在寻找一种将 argLine 设置为其当前值加上我设置的串联的方法。

我希望它会像

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

有没有类似的事情我可以做到这一点?

我尝试修复它,导致 maven 陷入递归循环。它记录在下面。

我最近的尝试是<my.argLines></my.argLines>全局定义一个属性,然后在配置文件中修改它。

在每个配置文件的属性块中,我将覆盖属性设置为:

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

在配置文件的每个安全配置中,我设置<argLines>为:

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

这在逻辑上适合我,但它评估的方式显然不会网格化。

4

4 回答 4

9

-DnewVMArg在里面定义你的默认参数,argLine如下所示:

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

定义配置文件参数

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

不需要额外的插件配置

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

我已经测试过这个配置,我的结果如下。

默认

mvn surefire:test -X 

结果

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

目标与配置文件

mvn surefire:test -X -Pprofile1

结果

(...)java -DmyUniqueToProfile1Args -DnewVMArg -jar (...) 
于 2014-01-11T16:27:33.063 回答
2

如果您只处理 -D 系统属性,则可以使用 <systemPropertyVariables> 而不是 <argLine>,然后它们将自然组合。其中一个配置文件可能具有:

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

和第二个配置文件:

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

此外,值得一提的是,这种方法允许您在子 pom 中覆盖来自父 pom 的单个属性。

于 2014-01-11T16:17:08.700 回答
0

正如您所发现的,属性不能引用自身。

您需要为每个配置文件定义不同的属性,最后在您的肯定调用中将它们连接起来:

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

另请注意非空默认值。Maven 有一些令人惊讶的处理方式。为了安全起见,请使用无害的非空白默认值(请参阅Maven 中的“Null”与“empty”参数

于 2013-12-17T13:40:25.613 回答
0

Eclipse:窗口 -> 首选项 -> TestNG -> Maven 取消选中“argLine”。

于 2016-09-08T13:49:22.437 回答