ユーザ用ツール

サイト用ツール


ant

ANT

実行可能なjarを作る

jar

環境変数

パス指定などに環境変数を埋め込める

</target>
  <mkdir dir="${user.home}/デスクトップ/hoge"/>
  <copy file="fuga.jar" todir="${user.home}/デスクトップ/hoge"></copy>	
</target>
  • ${user.home} - ユーザのホームディレクトリ
  • ${user.dir} - アプリケーションの実行ディレクトリ

この辺の環境変数はlog4jでも使える。

別のtargetを呼ぶ

targetから別のtarget呼ぶ、方法は2通り、 - depends属性 targetが依存する別のtarget名を指定、実行を指定したターゲットより先に実行される - antcall要素 target要素中の任意の場所で指定する

<target name="hoge" depends="fuga">
  <antcall target="piyo">
  <echo message="ほげ">
  <antcall target="piyo">
</target>
 
<target name="fuga">
  <echo message="ふが">
</target>
 
<target name="piyo">
  <echo message="ぴよ">
</target>

$ ant hogeの結果

ふが
ぴよ
ほげ
ぴよ

depends属性に設定するのはプロパティ値の設定など依存するtargetの実行。antcall は互いに依存・影響しない独立したビルドを同時に行いたい場合に使う。

<target description="コンフィグを変えた2つビルドを同時に行う" name="build_all">
  <antcall target="build_1" />
  <antcall target="build_2" />
</target>
 
 
<target name="build_1" description="1つめのビルド" depends="config_1, create_jar" />
<target name="build_2" description="2つめのビルド" depends="config_2, create_jar" />
 
 
<target name="config_1" description="1つめのビルド用の設定値をセット">
  <property name="build.config" value="hogehoge" />
</target>
<target name="config_2" description="2つめのビルド用の設定値をセット">
  <property name="build.config" value="fugafuga" />
</target>
 
 
<target description="ソースのコンパイル" name="compile">
  <javac><!-- 省略 --></javac>
</target>
 
 
<target description="jarの作成" name="create_jar" depends="compile">
  <!-- 設定値がないときは失敗させる -->
  <fail unless="build.config" />
 
  <jar destfile="library_${build.config}.jar"><!--省略 --></jar>
</target>

antcallで呼び出されるタスク同士は独立しているので、別のantcallで設定されたプロパティ値は引き継がれない。

<target name="antcalltest">
  <property name="hoge" value="antcall呼び出し元で設定したプロパティは引き継がれる" />
  <antcall target="build_1"/>
  <antcall target="build_2"/>
</target>
 
<target name="build_1">
    <echo message="hoge = ${hoge}" />
    <property name="fuga" value="antcallの中で設定したプロパティは別のantcallには影響しない" />
    <echo message="fuga = ${fuga}" />
</target>
 
<target name="build_2">
    <echo message="hoge = ${hoge}" />
    <echo message="fuga = ${fuga}" />
</target>

$ ant antcalltest の結果

test:
build_1:
     [echo] hoge = antcall呼び出し元で設定したプロパティは引き継がれる
     [echo] fuga = antcallの中で設定したプロパティは別のantcallには影響しない
build_2:
     [echo] hoge = antcall呼び出し元で設定したプロパティは引き継がれる
     [echo] fuga = ${fuga}
ant.txt · 最終更新: 2009/09/03 03:09 by 127.0.0.1