Getting Started with Maven | Java Application Development | Running Java with exec-maven-plugin
This section explains the plugin configuration that lets you easily run the Java application you created.
Program execution uses a plugin called exec-maven-plugin. By adding information about the application class to run to this plugin, you can execute the program easily.
The configuration information for this plugin is written as follows. The version shown was the latest version as of December 2017.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>Specify execution class</mainClass>
</configuration>
</plugin>
The item that must be written inside the <configuration> tag is <mainClass>. This sets the class of the application to execute.
In the SampleMavenApp project created earlier, the class com.devkuma.App was the main class. Specify it in <mainClass>.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<mainClass>com.devkuma.App</mainClass>
</configuration>
</plugin>
Running the Program
Now run the program. This is very simple. Move the current directory to the project folder and run the following command.
$ mvn exec:java
This executes the class specified in <mainClass>. If you run the example SampleMavenApp project, the following output should appear.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building SampleMavenApp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ SampleMavenApp ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.703 s
[INFO] Finished at: 2017-12-13T20:59:26+09:00
[INFO] Final Memory: 7M/155M
[INFO] ------------------------------------------------------------------------
You should see the text “Hello World!” around the middle of the output. This is the result printed by running the App class. At the end, the build result “BUILD SUCCESS” is printed, confirming that the program ran correctly.