Projects 和 tasks
每一個gradle腳本都是由兩個部分組成的,projects和tasks
每一個gradle的構(gòu)建都由一個或者多個projects構(gòu)成。project表示什么完全取決于他依賴的gradle是干什么的。比如,一個project可能表示一個jar包或者一個web程序。
每一個project都由一個或者幾個task組成。一個task表示一些自動的構(gòu)建公做。這可能是一個compile任務(wù)來構(gòu)建一個jar包,或者產(chǎn)生一個javadoc文件。
動態(tài)task
4.times { counter ->
task "task$counter" {
doLast {
println "I'm task number $counter"
}
}
}
操作已經(jīng)存在task
4.times { counter ->
task "task$counter" {
doLast {
println "I'm task number $counter"
}
}
}
task0.dependsOn task2, task3
結(jié)果:
> gradle -q task0
I'm task number 2
I'm task number 3
I'm task number 0
增加task的行為
task hello {
doLast {
println 'Hello Earth'
}
}
hello.doFirst {
println 'Hello Venus'
}
hello.doLast {
println 'Hello Mars'
}
hello {
doLast {
println 'Hello Jupiter'
}
}
結(jié)果
> gradle -q hello
Hello Venus
Hello Earth
Hello Mars
Hello Jupiter
task的屬性
task myTask {
ext.myProperty = "myValue"
}
task printTaskProperties {
doLast {
println myTask.myProperty
}
}
結(jié)果:
> gradle -q printTaskProperties
myValue
使用ant task
task loadfile {
doLast {
def files = file('../antLoadfileResources').listFiles().sort()
files.each { File file ->
if (file.isFile()) {
ant.loadfile(srcFile: file, property: file.name)
println " *** $file.name ***"
println "${ant.properties[file.name]}"
}
}
}
}
結(jié)果:
> gradle -q loadfile
*** agile.manifesto.txt ***
Individuals and interactions over processes and tools
Working software over comprehensive documentation
Customer collaboration over contract negotiation
Responding to change over following a plan
*** gradle.manifesto.txt ***
Make the impossible possible, make the possible easy and make the easy elegant.
(inspired by Moshe Feldenkrais)
通過hook來配置task
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 {
version = '1.0-SNAPSHOT'
}
}
結(jié)果:
> gradle -q release
We build the zip with version=1.0
We release now
標(biāo)準(zhǔn)的project的屬性

額外的屬性
apply plugin: "java"
ext {
springVersion = "3.1.0.RELEASE"
emailNotification = "build@master.org"
}
sourceSets.all { ext.purpose = null }
sourceSets {
main {
purpose = "production"
}
test {
purpose = "test"
}
plugin {
purpose = "production"
}
}
task printProperties {
doLast {
println springVersion
println emailNotification
sourceSets.matching { it.purpose == "production" }.each { println it.name }
}
}
結(jié)果:
> gradle -q printProperties
3.1.0.RELEASE
build@master.org
main
plugin
配置任意的對象
task configure {
doLast {
def pos = configure(new java.text.FieldPosition(10)) {
beginIndex = 1
endIndex = 5
}
println pos.beginIndex
println pos.endIndex
}
}
結(jié)果:
> gradle -q configure
1
5
使用外部的腳本配置對象
task configure {
doLast {
def pos = new java.text.FieldPosition(10)
// Apply the script
apply from: 'other.gradle', to: pos
println pos.beginIndex
println pos.endIndex
}
}
結(jié)果
> gradle -q configure
1
5
閉包在方法中作為最后的一個參數(shù)
在gradle中,很多地方都使用到了閉包,你能在很多地方看到,當(dāng)閉包作為最后一個一個方法參數(shù)的時候,你能夠把他放在方法的后面。
repositories {
println "in a closure"
}
repositories() { println "in a closure" }
repositories({ println "in a closure" })
閉包代理
dependencies {
assert delegate == project.dependencies
testCompile('junit:junit:4.12')
delegate.testCompile('junit:junit:4.12')
}
定義task
task(hello) {
doLast {
println "hello"
}
}
task(copy, type: Copy) { //這個type是一個類型提示,可以為同樣的類型但是不同的名字的task進行配置
from(file('srcDir'))
into(buildDir)
}
task依賴
project('projectA') {
task taskX(dependsOn: ':projectB:taskY') {
doLast {
println 'taskX'
}
}
}
project('projectB') {
task taskY {
doLast {
println 'taskY'
}
}
}
結(jié)果
> gradle -q taskX
taskY
taskX
task taskX {
doLast {
println 'taskX'
}
}
task taskY {
doLast {
println 'taskY'
}
}
taskX.dependsOn taskY
結(jié)果:
> gradle -q taskX
taskY
taskX
使用閉包進行依賴
task taskX {
doLast {
println 'taskX'
}
}
taskX.dependsOn {
tasks.findAll { task -> task.name.startsWith('lib') }
}
task lib1 {
doLast {
println 'lib1'
}
}
task lib2 {
doLast {
println 'lib2'
}
}
task notALib {
doLast {
println 'notALib'
}
}
結(jié)果:
> gradle -q taskX
lib1
lib2
taskX
task的執(zhí)行順序
mustRunAfter
task taskX {
doLast {
println 'taskX'
}
}
task taskY {
doLast {
println 'taskY'
}
}
taskY.mustRunAfter taskX
結(jié)果:
> gradle -q taskY taskX
taskX
taskY
shouldRunAfter
task taskX {
doLast {
println 'taskX'
}
}
task taskY {
doLast {
println 'taskY'
}
}
taskY.shouldRunAfter taskX
結(jié)果:
> gradle -q taskY taskX
taskX
taskY
如果有task的相關(guān)依賴的話,shouldRunAfter不起作用。
task taskX {
doLast {
println 'taskX'
}
}
task taskY {
doLast {
println 'taskY'
}
}
task taskZ {
doLast {
println 'taskZ'
}
}
taskX.dependsOn taskY
taskY.dependsOn taskZ
taskZ.shouldRunAfter taskX
結(jié)果:
> gradle -q taskX
taskZ
taskY
taskX
重寫tasks
task copy(type: Copy)
task copy(overwrite: true) {
doLast {
println('I am the new one.')
}
}
結(jié)果:
> gradle -q copy
I am the new one.
使用謂詞
可以使用onlyIf()方法來作為task的一個動作。task的功能只有在此動作返回為true時才會執(zhí)行,不然不會執(zhí)行。
task hello {
doLast {
println 'hello world'
}
}
hello.onlyIf { !project.hasProperty('skipHello') }
結(jié)果:
> gradle hello -PskipHello
:hello world
BUILD SUCCESSFUL
Total time: 1 secs
使用StopExecutionException
如果這個throw new StopExecutionException()異常被拋出,那么task的邏輯將不會執(zhí)行。
task compile {
doLast {
println 'We are doing the compile.'
}
}
compile.doFirst {
// Here you would put arbitrary conditions in real life.
// But this is used in an integration test so we want defined behavior.
if (true) { throw new StopExecutionException() }
}
task myTask(dependsOn: 'compile') {
doLast {
println 'I am not affected'
}
}
Enabling and disabling tasks
task disableMe {
doLast {
println 'This should not be printed if the task is disabled.'
}
}
disableMe.enabled = false
結(jié)果:
:disableMe SKIPPED
BUILD SUCCESSFUL
Total time: 1 secs
更新到最新的檢查 Up-to-date checks
自定義任務(wù)類型:如果你實現(xiàn)了一個自定義的任務(wù)作為一個類,他只需要兩步就能進行構(gòu)建工作:
- 創(chuàng)建屬性或這個方法對你的task的輸入和輸出
- 增加合適的注解在每個屬性或者get方法上
gradle主要支持3種主要的輸入和輸出的分類:
- 簡單值:像String類型或者Number類型。更簡單的說,就是任何一種實現(xiàn)了Serialiezable的類型:
- 文件類型。Project.file(java.lang.Object)或者Project.file(java.lang.Object[])
- 內(nèi)置類型。
使用gradle.properties文件
gradle.properties:
gradlePropertiesProp=gradlePropertiesValue
sysProp=shouldBeOverWrittenBySysProp
envProjectProp=shouldBeOverWrittenByEnvProp
systemProp.system=systemValue
systemProp.https.proxyHost=www.somehost.org
build.gradle:
task printProps {
doLast {
println (gradlePropertiesProp)
println envProjectProp
println System.properties['system']
println System.properties['https.proxyHost']
//println systemProp.https.proxyHost
}
}