4

Maven's 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 it is non-obvious how to do this.

Note: I want to pass properties to the JVM, not arguments to the application. Ideally, I would like to be able to specify these in the pom.xml, but I realise that is probably unlikely due to the startup of Maven. As a workaround, an exec:exec target that sets all the classpaths etc as if I called exec:java would be good.

fommil
  • 5,757
  • 8
  • 41
  • 81

3 Answers3

4

From the usage 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>

The additional JVM options have to be set in env variable MAVEN_OPTS

MAVEN_OPTS=-ea 
Luigi R. Viggiano
  • 8,659
  • 7
  • 53
  • 66
  • thanks, setting logging through `MAVEN_OPTS` or the `java:exec` configuration doesn't work. This might be to do with the strange startup process that is used by the Java Logger. Basically, the property *must* be defined as soon as the JVM is started. I don't know why `MAVEN_OPTS` doesn't work in this case. – fommil Jan 11 '13 at 14:10
0

The use of exec:java will not work as desired, even if systemProperties is used to pass in java.util.logging.config.file, as LogManager has already been configured for maven and exec:java executes your class in process.

Using exec:exec works as expected, and the magic argument <classpath/> mirrors exec:java experience:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>-ea</argument>
            <argument>-classpath</argument>
            <!-- uses dependencies and build directory -->
            <classpath/>
            <argument>-Djava.util.logging.config.file=${basedir}/logging.properties</argument>
            <argument>org.example.Logging/argument>
          </arguments>
        </configuration>
      </plugin>

For completness:

handlers=java.util.logging.ConsoleHandler

.level= INFO
java.util.logging.ConsoleHandler.level = ALL

org.example=FINE

For completeness:

package org.example;

import java.io.File;
import java.util.logging.Logger;
import java.util.logging.Level;

public class Logging {

    static final Logger LOGGER = Logger.getLogger("org.example");
    
    public static void main(String args[]) {       
        LOGGER.info("Welcome to Logging Example");
        LOGGER.config("Checking java.util.logging.config.file");
        if( System.getProperties().containsKey("java.util.logging.config.file") ){
            File config = new File(System.getProperty("java.util.logging.config.file"));
            LOGGER.config("java.util.logging.config.file="+config);
        }
        LOGGER.fine("Everything is fine...");
        LOGGER.finer("Everything is finer...");
        LOGGER.finest("Everything is finest...");
    }
}
Jody Garnett
  • 503
  • 3
  • 10
-1

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 the properties.

String loggingValue = MyPropertyManager.LOGGING.getPropertyValue();

Then, write the MyPropertyManager class to load the properties from the property 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 just loaded once.

Scott Izu
  • 2,229
  • 25
  • 12