Gradle Task入门演示二
https://docs.gradle.org/current/userguide/more_about_tasks.html
有多种方式可以定义task,现在再来看下面这几种方式,
task(helloTask) << {
println "hello"
}
task的名字helloTask,还可以使用引号,
task(‘helloTask2‘) << {
println "hello"
}
还可以使用tasks.create 定义一个task。
tasks.create(name: ‘helloTask3‘) << {
println ‘hello‘
}
定位task
task printTaskPath << {
println tasks.getByPath(‘hello‘).path
println tasks.getByPath(‘:hello‘).path
}
执行task输出,
:hello
:hello
配置task。如下声明和定义类型为Copy的task,同时定义myCopy的属性。
Copy myCopy = task(myCopy, type: Copy) myCopy.from ‘resources‘ myCopy.into ‘target‘ myCopy.include(‘**/*.txt‘, ‘**/*.xml‘, ‘**/*.properties‘)
上面这种方式时最平常的方式来定义属性,另外一种方式,这种方式使用闭包。
task myCopy(type: Copy)
myCopy {
from ‘resources‘
into ‘target‘
include(‘**/*.txt‘, ‘**/*.xml‘, ‘**/*.properties‘)
}
还有如下方式使用闭包定义属性,
task copy(type: Copy) {
from ‘resources‘
into ‘target‘
include(‘**/*.txt‘, ‘**/*.xml‘, ‘**/*.properties‘)
}
使用mustRunAfter来定义task的执行顺序,如下,
task pre << {
println ‘pre‘
}
task after << {
println ‘after‘
}
after.mustRunAfter pre
执行task,
? Gradle-Showcase gradle -q after pre pre after
可以看到task是按照 mustRunAfter定义的顺序执行的。
当定义相同名称的task时,可以使用overwrite重写前一个task。
task copy(type: Copy)
task copy(overwrite: true) << {
println(‘I am the new one.‘)
}
=====END=====
原文:http://my.oschina.net/xinxingegeya/blog/509760