First they ignore you
          then they ridicule you
          then they fight you
          then you win
              -- Mahatma Gandhi
          Chinese => English     英文 => 中文             
          隨筆-221  評論-1047  文章-0  trackbacks-0
          Ant的威力在Java界無人不知,無人不曉。可惜想在Ant的build.xml中處理邏輯很不方便,幸好在Groovy界出現了Gant(Groovy + Ant),使我們能夠像寫普通程序那樣編寫腳本。本文講解了如何將您所擁有的Ant知識應用到Gant中,并利用Gant大大提高開發效率。

          0,安裝Groovy( Groovy輕松入門——搭建Groovy開發環境 )
          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,驗證是否安裝成功,打開命令行,運行‘gant’(命令本身不帶‘’),如果您看到“Cannot open file build.gant”那就說明您安裝成功了
          4,小試牛刀,新建GantTest目錄,在新建的GantTest目錄下再新建build.gant和build.properties,并將下面的build.gant和build.properties內容分別復制到您剛剛新建的文件中,保存。在GantTest目錄下運行‘gant’命令,運行結果如下所示:
          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三個文件放于同一目錄下

          在Gant中調用Ant中的task是很簡單的,只要將元素名改為方法名,將屬性名和屬性值改寫為方法的參數名和參數方法,子元素改寫為子閉包(改寫子元素稍后進行講解)即可。以上述例子為例,<echo message="Executing init target..."/>改寫為echo(message: 'Executing init target...')。

          下面一個例子展現了如何在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')
          ????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放在一些文件中,需要時將它們引入:
          比如我將常用的target放在了build.ext.gant中
          target(ext?:?'ext?target')?{
          ????echo(message?:?
          "I'm?an?ext?target")
          }
          在需要的時候,我們通過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調用Ant中包含子元素的task:
          新建src和bin目錄,在src目錄下新建幾個xml文件以作測試之用
          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>

          改寫過程很簡單,僅僅是將子元素改寫為子閉包就可以了。

          請注意,build.gant本質上就是一個groovy程序,僅僅是文件后綴改為gant罷了,所以您可以將自己所有的Groovy知識應用到build.gant的編寫中。


          如果您想進一步學習研究Gant,請訪問官方網站:http://gant.codehaus.org/


          參考文獻:
          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 山風小子 閱讀(4241) 評論(5)  編輯  收藏 所屬分類: Groovy & Grails
          主站蜘蛛池模板: 沙洋县| 延津县| 潜江市| 巍山| 噶尔县| 安义县| 都兰县| 南靖县| 治多县| 枞阳县| 万盛区| 白城市| 湘乡市| 三河市| 伊金霍洛旗| 西宁市| 伊吾县| 唐山市| 读书| 图木舒克市| 沙田区| 平利县| 庆云县| 高淳县| 北川| 自贡市| 临沭县| 兴城市| 兴仁县| 潍坊市| 溧水县| 南木林县| 平果县| 交城县| 开平市| 伊吾县| 娄烦县| 抚顺市| 南昌市| 江西省| 陆河县|