【问题标题】:Maven 2.1.0 not passing on system properties to Java virtual machineMaven 2.1.0 未将系统属性传递给 Java 虚拟机
【发布时间】:2010-10-23 20:48:25
【问题描述】:

我们使用命令行将系统属性传递给 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-2 jvm maven-plugin surefire system-properties


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 令人惊讶的是 Locale.getDefault() 这些工作 mvn test -DargLine="-Duser.language=de -Duser.region=DE" 而不是 mvn test -DargLine="-Dsystem.user。语言=de -Dsystem.user.region=DE"
    • 请注意,vor maven 3 您只使用mvn -Dsystem.test.property=test test。 Maven 将属性传播到测试中。
    • 非常有帮助的答案。我被困住了 2 天,它对我有用,谢谢。根据问题还有一个问题,如果 maven 版本是 2.1.0,那么像“mvn test -Dtesting=testing”这样的命令行参数不起作用,但我的 maven 版本是 3.0.5,jvm 版本是 8。为什么它在我的案子。如果我按照您在回答中的指导传递参数,那就很好了。请解释一下。
    【解决方案2】:

    我在使用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>
    

    【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-22
      • 2017-03-02
      相关资源
      最近更新 更多