Running Java programs with the exec goal

Special treatment of some command line arguments and configuration parameters facilitate the running of Java programs in external processes.

Note: with the java mojo, there are some confusion caused by the almost duplication of functionality between the arguments and commandArgs configuration parameters.

  • If commandLineArgs is specified, it will be used as is, except for replacing %classpath with proper classpath using dependencies
  • Otherwise if the property exec.args is specified, it will be used
  • Otherwise the list of argument and classpath will be parsed and used

    See http://jira.codehaus.org/browse/MEXEC-59 for discussion.

Command line

If specified as part of the exec.args argument, the special string %classpath will be replaced by the project classpath as computed by Maven.

mvn exec:exec -Dexec.executable="java" [...] -Dexec.args="%classpath"

POM configuration

To execute Java programs, the Exec Plugin helps by allowing the <classpath> special argument:

        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>-Dmyproperty=myvalue</argument>
            <argument>-classpath</argument>
            <!-- automatically creates the classpath using all project dependencies,
                 also adding the project build directory -->
            <classpath/>
            <argument>com.example.Main</argument>
            ...
          </arguments>
        </configuration>

or if one wants to restrict the dependencies in the classpath:

        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>-Dmyproperty=myvalue</argument>
            <argument>-classpath</argument>
            <classpath>
              <dependency>commons-io:commons-io</dependency>
              <dependency>commons-lang:commons-lang</dependency>
            </classpath>
            <argument>com.example.Main</argument>
            ...
          </arguments>
        </configuration>