Basics of building scripts:
Everything in Gradle is based on these two basic concepts: projects,tasks
The 1:gradle command looks for a file called Build.gradle in the current directory. We call this Build.gradle file a build script. This script defines a project and its tasks.
1 task hello{2 dolast{3 'Hello world! ' 4 }5 }
Gradle-q Hello's output: Hello world! This build script defines a separate task called Hello and adds an action. When you run Gradlehello,gradle to execute a task called Hello, That is, the action you provide is executed. This action is a closure that contains some groovy code.
What is-Q?
-Q. Represents the quite mode. It does not generate Gradle log information (logmessages), so the user can only see the output of the tasks. It makes the output clearer. You don't necessarily need to add this option.
There is a more concise approach than the Hello task we defined earlier:
1 Task Hello <<{2 'helloworld! ' 3 }
It defines a task called Hello, a task that can be executed in a closed packet. We will use this approach to define all the tasks in this guide.
To build the script code:
Use groovy in the Gradle mission
1 Task Upper << {2 'luochuang'3 " "+ somestring; 4 " Upper Case: " + somestring.touppercase (); 5 }
Original:luochuang
Upper Case:luochuang
task dependencies:
As you might imagine, you can declare dependencies between tasks.
1 Task Hello << {2 'Hello world! ' 3 }45 Task Intro (dependson:hello) << {6 " I am gradle " 7 }
Hello world!
I am gradle
Dynamic tasks:
1 4. Times {counter-2 "task$counter" << {3 "I ' m task number $counter"4 }5 }
The TASK0,TASK1,TASK2,TASK3 is created dynamically here.
Use a task that already exists:
When a task is created, it can be accessed through the API. This is not the same as ant. For example, you can create additional dependencies.
1 4. Times {counter-2 "task$counter" << {3 "I ' m task number $counter"4 }5} 6 task0.dependson Task2, TASK3
1Task Hello << {2println'Hello World'3 }4 5 hello.dofirst{6println'Hello Venus'7 }8 9 hello.dolast{Tenprintln'Hello Mars' One } A -Hello << { -println'Hello Jupiter' the}
Hello Venus
Hello World
Hello Mars
Hello Jupiter
Dofirst and Dolast can be executed many times. They can add actions at the beginning and end of the task action list. When the task executes, the actions in the action table are executed sequentially. The fourth behavior here is a simple nickname for the Dolast << operator.
Short marking method:
As you have seen in the previous example, there is a short token that can access an existing task. That is, each task can be used as a property of the build script:
1 Task Hello << {2 println ('Hello world! ' )3}45hello.dolast{6 println' Greeting from the $hello. Name task. ' 7 }
Here the name is the default property of the task, which represents the name of the current task, and here is hello. This makes the code easy to read, especially if you are using a task provided by a plug-in, such as compilation.
custom Task Properties :
Gradle allows you to define one or more default tasks in the script:
1Defaulttasks' Clean','Run'2 3Task Clean << {4println'Default cleaning!'5 }6 7Task Run << {8println'Default running!'9 }Ten OneTask Other << { Aprintln"i ' m not a default task!" -}
Default cleaning!
Default running!
Equivalent to Gradle clean run. In a multi-project build, each subproject can have its own special default task. If a subproject does not have a special default task, the default task for the parent project will be executed.
Gradle Version Build Basics