1. Overview
1.概述
In this short tutorial, we’re going to see how to run arbitrary main methods from any Java class using Maven.
在这个简短的教程中,我们将看到如何使用Maven从任何Java类中运行任意的main方法。
2. The exec-maven-plugin
2.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.
我们想通过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.
为了做到这一点,我们可以使用exec-maven-plugin。更具体地说,该插件的exec:java 目标会执行所提供的Java类,并将周围项目的依赖性作为classpath。
To execute the main method of the Exec class, we have to pass the fully qualified name of the class to the plugin:
为了执行Exec类的主方法,我们必须将该类的完全合格名称传递给插件。
$ 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.
如上所示,我们使用exec.mainClass系统属性来传递完全合格的类名。
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.
另外,我们必须确保在运行main方法之前,classpath已经准备好了。这就是为什么我们要在执行main方法之前编译源代码。
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.
我们可以用普通的java和javac实现同样的事情。使用该插件时,Maven会自动处理填充classpath的问题。
3. Passing Arguments
3.传递参数
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:
我们也可以从命令行向main方法传递参数。为了做到这一点,我们可以使用exec.argssystem属性。
$ 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:
如上所示,我们传递的是一个空格分隔的参数列表。此外,我们可以通过exec.arguments系统属性使用逗号分隔的参数列表。
$ 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
4.自定义配置
We can also explicitly declare the plugin dependency in our pom.xml. This way, we can use custom and default configurations.
我们也可以在我们的pom.xml中明确声明插件的依赖性。这样,我们就可以使用自定义和默认的配置。
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.0.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:
现在,如果我们不指定所需类的完全合格名称,com.baeldung.main.Exec将被使用。
$ 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.
然而,仍然可以通过明确的exec.mainClass系统属性来覆盖这个默认配置。
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
5.总结
In this short article, we saw how to run the main methods from the command line via exec-maven-plugin.
在这篇短文中,我们看到了如何通过exec-maven-plugin从命令行运行主要方法。
As usual, all the examples are available over on GitHub.
像往常一样,所有的例子都可以在GitHub上找到。