1. Introduction
1.绪论
In this tutorial, we’ll explore the different methods of executing a Java main method using Gradle.
在本教程中,我们将探讨使用Gradle执行Java main方法的不同方法。
2. Java main Method
2.java main方法
There are several ways in which we can run a Java main method with Gradle. Let us look at them closely using a simple program that prints a message to the standard output:
我们可以用Gradle运行一个Java main方法,有几种方式。让我们用一个简单的程序来仔细看看,这个程序会向标准输出端打印一条信息。
public class MainClass {
public static void main(String[] args) {
System.out.println("Goodbye cruel world ...");
}
}
3. Running with the Application Plugin
3.使用应用程序插件运行
The Application plugin is a core Gradle plugin that defines a collection of ready-to-use tasks that help us package and distribute our application.
Application插件是一个核心的Gradle插件,它定义了一系列随时可用的任务,帮助我们打包和分发我们的应用程序。
Let’s start by inserting the following in our build.gradle file:
让我们先在我们的build.gradle文件中插入以下内容。
plugins {
id "application"
}
apply plugin : "java"
ext {
javaMainClass = "com.baeldung.gradle.exec.MainClass"
}
application {
mainClassName = javaMainClass
}
The plugin automatically generates a task called run that only requires us to point it to the main class. The closure at line 9 does exactly that, which allows us to trigger the task:
该插件自动生成了一个名为run的任务,只需要我们把它指向main类。第9行的闭包正是这样做的,它允许我们触发该任务。
~/work/baeldung/tutorials/gradle-java-exec> ./gradlew run
> Task :run
Goodbye cruel world ...
BUILD SUCCESSFUL in 531ms
2 actionable tasks: 1 executed, 1 up-to-date
4. Running with the JavaExec Task
4.使用JavaExec任务运行
Next, let’s implement a custom task for running the main method with the help of the JavaExec task type:
接下来,让我们在JavaExec任务类型的帮助下,实现一个运行main方法的自定义任务。
task runWithJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
main = javaMainClass
}
We need to define the main class on line 5 and, additionally, specify the classpath. The classpath is computed from the default properties of the build output and contains the correct path where the compiled class is actually placed.
我们需要在第5行定义main类,另外还要指定classpath。classpath是从构建输出的默认属性中计算出来的,包含了编译后的类实际放置的正确路径。
Notice that in each scenario, we use the fully qualified name, including package, of the main class.
请注意,在每种情况下,我们都使用main类的完全合格名称,包括包。
Let’s run our example using JavaExec:
让我们使用JavaExec运行我们的例子。
~/work/baeldung/tutorials/gradle-java-exec> ./gradlew runWithJavaExec
> Task :runWithJavaExec
Goodbye cruel world ...
BUILD SUCCESSFUL in 526ms
2 actionable tasks: 1 executed, 1 up-to-date
5. Running with the Exec Task
5.使用Exec任务运行
Finally, we can execute our main class using the base Exec task type. Since this option offers us the possibility to configure the execution in multiple ways, let’s implement three custom tasks and discuss them individually.
最后,我们可以使用基础Exec任务类型来执行我们的main类。由于这个选项为我们提供了以多种方式配置执行的可能性,让我们来实现三个自定义任务,并分别讨论它们。
5.1. Running from the Compiled Build Output
5.1.从编译后的构建输出中运行
First, we create a custom Exec task that behaves similarly to JavaExec:
首先,我们创建一个自定义Exec任务,其行为类似于JavaExec。
task runWithExec(type: Exec) {
dependsOn build
group = "Execution"
description = "Run the main class with ExecTask"
commandLine "java", "-classpath", sourceSets.main.runtimeClasspath.getAsPath(), javaMainClass
}
We can run any executable (in this case java) and pass the necessary arguments for it to run.
我们可以运行任何可执行文件(本例中为java),并为其运行传递必要的参数。
We configure the classpath and point to our main class on line 5, and we also add a dependency to the build task on line 2. This is necessary, as we can only run our main class after it is compiled:
我们在第5行配置了classpath并指向我们的main类,我们还在第2行向build任务添加了一个依赖项。这是必要的,因为我们只能在我们的main类被编译后运行它。
~/work/baeldung/tutorials/gradle-java-exec> ./gradlew runWithExec
> Task :runWithExec
Goodbye cruel world ...
BUILD SUCCESSFUL in 666ms
6 actionable tasks: 6 executed
5.2. Running from an Output Jar
5.2.从一个输出罐中运行
The second approach relies on the jar packaging of our small application:
第二种方法是依靠我们的小程序的罐子包装。
task runWithExecJarOnClassPath(type: Exec) {
dependsOn jar
group = "Execution"
description = "Run the mainClass from the output jar in classpath with ExecTask"
commandLine "java", "-classpath", jar.archiveFile.get(), javaMainClass
}
Notice the dependency to the jar task on line 2 and the second argument to the java executable on line 5. We use a normal jar, so we need to specify the entry point with the fourth parameter:
注意第2行对jar任务的依赖性,以及第5行对java可执行文件的第二个参数。我们使用的是普通的jar,所以我们需要用第四个参数指定入口点:。
~/work/baeldung/tutorials/gradle-java-exec> ./gradlew runWithExecJarOnClassPath
> Task :runWithExecJarOnClassPath
Goodbye cruel world ...
BUILD SUCCESSFUL in 555ms
3 actionable tasks: 3 executed
5.3. Running from an Executable Output Jar
5.3.从一个可执行的输出罐中运行
The third way also relies on the jar packaging, but we define the entry point with the help of a manifest property:
第三种方式也依赖于jar包装,但我们借助manifest属性来定义入口点。
jar {
manifest {
attributes(
"Main-Class": javaMainClass
)
}
}
task runWithExecJarExecutable(type: Exec) {
dependsOn jar
group = "Execution"
description = "Run the output executable jar with ExecTask"
commandLine "java", "-jar", jar.archiveFile.get()
}
Here, we no longer need to specify the classpath, and we can simply run the jar:
这里,我们不再需要指定classpath,我们可以简单地运行jar。
~/work/baeldung/tutorials/gradle-java-exec> ./gradlew runWithExecJarExecutable
> Task :runWithExecJarExecutable
Goodbye cruel world ...
BUILD SUCCESSFUL in 572ms
3 actionable tasks: 3 executed
6. Conclusion
6.结语
In this article, we explored the various ways of running a Java main method using Gradle.
在这篇文章中,我们探讨了使用Gradle运行Java main方法的各种方法。
Out of the box, the Application plugin provides a minimally configurable task to run our method. The JavaExec task type allows us to run the main method without specifying any plugins.
开箱即用,Application插件提供了一个最小化的可配置任务来运行我们的方法。JavaExec任务类型允许我们运行main方法,而无需指定任何插件。
Finally, the generic Exec task type can be used in various combinations with the java executable to achieve the same results but requires a dependency on other tasks.
最后,通用的Exec任务类型可以与java可执行文件进行各种组合,以达到相同的效果,但需要依赖其他任务。
As usual, the source code for this tutorial is available over on GitHub.
像往常一样,本教程的源代码可在GitHub上获得,。