Complete Guide to Executing Java Programs with Maven

Nov 23, 2025 · Programming · 5 views · 7.8

Keywords: Maven | exec-maven-plugin | Java program execution

Abstract: This article provides a comprehensive guide on using Maven's exec-maven-plugin to execute Java programs. Starting from basic configuration, it explains how to trigger program execution via command line and how to bind execution to specific phases of the Maven build lifecycle. By comparing traditional Makefile approaches with Maven methods, it helps readers understand the core principles of Maven plugin mechanisms, offering practical configuration examples and best practices.

Fundamental Principles of Maven Program Execution

In software development, it is often necessary to execute specific Java programs during the build process. The traditional Makefile approach uses the command mvn exec:java -Dexec.mainClass="org.dhappy.test.NeoTraverse" to run programs, but this method is less elegant and difficult to integrate into standard build workflows. Maven offers a more standardized solution.

Basic Configuration of exec-maven-plugin

To execute a Java program in Maven, you first need to configure the exec-maven-plugin in the project's pom.xml file. A basic configuration example is as follows:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4.0</version>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

This configuration defines the main class to be executed as org.dhappy.test.NeoTraverse. After configuration, you can run the program directly from the command line using mvn exec:java.

Triggering Execution via Command Line

Once the plugin is configured, the simplest way to execute the program is by directly invoking it from the command line:

mvn exec:java

Maven will automatically read the mainClass setting from the configuration file and execute the corresponding Java program. This approach is suitable for ad-hoc program execution needs.

Binding to Build Lifecycle

To integrate program execution into the standard build process, you can bind the exec-maven-plugin goal to a specific phase of the Maven build lifecycle. Maven's default lifecycle includes phases such as validate, compile, test, package, verify, install, and deploy.

The following example demonstrates how to bind program execution to the package phase:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4</version>
  <executions>
    <execution>
      <id>my-execution</id>
      <phase>package</phase>
      <goals>
        <goal>java</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <mainClass>org.dhappy.test.NeoTraverse</mainClass>
  </configuration>
</plugin>

In this configuration, the <execution> element defines the specific execution settings. <id> provides a unique identifier for the execution, <phase> specifies the lifecycle phase to bind to (here, package), and <goals> specifies the goal to execute (here, java).

When you run the mvn package command, Maven will automatically execute the configured Java program during the package phase. This ensures that program execution becomes an integral part of the build process.

Configuration Details and Best Practices

The exec-maven-plugin configuration is highly flexible, supporting various parameters beyond the basic mainClass:

For version selection, it is recommended to use a newer stable version (e.g., 1.4.0) for better features and performance. Additionally, ensure that the plugin version is compatible with your Maven version.

Comparison with Traditional Approaches

Compared to directly using the -Dexec.mainClass parameter in the command line, configuring the plugin in pom.xml offers several advantages:

By properly configuring the exec-maven-plugin, you can significantly enhance the standardization and maintainability of your build process.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.