task hello << { String greeting = 'hello ant' ant.echo(message:greeting) }
使用的是ant中的echo任务执行打印信息
qianhuis-Mac-mini:0112 qianhui$ gradle hello :hello [ant:echo] hello ant BUILD SUCCESSFUL Total time: 2.629 secs
上面是使用嵌套的方法传递信息个 ant,现在直接传递试试
task hello << { ant.echo("Hello ant") }
qianhuis-Mac-mini:0112 qianhui$ gradle hello :hello [ant:echo] Hello ant BUILD SUCCESSFUL Total time: 3.556 secs
你还可以个 ant任务传递闭包代码块:
task zip << { ant.zip(destfile:'archive.zip'){ fileset(dir:'src'){ include(name:'**.xml') exclude(name:'**.java') } } }
将src下的文件打包成archive.zip压缩包,只打包xml文件,不打包java文件。
先在src下随便建几个xml文件,执行命令:
qianhuis-Mac-mini:0112 qianhui$ gradle zip :zip BUILD SUCCESSFUL Total time: 2.801 secs
会在根目录下生成了一个压缩文件:
操作ant.path对象。
task list << { def path = ant.path{ fileset(dir:'libs',includes:'*.jar') } path.list().each{ println it } }
qianhuis-Mac-mini:0112 qianhui$ gradle list :list /Users/qianhui/Documents/Developer/gradle_project/0112/libs/1.jar BUILD SUCCESSFUL Total time: 2.943 secs
使用taskdef或者typedef关键字来自定义任务,与你在build.xml中是一样的效果。
task check <<{ ant.taskdef(name:'pmd', classname:'net.sourceforge.pmd.ant.PMDTask', classpath:configurations.pmd.asPath) ant.pmd(shortFilenames:'true', failonruleviolation:'true', rulesetfiles:file('pmd-rules.xml').toURI.toString()){ formatter(type:'text',toConsole:'true') fileset(dir:'src') } } configurations{ pmd } dependencies{ pmd group:'pmd',name:'pmd',version:'4.2.5' }
大家都知道ant的构建脚本是 build.xml文件,但是你不知道的是gradle可以包含该build.xml能解析里面的信息。看gradle是如何做的:
首先生成一个build.xml 文件:
<project> <target name="hello"> <echo>Hello, from Ant</echo> </target> </project>
build.gradle包含该xml文件:
ant.importBuild 'build.xml'
qianhuis-Mac-mini:0112 qianhui$ gradle hello :hello [ant:echo] Hello, from Ant BUILD SUCCESSFUL Total time: 2.775 secs
嘿,还真的可以。
既然可以将ant的任务当成gradle的任务,那么我们添加一个依赖试试:
ant.importBuild 'build.xml' task intro(dependsOn:hello) << { println 'Hello,from gradle' }
执行任务试试
qianhuis-Mac-mini:0112 qianhui$ gradle intro :hello [ant:echo] Hello, from Ant :intro Hello,from gradle BUILD SUCCESSFUL Total time: 3.726 secs
ant.importBuild 'build.xml' task intro(dependsOn:hello) << { println 'Hello,from gradle' } hello <<{ println 'Hello,from gradle' }
qianhuis-Mac-mini:0112 qianhui$ gradle hello :hello [ant:echo] Hello, from Ant Hello,from gradle BUILD SUCCESSFUL Total time: 3.704 secs
一样有效果。
我们还可以在build.xml中依赖我们在build.gradle 中定义的任务。修改build.xml文件:
<project> <target name="hello" depends="intro"> <echo>Hello, from Ant</echo> </target> </project>
修改build.gradle
ant.importBuild 'build.xml' task intro << { println 'Hello,from gradle' } hello <<{ println 'Hello,from gradle' }
qianhuis-Mac-mini:0112 qianhui$ gradle hello :intro Hello,from gradle :hello [ant:echo] Hello, from Ant Hello,from gradle BUILD SUCCESSFUL Total time: 2.713 secs
build.xml
<project> <target name="hello" > <echo>Hello, from Ant</echo> </target> </project>
build.gradle
ant.importBuild ('build.xml'){ antTargetName ->'a-'+antTargetName } task intro << { println 'Hello,from gradle' }
qianhuis-Mac-mini:0112 qianhui$ gradle a-hello :a-hello [ant:echo] Hello, from Ant BUILD SUCCESSFUL Total time: 3.631 secs