Setting Java Properties for `mvn exec: java`

Maven exec:java target runs in the same JVM as Maven itself.

I would like to be able to pass some properties to the java binary (in particular, -ea -Djava.util.logging.config.file=logging.properties ), but this is not obvious how to do this.

Note. I want to pass JVM properties, not arguments to the application. Ideally, I would like to list them in pom.xml , but I understand that it is probably unlikely due to the launch of Maven. As a workaround, the goal is exec:exec , which sets all the classpaths, etc., as if I were calling exec:java , would be nice.

+4
source share
2 answers

On the page :

  <configuration> <mainClass>com.example.Main</mainClass> <arguments> <argument>argument1</argument> ... </arguments> <systemProperties> <systemProperty> <key>java.util.logging.config.file</key> <value>logging.properties</value> </systemProperty> ... </systemProperties> </configuration> 

Additional JVM parameters must be set in env variable MAVEN_OPTS

 MAVEN_OPTS=-ea 
+4
source

Use the command line:

 call mvn exec:java -Dexec.classpathScope="test" -Dexec.mainClass="com.mycompany.MyFirstTest" -DPROPERTY_FILE="MyPropertyFile" 

to run your program.

Have a manager class responsible for reading in properties.

 String loggingValue = MyPropertyManager.LOGGING.getPropertyValue(); 

Then write the MyPropertyManager class to load properties from the properties file.

 public enum MyPropertyManager { LOGGING, OTHERPROPERTY, OTHER; public String getPropertyValue() { String propertyFile = System.getProperty("PROPERTY_FILE"); // ... load property file Properties loadedProperties = ..... return properties.get(LOGGING.toString()); } } 

Improve the code so that the properties file is loaded only once.

0
source

All Articles