Ant的威力在Java界無人不知,無人不曉。可惜想在Ant的build.xml中處理邏輯很不方便,幸好在Groovy界出現(xiàn)了Gant(Groovy + Ant),使我們能夠像寫普通程序那樣編寫腳本。本文講解了如何將您所擁有的Ant知識(shí)應(yīng)用到Gant中,并利用Gant大大提高開發(fā)效率。
0,安裝Groovy(
Groovy輕松入門——搭建Groovy開發(fā)環(huán)境 )
1,下載Gant,訪問http://gant.codehaus.org (目前最新版的下載地址:http://dist.codehaus.org/gant/distributions/gant-1.1.0_groovy-1.5.2.zip,該版本依賴Groovy1.5.2+)
2,安裝Gant,將zip文件中的bin目錄和lib目錄中的文件分別解壓到%GROOVY_HOME%\bin和%GROOVY_HOME%\lib下3,驗(yàn)證是否安裝成功,打開命令行,運(yùn)行‘gant’(命令本身不帶‘’),如果您看到“Cannot open file build.gant”那就說明您安裝成功了
4,小試牛刀,新建GantTest目錄,在新建的GantTest目錄下再新建build.gant和build.properties,并將下面的build.gant和build.properties內(nèi)容分別復(fù)制到您剛剛新建的文件中,保存。在GantTest目錄下運(yùn)行‘gant’命令,運(yùn)行結(jié)果如下所示:
D:\_DEV\groovy_apps\GantTest>gant
?????[echo]?running?build.gant
?????[echo]?Executing?init?target
?????[echo]?hello,?Daniel
D:\_DEV\groovy_apps\GantTest> build.gantAnt.echo(message?:?'running?build.gant')
Ant.property(file?:?'build.properties')
def?antProperty?=?Ant.project.properties
target(init?:?'init?target')?{
????echo(message?:?'Executing?init?target
')
}
target(hello?:?'say?hello?target')?{
????depends(init)
????echo(message?:?antProperty.'echo.msg')
}
setDefaultTarget(hello)
build.propertiesecho.msg=hello,?Daniel
與build.gant等同的build.xml如下所示
<?xml?version="1.0"?encoding="UTF-8"?>
<project?name="test"?default="hello">
????<echo?message="running?build.xml?which?is?equivalent?to?build.gant"/>
????<property?file="build.properties"/>
????
????<target?name="init"??description="init?target"?>?
????????<echo?message="Executing?init?target
"/>
????</target>
????
????<target?name="hello"?depends="init"?description="say?hello?target">?
????????<echo?message="${echo.msg}"/>
????</target>
</project> 順便提一下,我將build.gant,
build.properties,build.xml三個(gè)文件放于同一目錄下
在Gant中調(diào)用Ant中的task是很簡(jiǎn)單的,只要將元素名改為方法名,將屬性名和屬性值改寫為方法的參數(shù)名和參數(shù)方法,子元素改寫為子閉包(改寫子元素稍后進(jìn)行講解)即可。以上述例子為例,<
echo message="Executing init target..."/>改寫為
echo(
message: 'Executing init target...')。
下面一個(gè)例子展現(xiàn)了如何在Gant的腳本中表達(dá)邏輯,如你所看到那樣,與平常所寫代碼并無兩樣:
Ant.echo(message?:?'running?build.gant')
Ant.property(file?:?'build.properties')
def?antProperty?=?Ant.project.properties
target(init?:?'init?target')?{
????echo(message?:?'Executing?init?target
')
}
target(hello?:?'say?hello?target')?{
????depends(init)
????//echo(message?:?antProperty.'echo.msg')
????int?alt?=?new?Random().nextInt(3)
????if?(0?==?alt)?{
????????echo(message?:?'hello?world')
????}?else?if?(1?==?alt)?{
????????echo(message?:?'hello?gant')
????}?else?{
????????echo(message?:?'hello?Daniel')
????}
}
setDefaultTarget(hello)
我們也可以將一些常用的target放在一些文件中,需要時(shí)將它們引入:
比如我將常用的target放在了build.ext.gant中
target(ext?:?'ext?target')?{
????echo(message?:?"I'm?an?ext?target")
}
在需要的時(shí)候,我們通過includeTargets << new File('build.ext.gant')語句將其引入:
includeTargets?<<?new?File('build.ext.gant')
Ant.echo(message?:?'running?build.gant')
Ant.property(file?:?'build.properties')
def?antProperty?=?Ant.project.properties
def?binDir?=?'bin'
def?srcDir?=?'src'
target(init?:?'init?target')?{
????echo(message?:?'Executing?init?target
')
????
????delete(dir?:?"${binDir}")
????mkdir(dir?:?"${binDir}")
????copy?(todir?:?"${binDir}")?{
????????fileset(dir?:?"${srcDir}")?{
????????????include(name?:?"**/*.xml")
????????}
????}
}
target(hello?:?'say?hello?target')?{
????depends(init,?ext)
????//echo(message?:?antProperty.'echo.msg')
????int?alt?=?new?Random().nextInt(3)
????if?(0?==?alt)?{
????????echo(message?:?'hello?world')
????}?else?if?(1?==?alt)?{
????????echo(message?:?'hello?gant')
????}?else?{
????????echo(message?:?'hello?Daniel')
????}
}
setDefaultTarget(hello)
最后再看一下Gant調(diào)用Ant中包含子元素的task:
新建src和bin目錄,在src目錄下新建幾個(gè)xml文件以作測(cè)試之用
Ant.echo(message?:?'running?build.gant')
Ant.property(file?:?'build.properties')
def?antProperty?=?Ant.project.properties
def?binDir?=?'bin'
def?srcDir?=?'src'
target(init?:?'init?target')?{
????echo(message?:?'Executing?init?target
')
????
????delete(dir?:?"${binDir}")
????mkdir(dir?:?"${binDir}")
????copy?(todir?:?"${binDir}")?{
????????fileset(dir?:?"${srcDir}")?{
????????????include(name?:?"**/*.xml")
????????}
????}
}
target(hello?:?'say?hello?target')?{
????depends(init)
????//echo(message?:?antProperty.'echo.msg')
????int?alt?=?new?Random().nextInt(3)
????if?(0?==?alt)?{
????????echo(message?:?'hello?world')
????}?else?if?(1?==?alt)?{
????????echo(message?:?'hello?gant')
????}?else?{
????????echo(message?:?'hello?Daniel')
????}
}
setDefaultTarget(hello)
如你所看到的,上面例子中的
delete(dir?:?"${binDir}")
mkdir(dir?:?"${binDir}")
copy?(todir?:?"${binDir}")?{
????fileset(dir?:?"${srcDir}")?{
????????include(name?:?"**/*.xml")
????}
}
改寫自
<delete?dir="${binDir}"/>
<mkdir?dir="${binDir}"/>
<copy?todir="${binDir}">
???? <fileset?dir="${srcDir}">
????????? <include?name="**/*.xml"?/>
???? </fileset>
</copy>
改寫過程很簡(jiǎn)單,僅僅是將子元素改寫為子閉包就可以了。
請(qǐng)注意,build.gant本質(zhì)上就是一個(gè)groovy程序,僅僅是文件后綴改為gant罷了,所以您可以將自己所有的Groovy知識(shí)應(yīng)用到build.gant的編寫中。
如果您想進(jìn)一步學(xué)習(xí)研究Gant,請(qǐng)?jiān)L問官方網(wǎng)站:
http://gant.codehaus.org/參考文獻(xiàn):
Groovy-power automated builds with Gant - Enhance your build process with Groovy plus Ant By Klaus P. Berg附:
朝花夕拾——Groovy & Grails
posted on 2008-02-16 17:58
山風(fēng)小子 閱讀(4242)
評(píng)論(5) 編輯 收藏 所屬分類:
Groovy & Grails