includeTargetsの制限と,Antタスク相当のことをGantでやる方法

gant-user mlで面白い投稿を見つけたのでメモ。


コードと実行例を見た方が早いので,ぺったり貼っておく。

--- a.gant ---
import gant.Gant

includeTargets << new File('b.gant')

target(default : '') {
  echo "test1 start"
  test_b()
//  call_test_b1()  // メソッドは includeTargetsでは呼べない
  call_test_b2()

  -- Gantオブジェクトを作れば,Antタスク相当のことができる。
  gant = new Gant('c.gant')
  gant.processTargets('test_c')
  echo "test1 end"
}


--- b.gant ---
target(test_b : '') {
  echo "test_b start"
  call_test_b1()   // 同一ファイル内だったらメソッドもおk
  call_test_b2()
  echo "test_b end"
}

def call_test_b1() {
  Ant.echo "call test_b1"
}

call_test_b2 = { ->
  Ant.echo "call test_b2"
}


--- c.gant ---
target(test_c : '') {
  echo "test_c"
}

実行例は以下のとおり。

> gant -f a.gant
     [echo] test1 start
     [echo] test_b start
     [echo] call test_b1
     [echo] call test_b2
     [echo] test_b end
     [echo] call test_b2
     [echo] test_c
     [echo] test1 end


ちなみに'Gant(String)'は最近出来たようで,Gant v1.0.2では使えなかった(Gant v1.1.1ではOK)。
それでもAntタスクみたいなことができるのであれば,ますますもってAntの出番は無くなりそうだ。:-)