Fork me on GitHub

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 exec goal, there are some confusion caused by the almost duplication of functionality between the arguments and commandlineArgs configuration parameters.

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

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. Same couunts for %modulepath

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

POM configuration

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

        <configuration>
          <executable>java</executable>
          <arguments>
            <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>-classpath</argument>
            <classpath>
              <dependency>commons-io:commons-io</dependency>
              <dependency>commons-lang:commons-lang</dependency>
            </classpath>
            <argument>com.example.Main</argument>
            ...
          </arguments>
        </configuration>

In case of the modules as supported since Java9 the configuration looks like

        <configuration>
          <executable>java</executable>
          <arguments>
            <argument>--module-path</argument> <!-- or -p  -->
            <!-- automatically creates the modulepath using all project dependencies,
                 also adding the project build directory -->
            <modulepath/>
            <argument>--module</argument> <!-- or -m -->
            <argument>mymodule/com.example.Main</argument>
            ...
          </arguments>
        </configuration>