简体   繁体   English

通过Maven命令行设置属性

[英]Setting properties via Maven command line

I'm confused about the correct way to set a property for some unit tests via the command line when using Maven. 我很困惑在使用Maven时通过命令行为某些单元测试设置属性的正确方法。 There are a number of questions (eg 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. 有很多问题(例如,在没有设置MAVEN_OPTS环境变量的情况下指定Maven内存参数有没有办法通过命令行将jvm args传递给maven?, 如何为Junit单元测试设置JVM参数? )触及这个主题但是没有人能找到我想要的答案。

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. 我想将属性java.util.logging.config.class设置为某个值,但我不想设置MAVEN_OPTS环境变量。

I can configure the surefire plugin in my pom file with the property: 我可以使用属性在我的pom文件中配置surefire插件:

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

so that it is set every time the test phase is run. 这样每次运行测试阶段时都会设置它。

However, if I remove the setting from the pom file and add the following to the command line: 但是,如果我从pom文件中删除该设置并将以下内容添加到命令行:

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

then the following error in the test phase is reported and the build fails: 然后报告测试阶段中的以下错误,并且构建失败:

Error: Could not find or load main class java.util.logging.config.class=someClass 错误:无法找到或加载主类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: 然后在构建开始时报告以下错误,但构建和测试成功:

Logging configuration class "someClass" failed java.lang.ClassNotFoundException: someClass 记录配置类“someClass”失败java.lang.ClassNotFoundException:someClass

I don't really understand the behaviour. 我真的不明白这种行为。 Can someone enlighten me? 有人可以开导我吗?

Yes, you should have 是的,你应该有

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

Notice the addition of -D inside the argLine . 注意在argLine添加了-D

Let's explain why. 我们来解释一下原因。 argLine is indeed an attribute of the maven-surefire-plugin with the corresponding user property argLine . argLine确实是maven-surefire-plugin一个属性,具有相应的用户属性argLine This means that you can set this property directly on the command line with -DargLine . 这意味着您可以使用-DargLine直接在命令行上设置此属性。 But then, the value of that property is -Djava.util.logging.config.class=someClass . 但是,该属性的值是-Djava.util.logging.config.class=someClass This is exactly what you had when you configured the plugin in the POM directly with 这正是您在POM中直接配置插件时所拥有的

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

Additionally, when you call 另外,当你打电话

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

then you are not setting the argLine property. 那么你没有设置argLine属性。 You are adding a system property. 您正在添加系统属性。

Regarding your approach to configuring tests 关于配置测试的方法

If you want to pass in configuration to your tests, use for example a file in src/test/resources/myTestConfig.xml. 如果要将配置传递给测试,请使用src / test / resources / myTestConfig.xml中的文件。 Or use the enhanced features of Test-ng . 或者使用Test-ng的增强功能。 Your tests will have no value on the centralized build server, or those who want to run/test your code, where config values can't be changed (easily). 您的测试对集中式构建服务器或那些想要运行/测试代码的人没有任何价值,其中配置值无法更改(轻松)。

The recommended usage of command-line arguments for Maven is for configuring the Maven plugins, build environment and Java config for the build. Maven的命令行参数的推荐用法是为构建配置Maven插件,构建环境和Java配置。 Use as little features, config and plugins as possible. 尽可能使用少量功能,配置和插件。 Or you'll face a ton of issues down the line, and the tweak-all-you-want-A-standardized-build-doesn't-mean-sh*t-for-me- Gradle will feel more comfortable. 否则你会面临一吨的路线问题,以及TWEAK-所有你想-A-标准化-构建- doesn't均值-SH * T换我- 摇篮会感觉更舒服。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM