Run a Java Class from Maven made simple

Welcome to this tutorial on running a Java class from Maven using the Maven exec plugin without leaving the Maven environment. Let’s dive in and discover how to leverage this plugin to run Java classes seamlessly within your Maven projects.

Execute a Java Class from Maven with one liner

The maven exec plugin supports the following goals:

  • exec:exec executes programs and Java programs in a separate process.
  • exec:java executes Java programs in the same VM.

Therefore, the simplest way to run a Java class using Maven’s JVM is the following one:

mvn exec:java -Dexec.mainClass="com.sample.MainClass"  

To pass arguments to the Java main class, use -Dexec.args as follows:

mvn exec:java -Dexec.mainClass="com.sample.MainClass" -Dexec.args="arg0 arg1 arg2"   

Important: : As said, the java goal runs in the same JVM process. Therefore, any JVM option that you want to pass to the executed class requires the MAVEN_OPTS environment variable.

Adding the exec plugin in your pom.xml

If you prefer, you can add the Maven plugin into your configuration. This can be a handy solution if you want to run a Java class as part of a goal. For example, in the following example, you are binding the Java class execution with the package goal:

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>exec-maven-plugin</artifactId>
	<version>3.1.1</version>
	<executions>
	    <execution>
		<id>my-execution</id>
		<phase>package</phase>
		<goals>
		    <goal>java</goal>
		</goals>
	    </execution>
	</executions>
	<configuration>
	    <mainClass>com.example.RestClient</mainClass>
	</configuration>
</plugin>

To run the com.example.RestClient just execute:

mvn install exec:java

Managing Classpath

The maven exec plugin automatically builds the classpath using all project dependencies,
including the project build directory. That is equivalent to using:

  <arguments>
    <argument>-classpath</argument>
    <classpath/>
    <argument>com.sample.MainClass</argument>
    ...
  </arguments>

On the other hand, if you want to specify the Classpath of your Class, you need to add manually the list of dependencies:

<argument>-classpath</argument>
<classpath>
  <dependency>commons-io:commons-io</dependency>
  <dependency>commons-lang:commons-lang</dependency>
</classpath>

Advanced configuration settings

Within your configuration settings, you can specify some optional settings, such as the workingDirectory where the task is executed, the JVM arguments or environmentVariables. Here is an example:

<configuration>
  <executable>maven</executable>
  <workingDirectory>/home/jboss</workingDirectory>
  <arguments>
     <argument>-Djava.library.path=${project.build.directory}/lib</argument>
  </arguments>
  <environmentVariables>
    <LANG>en_US</LANG>
    <MYVAR>value</MYVAR> 
  </environmentVariables>
</configuration>

Also, be aware that the configuration settings will be overridden from the Command Line options:

mvn exec:exec -DargumentA=alternateA -DargumentB=alternateB

How to speed up the build of your Maven project

When you are running Java Classes from Maven, you might be interested to complete your build phase as quick as possible. There are several plugins (such as JavaDoc or CheckStyle) that might be disabled for this purpose. The following command line shows how to build a Maven project as fast as possible:

mvn -DskipTests -DskipITs -Dcheckstyle.skip -Denforcer.skip -Dpmd.skip=true -Dmaven.javadoc.skip=true -Dgwt.skipCompilation=true

Running the exec plugin within Eclipse

The problem is that the m2e plugin (which integrates Maven with Eclipse) is now aware of exec-maven-plugin. To solve it, you need to add the following plugin configuration:

   <build>
      <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>exec-maven-plugin</artifactId>
                                        <versionRange>[3.0.0,)</versionRange>
                                        <goals>
                                            <goal>java</goal>
                                            <goal>exec</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore/>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
   </build>

You can now run the Java class from Eclipse:

maven exec java class

You can learn more about Maven by checking our Apache Maven Faqs

Conclusion: In this tutorial, we have learned how to run a Java class from Maven using the exec-maven-plugin. By configuring the plugin within the pom.xml file and specifying the main class, you can effortlessly execute your Java code as part of the Maven build process. We explored various tips and hacks, such as customizing command-line arguments, including additional classpath elements, executing in a specific phase, and setting JVM options.

Found the article helpful? if so please follow us on Socials