1. Overview

In this short tutorial, we’re going to see how to run arbitrary main methods from any Java class using Maven.

2. The exec-maven-plugin

Let’s suppose we have the following class:

public class Exec {

    private static final Logger LOGGER = LoggerFactory.getLogger(Exec.class);

    public static void main(String[] args) {
        LOGGER.info("Running the main method");
        if (args.length > 0) {
            LOGGER.info("List of arguments: {}", Arrays.toString(args));
        }
    }
}

And we want to execute its main method from the command line via Maven.

In order to do this, we can use the exec-maven-plugin. To be more specific, the exec:java goal from this plugin executes the supplied Java class with the enclosing project’s dependencies as the classpath.

To execute the main method of the Exec class, we have to pass the fully qualified name of the class to the plugin:

$ mvn compile exec:java -Dexec.mainClass="com.baeldung.main.Exec"
02:26:45.112 INFO com.baeldung.main.Exec - Running the main method

As shown above, we’re using the exec.mainClass system property to pass the fully qualified class name.

Also, we have to make sure that the classpath is ready before running the main method. That’s why we’re compiling the source code before executing the main method.

We can achieve the same thing with plain java and javac. However, this can be cumbersome when we’re working with a pretty large classpath. On the contrary, when using this plugin, Maven automatically takes care of populating the classpath.

3. Passing Arguments

It’s also possible to pass arguments from the command line to the main method. In order to do that, we can use the exec.args system property:

$ mvn compile exec:java -Dexec.mainClass="com.baeldung.main.Exec" \
  -Dexec.args="First Second"
02:31:08.235 INFO com.baeldung.main.Exec - Running the main method
02:31:08.236 INFO com.baeldung.main.Exec - List of arguments: [First, Second]

As shown above, we’re passing a space-separated list of arguments. Moreover, we can use a comma-separated list of arguments via the exec.arguments system property:

$ mvn compile exec:java -Dexec.mainClass="com.baeldung.main.Exec" \ 
  -Dexec.arguments="Hello World,Bye"
02:32:25.616 INFO com.baeldung.main.Exec - Running the main method
02:32:25.618 INFO com.baeldung.main.Exec - List of arguments: [Hello World, Bye]

These two options can be useful when we want to use the delimiter (space or comma) in the argument itself.

4. Custom Configuration

We can also explicitly declare the plugin dependency in our pom.xml. This way, we can use custom and default configurations.

For instance, we can specify a default main class in the plugin’s configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <mainClass>com.baeldung.main.Exec</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

Now if we don’t specify the fully qualified name of the desired class, com.baeldung.main.Exec will be used:

$ mvn compile exec:java
02:33:14.197 INFO com.baeldung.main.Exec - Running the main method

However, it’s still possible to override this default configuration via an explicit exec.mainClass system property.

Moreover, we can also specify default program arguments in our configuration:

<configuration>
    <mainClass>com.baeldung.main.Exec</mainClass>
    <arguments>
        <argument>First</argument>
        <argument>Second</argument>
    </arguments>
</configuration>

This way we won’t need to pass these arguments on the command line:

$ mvn clean compile exec:java
02:34:24.448 INFO com.baeldung.main.Exec - Running the main method
02:34:24.450 INFO com.baeldung.main.Exec - List of arguments: [First, Second]

In addition to these configurations, there are plenty more available which are covered in the official documentation.

5. Conclusion

In this short article, we saw how to run the main methods from the command line via exec-maven-plugin.

As usual, all the examples are available over on GitHub.


« 上一篇: Maven日志选项