<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    First they ignore you
    then they ridicule you
    then they fight you
    then you win
        -- Mahatma Gandhi
    Chinese => English     英文 => 中文             
    隨筆-221  評(píng)論-1047  文章-0  trackbacks-0
    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.gant
    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')
    }

    setDefaultTarget(hello)

    build.properties

    echo.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
    主站蜘蛛池模板: 国产伦精品一区二区免费| 亚洲欧美综合精品成人导航| 亚洲久本草在线中文字幕| 亚洲女同成av人片在线观看| 亚洲视频在线一区二区| 久久久久国产成人精品亚洲午夜| 国产精品亚洲综合专区片高清久久久| 亚洲国产综合久久天堂| 激情综合色五月丁香六月亚洲| 丝袜熟女国偷自产中文字幕亚洲| 亚洲国产另类久久久精品黑人| 日韩va亚洲va欧洲va国产| 伊人久久综在合线亚洲2019| 亚洲永久中文字幕在线| 国产精品亚洲午夜一区二区三区| 亚洲人成色4444在线观看| 亚洲av无码无线在线观看| 无码毛片一区二区三区视频免费播放| 欧洲乱码伦视频免费国产| 成年免费a级毛片免费看无码| 免费观看男人吊女人视频| 16女性下面扒开无遮挡免费| 日韩免费a级毛片无码a∨ | 亚洲精品天堂在线观看| 亚洲欧美国产欧美色欲| 无遮挡呻吟娇喘视频免费播放| 精品多毛少妇人妻AV免费久久| 免费国产污网站在线观看| 18未年禁止免费观看| 色吊丝最新永久免费观看网站| 人人狠狠综合久久亚洲高清| 国产亚洲大尺度无码无码专线| 亚洲综合无码一区二区| 亚洲精品无码aⅴ中文字幕蜜桃| 免费一区二区三区在线视频| 最近2019中文免费字幕在线观看| 国产2021精品视频免费播放| 国产精品冒白浆免费视频| 亚洲国产精品一区二区成人片国内| 亚洲妇女水蜜桃av网网站| 黄网站色视频免费观看45分钟 |