java 通过 Maven 命令行设置属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34716630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting properties via Maven command line
提问by ksl
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.
我对使用 Maven 时通过命令行为某些单元测试设置属性的正确方法感到困惑。有一些问题(如指定Maven的内存参数没有设置MAVEN_OPTS环境变量,有没有办法通过JVM ARGS通过命令行到Maven? ,如何设置JVM参数JUnit测试?)在这个问题上的那一抹但没有人有我正在寻找的答案。
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?
我真的不明白这种行为。有人可以启发我吗?
回答by Tunaki
Yes, you should have
是的,你应该有
mvn package -DargLine="-Djava.util.logging.config.class=someClass"
Notice the addition of -D
inside the argLine
.
注意-D
在argLine
.
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
确实是maven-surefire-plugin
具有相应用户属性的属性argLine
。这意味着您可以直接在命令行上使用-DargLine
. 但是,该属性的值为-Djava.util.logging.config.class=someClass
。这正是您在 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. You are adding a system property.
那么你没有设置argLine
属性。您正在添加系统属性。
回答by Stig
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. Or use the enhanced features of 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).
如果要将配置传递给测试,请使用例如 src/test/resources/myTestConfig.xml 中的文件。或者使用Test-ng的增强功能。您的测试在集中式构建服务器或那些想要运行/测试您的代码的服务器上没有任何价值,其中配置值无法(轻松)更改。
The recommended usage of command-line arguments for Maven is for configuring the Maven plugins, build environment and Java config for the build. 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-Gradlewill feel more comfortable.
Maven 的命令行参数的推荐用法是为构建配置 Maven 插件、构建环境和 Java 配置。使用尽可能少的功能、配置和插件。或者,您将面临大量问题,并且随心所欲调整标准化构建并不意味着 sh*t-for- me- Gradle会感觉更舒服。