13

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.

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.

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.

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:

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:

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

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

asked Jan 11, 2016 at 8:02

2 Answers 2

27

Yes, you should have

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

Notice the addition of -D inside the 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>-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.

answered Jan 11, 2016 at 8:07
Sign up to request clarification or add additional context in comments.

Comments

1

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).

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-Gradle will feel more comfortable.

answered Jan 11, 2016 at 8:25

2 Comments

Wouldn't a config file be overkill here? I just want to set the java.util.logging.config.class property to control the logging output from a 3rd party library that my application depends on.
If a command-line argument is for "just getting stuff to work on your computer", go for it. But if that code is intended to run somewhere else; you'll have to supply pretty detailed instructions for adding the command line options. Even placing such config in the pom is a smell. I've not solved this particular problem before, but a logging-shim like SLF4J might lift the problems out of heterogenous logging frameworks.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.