■
EclipseJdt
EclipseJdt { //定义一个clos的数组 def clos = [] //定义fp这个文件参数(.factorypath文件) def fp = file('.factorypath') //把outputs的file定义为fp所指的文件 //这样如果.factorypath文件存在的时候,EclipseJdt就不会被重复执行 outputs.file fp clos += { //clos这个闭包的参数为fp.withWriter fp.withWriter { //定义domaJar这个参数,寻找配置中以doma-2开头 def domaJar = configurations.compile.find { it.name.startsWith('doma-2') } //增加了一行EXTJAR new groovy.xml.MarkupBuilder(it).factorypath() { factorypathentry(kind:'EXTJAR', id:domaJar, enabled:true, runInBatchMode:false) } } } //定义一个prefs的方法 def prefs = { name, contents -> //定义了一个文件的参数f def f = file(".settings/$name") //把contents的内容多行分解成string,赋值给f.text clos += { f.text = contents.stripMargin() } //输出内容到f这个参数定义的文件中去 outputs.file f } //调用prefs这个方法,org.eclipse.jdt.apt.core.prefs这个文件里面,写下面四行内容。 //第一行的“\”是什么意思→表示这行没有换行 prefs 'org.eclipse.jdt.apt.core.prefs', """\ |eclipse.preferences.version=1 |org.eclipse.jdt.apt.aptEnabled=true |org.eclipse.jdt.apt.genSrcDir=${aptDir} |org.eclipse.jdt.apt.reconcileEnabled=true |""" doLast { clos*.run() } }
增量式构建
为一个Task定义输入(inputs)和输出(outputs),在执行该Task时,如果它的输入和输出与前一次执行时没有变化,
那么Gradle便会认为该Task是最新的(日志会输出“UP-TO-DATE“),因此不会重复执行
task combineFileContent { def sources = fileTree('sourceDir') def destination = file('destination.txt') inputs.dir sources // 将sources声明为该Task的inputs outputs.file destination // 将destination声明为outputs doLast { destination.withPrintWriter { writer -> sources.each {source -> writer.println source.text } } } }
当首次执行combineFileContent时,Gradle会完整地执行该Task,但是紧接着再执行一次,命令行显示:
如果修改inputs(上述即sourceDir文件夹)中的任何一个文件或删除destination.txt,再次调用“gradle combineFileContent”时,该Task又会重新执行
在这个例子中inputs是目录,outputs是文件。
当然也可以inputs是文件,outputs是目录。
理解doLast
doLast意思是定义一个行为(映射Gradle中的Action类),放在当前task的最后,类似的,还有doFirst, 表示将定义的行为放在当前task最前面,例如
task hello { doLast{ println "Hello world" } doFirst{ println "I am xxx" } }
执行gradle hello, 将输出 "I am xxx" "Hello world"