2021年1月22日 星期五

[ Gradle 文章收集 ] Passing Command Line Arguments in Gradle

 Source From Here

Overview
Sometimes, we want to execute various programs from Gradle that require input parameters. In this quick tutorial, we’re going to see how to pass command-line arguments from Gradle.

Types of Input Arguments
When we want to pass input arguments from the Gradle CLI, we have two choices:
* 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. 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:
  1. apply plugin: "java"  
  2. description = "Gradle Command Line Arguments examples"  
  3.   
  4. task propertyTypes(){  
  5.     doLast{  
  6.         if (project.hasProperty("args")) {  
  7.             println "Our input argument with project property ["+project.getProperty("args")+"]"  
  8.         }  
  9.         println "Our input argument with system property ["+System.getProperty("args")+"]"  
  10.     }  
  11. }  
Notice we read them differently in our task.

We do this because of project.getProperty() throws a MissingPropertyException in case our property is not defined. Unlike project properties, System.getProperty() returns a null value in case the property is not defined. Next, let’s run the task and see its output:
# gradle propertyTypes -Dargs=john -Pargs=bob
...
:propertyTypes
Our input argument with project property [bob]
Our input argument with system property [john]


BUILD SUCCESSFUL

Total time: 2.719 secs

Passing Command Line Arguments
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.

Passing Arguments to Java Applications
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.

First, let’s use the application plugin in our build.gradle:
  1. apply plugin: "java"  
  2. apply plugin: "application"  
  3. description = "Gradle Command Line Arguments examples"  
  4.   
  5. // previous declarations  
  6.   
  7. ext.javaMainClass = "com.baeldung.cmd.MainClass"  
  8.   
  9. application {  
  10.     mainClassName = javaMainClass  
  11. }  
Now, let’s take a look at our main class:
- src/Hello.java
  1. public class Hello{  
  2.     public static void main(String args[]){  
  3.         System.out.println("Gradle command line arguments example");  
  4.         for (String arg: args) {  
  5.             System.out.println("Got argument [" + arg + "]");  
  6.         }  
  7.     }  
  8. }  
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.

This is a nice wrapper provided by the application plugin. However, this is only available from Gradle 4.9 onward.

Let’s see what this would look like using a JavaExec task. First, we need to define it in our build.gradle:
ext.javaMainClass = "com.baeldung.cmd.MainClass"
  1. if (project.hasProperty("args")) {  
  2.     ext.cmdargs = project.getProperty("args")  
  3. else {   
  4.     ext.cmdargs = ""  
  5. }  
  6. task cmdLineJavaExec(type: JavaExec) {  
  7.     group = "Execution"  
  8.     description = "Run the main class with JavaExecTask"  
  9.     classpath = sourceSets.main.runtimeClasspath  
  10.     main = javaMainClass  
  11.     args cmdargs.split()  
  12. }  
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. Next, we pass this array to the args property of our JavaExec task. Let’s see what happens when we run this task, passing project properties with the -P option:
# gradle cmdLineJavaExec -Pargs="Nice to meet you"
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:jar UP-TO-DATE
:cmdLineJavaExec
Gradle command line arguments example
Got argument [Nice]
Got argument [to]
Got argument [meet]
Got argument [you]

BUILD SUCCESSFUL

Total time: 0.618 secs

Passing Arguments to Other Applications
In some cases, we might want to pass some arguments to a third-party application from Gradle. Luckily, we can use the more generic Exec task to do so:
  1. if (project.hasProperty("args")) {  
  2.     ext.cmdargs = project.getProperty("args")  
  3. else {   
  4.     ext.cmdargs = "ls"  
  5. }  
  6.   
  7. task cmdLineExec(type: Exec) {  
  8.     group = "Execution"  
  9.     description = "Run an external program with ExecTask"  
  10.     commandLine cmdargs.split()  
  11. }  
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.

Let’s see how to run this for the ls command:
# gradle cmdLineExec -Pargs="ls -hl"
:cmdLineExec
total 4.0K
-rw-r--r--. 1 root root 987 Jan 22 04:22 build.gradle
drwxr-xr-x. 6 root root 68 Jan 22 03:38 out
drwxr-xr-x. 2 root root 24 Jan 22 04:03 src

BUILD SUCCESSFUL

Total time: 0.609 secs


Supplement
Gradle: Running a jar file

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...