Create a custom task
Open the build.gradle
file and add the following to the end:
println "1"
task howdy {
println "2"
doLast {
println "Howdy"
}
}
println "3"
This will show you some information about how Gradle script work. Run it with the following command:
./gradlew howdy
You will see (omit some extra lines):
> Configure project :
1
2
3
> Task :howdy
Howdy
Here, Configure project
the task will be to build and run the generated script. In Gradle to perform Configure project
a task, it performs the following actions:
It hit first println
and Printing "1"
it is found to perform howdy
the task definition block, a closure, and displays "2." Note that it does not perform doLast
close operation, and therefore have not been printed "Howdy".
It continues executing the script until the fourth println
, then print "3."
So far, the build script itself is used to configure the build environment. The next step is to perform all the tasks specified in the command line, in this case howdy
the task.
This is the task.doLast{}
place to perform the block, so you'll see the words "Howdy" in the output.
doLast
It is another name for the block; it really means is similar to the "task operation", while the external block is the task configuration.
task howdy {
// 始终在初始构建脚本配置期间执行
doLast {
// 仅在任务本身被调用时执行
}
// 始终在初始构建脚本配置期间执行
}
Use Graovy DSL according to various methods Gradle document defines the tasks are as follows:
task taskName
task taskName { configure closure }
task taskName(type: SomeType)
task taskName(type: SomeType) { configure closure }
Just to temper, "Configuring closure" when running the build script immediately, and in the doLast
implementation of the closure defined in the configuration when the closure specialized tasks.
Add a second custom task to build.gradle
file:
task partner {
println "4"
doLast {
println "Partner"
}
}
println "5"
If you ./gradlew partner
see:
> Configure project :
1
2
3
4
5
> Task :partner
Partner
If you want a custom task depend on another task how to do? This is simple. build.gradle
After defining the two custom task, add the following line somewhere in the file.
partner.dependsOn howdy
And run:./gradlew partner
...
> Task :howdy
Howdy
> Task :partner
Partner
You can also use the task properties express similar relationship finalizedBy. If dependsOn line replaced with:
howdy.finalizedBy partner
And /gradlew howdy
run: .
...
> Task :howdy
Howdy
> Task :partner
Partner
You get the same output. Of course, they express different relationships.
About the task last point: In practice, you rarely write custom tasks for things such as "Howdy Partner" and the like (I find it hard to believe, I know). In fact, you'll usually cover the type of task you have defined. For example, Gradle defines a Copy to copy files from one location to another task.
This is a copy of the document to the sample build targets:
task copyDocs(type: Copy) {
from 'src/main/doc'
into 'build/target/doc'
}
When you realize that build.gradle
when the file is actually a Groovy script, you can use Groovy and Gradle real function, if desired, you can execute arbitrary code to filter and convert these files.
The following task to convert each copy of the file and exclude .DS_Store
files. DSL is very flexible. You can use from
and a plurality of blocks excludes
, such as may be performed exclusively or rename the file contains the file operation or the like. Check the "copy" Document task again to get a more complete idea.
task copyDocs(type: Copy) {
from 'src/main/doc'
into 'build/target/doc'
eachFile { file ->
doSomething(file);
}
exclude '**/.DS_Store'
}
I am most attention in the Gradle Jar War or mission is responsible for packing .jar
and .war
files for final distribution of tasks. Like Copy
tasks, they have the ability to customize a very open process, which may be of great help for the need to customize the final product of the project. In fact, you can use Gradle DSL complete control over all aspects of the packaging process.
Spring Boot plug bootJar
and bootWar
tasks inherited from Jar
and War
mission, they include all the configuration options, including the ability to filter and modify files and the ability to customize the list of replication.
Rise piece is over, if we are interested are welcome to view the official website Gradle API documentation, very helpful.
- Solemnly declare : Articles prohibit third parties (except Tencent cloud) reproduce, publish, what was going on Test nest, seven original copy my home also pull the black, your conscience will not hurt?
Technology Featured articles
- java line of code print Heart
- Linux performance monitoring software netdata Chinese Version
- Interface test code coverage (jacoco) Sharing Scheme
- Performance testing framework Second Edition
- How delightful performance testing Linux command line interface
- Brain HTTP illustrated in FIG.
- The swagger document automatically become the test code
- Five lines of code to build static blog
- On the basis rectilinear frame interface test the java
Non-technical Selected Articles
- Why software testing as a career path?
- Wrote thinking of all programming
- Be a good test automation engineer 7 steps
- Seven kinds of skills become automated testing
- Automated testing life cycle
- How to introduce automated testing DevOps
- Web end automated test failure reasons Summary
- How to introduce automated testing DevOps
- How testers to become agents of change
- Writing skills test cases