通过Maven命令行设置属性 [英] Setting properties via Maven command line

查看:436
本文介绍了通过Maven命令行设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Maven时,我对通过命令行为某些单元测试设置属性的正确方法感到困惑。有很多问题(例如在未设置MAVEN_OPTS环境变量的情况下指定Maven内存参数有没有办法通过命令行将jvm args传递给maven?如何为Junit单元测试设置JVM参数?)触及这个主题,但没有一个我正在寻找的答案。

I'm confused about the correct way to set a property for some unit tests via the command line when using Maven. There are a number of questions (e.g. Specifying Maven memory parameter without setting MAVEN_OPTS environment variable, Is there a way to pass jvm args via command line to maven?, How to set JVM parameters for Junit Unit Tests?) that touch on this subject but none have the answer I'm looking for.

我想将属性 java.util.logging.config.class 设置为某个值,但我不知道我想设置 MAVEN_OPTS 环境变量。

I want to set the property java.util.logging.config.class to some value but I don't want to set the MAVEN_OPTS environment variable.

我可以使用属性在我的pom文件中配置surefire插件:

I can configure the surefire plugin in my pom file with the property:

<argLine>-Djava.util.logging.config.class=someClass</argLine>

因此每次运行测试阶段时都会设置它。

so that it is set every time the test phase is run.

但是,如果我从pom文件中删除该设置并将以下内容添加到命令行:

However, if I remove the setting from the pom file and add the following to the command line:

mvn package -DargLine="java.util.logging.config.class=someClass"

然后是报告测试阶段中的跟随错误并且构建失败:

then the following error in the test phase is reported and the build fails:


错误:无法找到或加载主类java.util.logging。 config.class = someClass

Error: Could not find or load main class java.util.logging.config.class=someClass

如果我从命令行运行以下命令:

If I run the following from the command line:

mvn package -Djava.util.logging.config.class=someClass

然后在构建开始时报告以下错误,但构建和测试成功:

then the following error is reported at the beginning of the build but the build and tests are successful:


记录配置类someClass失败
java.lang.ClassNotFoundException:someClass

Logging configuration class "someClass" failed java.lang.ClassNotFoundException: someClass

我真的不明白这种行为。有人可以开导我吗?

I don't really understand the behaviour. Can someone enlighten me?

推荐答案

是的,你应该

mvn package -DargLine="-Djava.util.logging.config.class=someClass"

请注意在 argLine 中添加 -D

让我们解释原因。 argLine 确实是 maven的一个属性-surefire-plugin ,具有相应的用户属性 argLine 。这意味着您可以使用 -DargLine 直接在命令行上设置此属性。但是,该属性的值是 -Djava.util.logging.config.class = someClass 。这正是您在POM中直接配置插件时所拥有的

Let's explain why. argLine is indeed an attribute of the maven-surefire-plugin with the corresponding user property argLine. This means that you can set this property directly on the command line with -DargLine. But then, the value of that property is -Djava.util.logging.config.class=someClass. This is exactly what you had when you configured the plugin in the POM directly with

<argLine>-Djava.util.logging.config.class=someClass</argLine>






此外,当你致电


Additionally, when you call

mvn package -Djava.util.logging.config.class=someClass

然后您没有设置 argLine 属性。您正在添加系统属性。

then you are not setting the argLine property. You are adding a system property.

这篇关于通过Maven命令行设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆