article content from Gradle official User-guide document basic content of the build script Engineering (projects) and task (tasks)
Each build script is made up of one or more projects.
A project consists of one or more tasks. a HelloWorld project
The Gradle command will look for the current directory's Build.gradle file, which is our build script.
Let's go to an empty directory, create a new Build.gradle file, and enter the following:
Task Hello {
dolast {
println ' Hello world! '
}
}
Enter gradle-q hello on the command line and you will see the output as follows:
Hello world!
-Q means quiet, that is, do not output any logs, directly output the task related content.
The above build.gradle defines a Hello task, which adds an action to the task,
The action executes a piece of code, which is the output Hello world!. When you execute gradle-q Hello,
Gradle will perform the Hello task.
a simplified task definition
Task Hello << {
println ' Hello world! '
}
The << operator is similar to the Dolast alias build script, which is actually code
Gradle's build scripts are able to use all the power of the groovy language. Such as:
Task Count {
Dolast {
4.times {print "$it"}
}
}
Gradle-q Count
0 1 2 3 dependencies between Tasks
TASKX relies on Tasky's declarative approach
Task Taskx (dependsOn: ' Tasky ') << {
println ' task X '
}
task Tasky << {
println ' task Y '
}
TASKX relies on Tasky, but Tasky has not yet declared that this is a deferred dependency (lazy dependsOn).
The output is as follows: Perform dependent tasks first
Gradle-q taskx
Task X
Task Y Dynamic Task
Dynamically create tasks with groovy
4.times {counter-a
task "Task$counter" {
dolast {
println "I ' m task number $counter"
}
}< c18/>}
TASK1-TASK4 can be created dynamically
Gradle-q TASK3
tasks that exist for the I ' m task number 3 operation
Once the task is created, it can be accessed through the API. You can add dependencies dynamically.
4.times {counter-a
task "Task$counter" {
dolast {
println "I ' m task number $counter"
}
}
}< C6/>task0.dependson Task2, Task3
Gradle-q Task0
I ' m task number 2
I ' m task number 3
I ' m task number 0 short notation
There are some handy tokens that can access an existing task. No task can be used as a property of a build script.
Task notation {
Dolast {
println ' Hello world! '
}
}
notation.dolast {
println ' this is $notation. Name task! '
}
Gradle-q notation
Hello world!
This is $notation. Name task! Custom Project Properties
You can add your own properties to the task.
Task MyProperty {
Ext.time = "Time is Now"
}
task Printmyproperty {
Dolast {
println myproperty . Time
}
}
Gradle-q Printmyproperty
Time is now using ant task
Gradle is well integrated with the ant task by simply relying on groovy. Using ant tasks through Gradle is more powerful and convenient.
The following example shows how to perform ant tasks and how to access the ant properties.
Task LoadFile {
Dolast {
def files = File ('. '). Listfiles (). Sort ()
files.each {file file,
if (File.isfile ()) {
Ant.loadfile (Srcfile:file, property: File.name)
println "* * * $file. Name * * * *"
println "${ant.properties[file.name]}"}
}}
The current directory is only build.gradle, so the output is as follows
* build.gradle *
Task LoadFile {
Dolast {
def files = File ('. '). Listfiles (). Sort ()
Files.each {File File-
if (File.isfile ()) {
Ant.loadfile (Srcfile:file, Property:file.name)
println " * $file. Name *"
Println "${ant.properties[file.name]}"
}
}
}
}
More ant operations will later describe the use of the method
Gradle define methods to organize the logic.
Task Checksum {
Dolast {
fileList ('. '). Each {File file--
ant.checksum (File:file, property: "Cs_$file.name")
println "$file. Name Checksum: ${ ant.properties["Cs_$file.name"]} "}}
}
task LoadFile {
Dolast {
fileList ('. '). Each {file file
, Ant.loadfile (Srcfile:file, property:file.name)
println "I ' m fond of $file. Name"
}
}
}
File[] FileList (String dir) {
file (dir). listfiles ({file, File.isfile ()} as FileFilter). Sort ()
}
Default Task
Gradle can define one or more tasks, and if no other tasks are specified, the default task executes.
Defaulttasks ' clean ', ' Run '
task Clean {
Dolast {
println ' Default cleaning! '
}
}
Task Run {
Dolast {
println ' Default running! '
}
}
Task Other {
Dolast {
println "I ' m not a default task!"
}
}
Gradle-q
Default cleaning!
Default running! Configuring with Dags
The Gradle has a configuration resolution period (config phase) and an execution resolution period (execution phase).
After the configuration resolution is complete, Gradle knows all the tasks that should be performed. In order to use this information, Gradle
A hook has been raised. So we can give different values to the variables.
In the following example, the value of the version variable depends on which task is executed.
Task Distribution {
Dolast {
println "We build the zip with version= $version"
}
} "
Task release ( DependsOn: ' distribution ') {
dolast {
println "We release Now"
}
}
Gradle.taskGraph.whenReady {taskgraph-
if (taskgraph.hastask (release)) {
Version = 1.0
} else { c30/>version = ' 1.0-snapshot '
}
}
The different execution results are as follows
GRADLE-Q distribution
We build the zip with Version=1.0-snapshot
GRADLE-Q Release
We build the zip with version=1.0
We Release Now
Whenready affects the release task before the release task is executed.
The next step is to introduce more detailed gradle content.