1. Overview
1.概述
Sometimes, we want to execute various programs from Gradle that require input parameters.
有时,我们想从Gradle执行各种需要输入参数的程序。
In this quick tutorial, we’re going to see how to pass command-line arguments from Gradle.
在这个快速教程中,我们将看到如何从Gradle传递命令行参数。
2. Types of Input Arguments
2.输入论据的类型
When we want to pass input arguments from the Gradle CLI, we have two choices:
当我们想从Gradle CLI传递输入参数时,我们有两个选择。
- setting system properties with the -D flag
- setting project properties with the -P flag
In general, we should use project properties unless we want to customize settings in the JVM.
一般来说,我们应该使用项目属性,除非我们想在JVM中自定义设置。
Although it is possible to hijack system properties to pass our inputs, we should avoid doing this.
尽管有可能劫持系统属性来传递我们的输入,但我们应该避免这样做。
Let’s see these properties in action. First, we configure our build.gradle:
让我们来看看这些属性的实际应用。首先,我们配置我们的build.gradle。
apply plugin: "java"
description = "Gradle Command Line Arguments examples"
task propertyTypes(){
doLast{
if (project.hasProperty("args")) {
println "Our input argument with project property ["+project.getProperty("args")+"]"
}
println "Our input argument with system property ["+System.getProperty("args")+"]"
}
}
Notice we read them differently in our task.
请注意,在我们的任务中,我们对它们的解读是不同的。
We do this because project.getProperty() throws a MissingPropertyException in case our property is not defined.
我们这样做是因为project.getProperty()抛出一个MissingPropertyException,以防我们的属性未被定义。
Unlike project properties, System.getProperty() returns a null value in case the property is not defined.
与项目属性不同,System.getProperty()在属性未被定义的情况下返回null值。
Next, let’s run the task and see its output:
接下来,让我们运行这个任务,看看它的输出。
$ ./gradlew propertyTypes -Dargs=lorem -Pargs=ipsum
> Task :cmd-line-args:propertyTypes
Our input argument with project property [ipsum]
Our input argument with system property [lorem]
3. Passing Command Line Arguments
3.传递命令行参数
So far, we’ve seen just how to read the properties. In practice, we need to send these properties as arguments to our program of choice.
到目前为止,我们只看到了如何读取属性。在实践中,我们需要将这些属性作为参数发送到我们选择的程序中。
3.1. Passing Arguments to Java Applications
3.1.向Java应用程序传递参数
In a previous tutorial, we explained how to run Java main classes from Gradle. Let’s build upon that and see how we can also pass arguments.
在之前的教程中,我们解释了如何从Gradle运行Java主类。让我们在此基础上,看看我们如何也能传递参数。
First, let’s use the application plugin in our build.gradle:
首先,让我们在我们的build.gradle中使用应用程序插件。
apply plugin: "java"
apply plugin: "application"
description = "Gradle Command Line Arguments examples"
// previous declarations
ext.javaMainClass = "com.baeldung.cmd.MainClass"
application {
mainClassName = javaMainClass
}
Now, let’s take a look at our main class:
现在,让我们看一下我们的主类。
public class MainClass {
public static void main(String[] args) {
System.out.println("Gradle command line arguments example");
for (String arg : args) {
System.out.println("Got argument [" + arg + "]");
}
}
}
Next, let’s run it with some arguments:
接下来,让我们用一些参数来运行它。
$ ./gradlew :cmd-line-args:run --args="lorem ipsum dolor"
> Task :cmd-line-args:run
Gradle command line arguments example
Got argument [lorem]
Got argument [ipsum]
Got argument [dolor]
Here, we don’t use properties to pass arguments. Instead, we pass the –args flag and the corresponding inputs there.
在这里,我们不使用属性来传递参数。相反,我们在这里传递-args标志和相应的输入。
This is a nice wrapper provided by the application plugin. However, this is only available from Gradle 4.9 onward.
这是一个由应用程序插件提供的很好的封装器。然而,这只在Gradle 4.9以上版本可用。
Let’s see what this would look like using a JavaExec task.
让我们看看这将是什么样子使用JavaExec任务。
First, we need to define it in our build.gradle:
首先,我们需要在我们的build.gradle中定义它。
ext.javaMainClass = "com.baeldung.cmd.MainClass"
if (project.hasProperty("args")) {
ext.cmdargs = project.getProperty("args")
} else {
ext.cmdargs = ""
}
task cmdLineJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the main class with JavaExecTask"
classpath = sourceSets.main.runtimeClasspath
main = javaMainClass
args cmdargs.split()
}
Let’s take a closer look at what we did. We first read the arguments from a project property.
让我们仔细看看我们做了什么。我们首先从一个项目属性中读取参数。
Since this contains all the arguments as one string, we then use the split method to obtain an array of arguments.
由于这包含了所有的参数作为一个字符串,然后我们使用split方法来获得一个参数数组。
Next, we pass this array to the args property of our JavaExec task.
接下来,我们将这个数组传递给JavaExec任务的args属性。
Let’s see what happens when we run this task, passing project properties with the -P option:
让我们看看当我们运行这个任务,用-P选项传递项目属性时会发生什么。
$ ./gradlew cmdLineJavaExec -Pargs="lorem ipsum dolor"
> Task :cmd-line-args:cmdLineJavaExec
Gradle command line arguments example
Got argument [lorem]
Got argument [ipsum]
Got argument [dolor]
3.2. Passing Arguments to Other Applications
3.2.将参数传递给其他应用程序
In some cases, we might want to pass some arguments to a third-party application from Gradle.
在某些情况下,我们可能想从Gradle向第三方应用程序传递一些参数。
Luckily, we can use the more generic Exec task to do so:
幸运的是,我们可以使用更通用的Exec任务来做到这一点。
if (project.hasProperty("args")) {
ext.cmdargs = project.getProperty("args")
} else {
ext.cmdargs = "ls"
}
task cmdLineExec(type: Exec) {
group = "Execution"
description = "Run an external program with ExecTask"
commandLine cmdargs.split()
}
Here, we use the commandLine property of the task to pass the executable along with any arguments. Again, we split the input based on spaces.
这里,我们使用任务的commandLine属性来传递可执行文件和任何参数。同样,我们根据空格来分割输入。
Let’s see how to run this for the ls command:
让我们看看如何为ls命令运行这个。
$ ./gradlew cmdLineExec -Pargs="ls -ll"
> Task :cmd-line-args:cmdLineExec
total 4
drwxr-xr-x 1 user 1049089 0 Sep 1 17:59 bin
drwxr-xr-x 1 user 1049089 0 Sep 1 18:30 build
-rw-r--r-- 1 user 1049089 1016 Sep 3 15:32 build.gradle
drwxr-xr-x 1 user 1049089 0 Sep 1 17:52 src
This can be pretty useful if we don’t want to hard-code the executable in the task.
如果我们不想在任务中对可执行文件进行硬编码,这可能相当有用。
4. Conclusion
4.总结
In this quick tutorial, we saw how to pass input arguments from Gradle.
在这个快速教程中,我们看到了如何从Gradle传递输入参数。
First, we explained the types of properties we can use. Although we can use system properties to pass input arguments, we should prefer project properties instead.
首先,我们解释了我们可以使用的属性的类型。尽管我们可以使用系统属性来传递输入参数,但我们应该更喜欢项目属性。
Then, we explored different approaches for passing command-line arguments to Java or external applications.
然后,我们探索了将命令行参数传递给Java或外部应用程序的不同方法。
As usual, the complete code can be found over on GitHub.
像往常一样,完整的代码可以在GitHub上找到超过。