Maven 2.1.0没有将系统属性传递给Java虚拟机

rai*_*tin 38 maven-2 jvm system-properties maven-plugin surefire

在Linux 机器上运行Hudson构建时,我们使用命令行将系统属性传递给Java虚拟机.它曾经在2.0.9中很好地工作,因为我们升级到2.1.0它已经完全停止工作.系统属性永远不会进入Java虚拟机.

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

这应该适用于Maven 2.0.9:

mvn2.0.9 -Dsystem.test.property=test test 
Run Code Online (Sandbox Code Playgroud)

但这会失败:

mvn2.1 -Dsystem.test.property=test test 
Run Code Online (Sandbox Code Playgroud)

Java代码就是这么做的

assertTrue( System.getProperty("system.test.property") != null); 
Run Code Online (Sandbox Code Playgroud)

rai*_*tin 53

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

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

mvn2.1 -Dsystem.test.property=test test -DforkMode=never 
Run Code Online (Sandbox Code Playgroud)

要么

mvn2.1 test -DargLine="-Dsystem.test.property=test"
Run Code Online (Sandbox Code Playgroud)

  • 请注意,vor maven 3你只需使用`mvn -Dsystem.test.property = test test`.Maven将财产用于测试. (3认同)
  • 真有帮助的答案.我被困了2天,它对我有用,谢谢.还有一个问题,如果maven版本是2.1.0然后命令行参数如"mvn test -Dtesting = testing"不起作用,但我的maven版本是3.0.5而jvm版本是8.为什么它不能在我的工作案件.如果我按照你在回答中的指导传递参数,它的工作正常.请解释. (2认同)

Mik*_*one 12

我通过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>
Run Code Online (Sandbox Code Playgroud)

  • 现在使用<systemPropertyVariables>而不是<systemProperties>.请参阅http://maven.apache.org/plugins/maven-surefire-plugin/examples/system-properties.html (4认同)