Complete Guide to Executing Java Main Methods with Maven

Nov 23, 2025 · Programming · 9 views · 7.8

Keywords: Maven | Java Execution | exec Plugin

Abstract: This article provides a comprehensive guide on using Maven's exec plugin to execute Java class main methods, covering both command-line and POM configuration approaches. It delves into Maven lifecycle and plugin integration mechanisms, offering complete configuration examples and best practices to help developers quickly master project testing and execution methods.

Background of Executing Java Main Methods with Maven

During Java project development, there is often a need to directly run the main method of a class for manual testing or quick feature verification. While Maven does not have a built-in "run" phase or goal, this requirement can be perfectly fulfilled using the exec-maven-plugin.

Overview of exec-maven-plugin

The exec-maven-plugin is an official plugin provided by the Mojohaus organization, specifically designed to execute external programs or Java classes during the Maven build process. The plugin offers two main goals: exec:java for executing Java classes and exec:exec for executing system commands.

Command-Line Execution Method

The most direct way to execute is by specifying the class to run via command-line parameters:

mvn exec:java -Dexec.mainClass="com.example.Main" -Dexec.args="argument1 argument2"

Here, the -Dexec.mainClass parameter specifies the fully qualified class name containing the main method, and -Dexec.args is used to pass command-line arguments. This method is suitable for temporary testing without modifying project configuration.

POM Configuration File Method

For projects that require frequent execution, it is recommended to configure the plugin in pom.xml:

<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>com.example.Main</mainClass>
                    <arguments>
                        <argument>argument1</argument>
                        <argument>argument2</argument>
                    </arguments>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

After configuration, simply execute mvn exec:java to run the specified class, greatly simplifying command input.

Plugin Dependency Management

The exec-maven-plugin supports automatic resolution of project dependencies, ensuring the classpath is correct during execution. The plugin uses the project's compile classpath, including all dependent jar files, which guarantees consistency between the execution environment and the actual project runtime environment.

Advanced Configuration Options

The plugin offers a rich set of configuration options:

Integration with Maven Lifecycle

The exec goal can be bound to specific phases of the Maven lifecycle to achieve automated execution. For example, automatically running a validation class after the test phase:

<executions>
    <execution>
        <phase>test</phase>
        <goals>
            <goal>java</goal>
        </goals>
    </execution>
</executions>

Best Practices and Considerations

When using exec-maven-plugin, note the following:

  1. Ensure the target class is available in the compiled classpath
  2. For complex parameter passing, using <arguments> configuration is more reliable than command-line
  3. Use cautiously in production environments to avoid unnecessary execution overhead
  4. Consider using profiles to differentiate execution configurations for different environments

Comparison with Other Tools

Compared to directly using the java command, exec-maven-plugin offers advantages such as:

Conclusion

The exec-maven-plugin provides flexible and reliable Java class execution capabilities for Maven projects, suitable for both quick testing and automated processes. Through proper configuration and usage, it can significantly enhance development efficiency and project quality.

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.