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

发布于 2024-07-18 14:13:01 字数 636 浏览 9 评论 0原文

我们使用命令行将系统属性传递给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); 

We use the command line to pass on system properties to the Java
virtual machine when running our Hudson builds on a Linux box. It used
to work quite well in 2.0.9 by since we upgraded to 2.1.0 it has
stopped working altogether. The system properties just never make it
to the Java virtual machine.

I have created a small test project and indeed it does not work at all.

This should work just fine with Maven 2.0.9:

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

But this will fail:

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

The Java code simply does this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生再见 2024-07-25 14:13:01

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

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

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

或者

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

I don't think this is a problem in either Maven or Surefire plug-in. Else the surefire is behaving differently. It looks like now, when Surefire forks the JVM, will not take all the system properties from the parent JVM.

That's why you should pass whatever system properties you want for the tests, using argLine.
So, both these should work

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

or

mvn2.1 test -DargLine="-Dsystem.test.property=test"
鲜血染红嫁衣 2024-07-25 14:13:01

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

I've experienced this with the Surefire plug-in. The Surefire plug-in is run under a different JVM instance that is launched by Maven. The command line params are configurable under the surefile-plugin configuration in your pom.xml. Here is a sample of our configuration.

        <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>
夜夜流光相皎洁 2024-07-25 14:13:01

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

Be careful to not mix up configuration file with command line arguments. Configuration file (pom.xml) is overriding all the cmd arguments. So don't configure surefire plugin within pom.xml if you want to pass it throught command line like raisercostin explained.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文