Thursday, November 14, 2013

Using Maven to execute a Java class with Java options

I needed to run a Maven script, explicitly including both java options and application arguments. My boss gave me this sample which makes it possible to do so via a Maven call.

Note that the goal is changed from java to exec. There is no mainClass; instead, use executable. Then make sure the argument values are included in the order they should appear in the command. Maven makes it easy to include the classpath with the classpath element.

The system properties in the systemProperties section become superfulous since they are being included earlier in the argument values.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>

            <phase>test</phase>

            <goals>
                <goal>exec</goal>
            </goals>

            <configuration>

                <!-- mainClass>jovial.Runner</mainClass -->
                <executable>java</executable>

                <arguments>
                    <argument>-classpath</argument>
                    <classpath />
                    <argument>-Dcom.mchange.v2.log.MLog=com.mchange.v2.log.log4j.Log4jMLog</argument>
                    <argument>jovial.Runner</argument>
                    <argument>${main.basedir}/jovial-config.xml</argument>
                </arguments>

                <systemProperties>
                  <systemProperty>
                    <key>com.mchange.v2.log.MLog</key>
                    <value>com.mchange.v2.log.log4j.Log4jMLog</value>
                  </systemProperty>
                </systemProperties>

            </configuration>
        </execution>
    </executions>
</plugin>

No comments:

Post a Comment