38

在 Linux机器上运行Hudson构建时,我们使用命令行将系统属性传递给 Java 虚拟机。它曾经在 2.0.9 中运行良好,但自从我们升级到 2.1.0 后,它已经完全停止运行。系统属性永远不会进入 Java 虚拟机。

我创建了一个小型测试项目,实际上它根本不起作用。

这应该适用于Maven 2.0.9:

mvn2.0.9 -Dsystem.test.property=test test 

但这会失败:

mvn2.1 -Dsystem.test.property=test test 

Java 代码就是这样做的

assertTrue( System.getProperty("system.test.property") != null); 
4

3 回答 3

54

我认为这在 Maven 或 Surefire 插件中都不是问题。否则,万无一失的行为会有所不同。现在看起来,当 Surefire 分叉JVM时,不会从父 JVM 获取所有系统属性。

这就是为什么您应该使用 argLine 为测试传递所需的任何系统属性。所以,这两个都应该工作

mvn2.1 -Dsystem.test.property=test test -DforkMode=never 

或者

mvn2.1 test -DargLine="-Dsystem.test.property=test"
于 2009-05-05T09:14:41.603 回答
13

我在Surefire插件中体验过这一点。Surefire 插件在由 Maven 启动的不同 JVM 实例下运行。命令行参数可在 pom.xml 中的 surefile-plugin 配置下进行配置。这是我们的配置示例。

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.3</version>
            <!--
                    By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
                    "**/Test*.java" - includes all of its subdirectory and all java filenames that start with "Test". "**/*Test.java" -
                    includes all of its subdirectory and all java filenames that end with "Test". "**/*TestCase.java" - includes all of
                    its subdirectory and all java filenames that end with "TestCase".
                -->
            <configuration>
                <includes>
                    <include>**/*Test.java</include>
                </includes>
                <systemProperties>
                    <property>
                        <name>app.env</name>
                        <value>dev</value>
                    </property>
                     <property>
                        <name>oracle.net.tns_admin</name>
                        <value>${oracle.net.tns_admin}</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
于 2009-05-15T17:54:10.183 回答
2

注意不要将配置文件与命令行参数混淆。配置文件 (pom.xml) 覆盖所有 cmd 参数。所以不要在 pom.xml 中配置 surefire 插件,如果你想像 raisercostin 解释的那样通过命令行传递它。

于 2010-07-15T13:03:10.787 回答