我们使用命令行将系统属性传递给Java 运行我们的 Hudson 构建在Linux机器上的虚拟机。它用了 自从我们升级到2.1.0之后,在2.0.9中工作得很好 完全停止了工作。系统属性永远不会成功 到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); 
有帮助吗?

解决方案

我不认为这是Maven或Surefire插件中的问题。否则,万不得已表现出不同的表现。现在看来,当Surefire分析 JVM 时,不会从父级获取所有系统属性JVM。

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

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

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

其他提示

我通过 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>

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top