LoginSignup
1
2

More than 5 years have passed since last update.

Antターゲット覚え書き

Last updated at Posted at 2017-07-20

実行可能なjarを作るために簡単なbuild.xmlを書いたので備忘録
version : 1.9.6

クラスパス

lib配下の全てのjarを指定

<path id="classpath">
    <fileset dir="${lib.dir}" includes="*.jar" />
</path>

コンパイル

クラスファイル置くディレクトリを削除

<target name="clean">
    <delete dir="${classes.dir}" />
</target>

上記のcleanをdependsに指定してるので、clean後にコンパイル

<target name="compile" depends="clean">
    <mkdir dir="${classes.dir}" />
    <!-- classpathrefを追加 -->
    <javac includeAntRuntime="false" srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath" encoding="utf-8"/>
</target>
項目 説明
includeAntRuntime Antのライブラリを参照するか
srcdir ソース
destdir クラスファイルの保存先
classpathref 上記で指定したクラスパスのpath id を指定

jarに入れる環境別の設定ファイルを指定

プロジェクトに設定ファイルを複数作る
今回は検品用と本番用を用意

antビルド実行時に引数を渡して、どのファイルをjarに入れるか指定する
以下、実行コマンド

検品

ant -Dtarget=it

本番

ant -Dtarget=prd

コマンドの引数チェック

"it"か"prd"じゃない場合はエラーとなる

<target name="check">
    <fail message="target property is invalid">
            <condition>
                <not>
                    <or>
                        <equals arg1="${target}" arg2="it" />
                        <equals arg1="${target}" arg2="prd" />
                    </or>
                </not>
            </condition>
        </fail>

        <condition property="env.it" value="true">
            <equals arg1="${target}" arg2="it" />
        </condition>

        <condition property="env.prd" value="true">
            <equals arg1="${target}" arg2="prd" />
        </condition>
    </target>

コマンドの引数に応じてjarに入れる設定ファイルのパスを決める

<target name="it-environment" if="env.it">
    <!-- "it" の場合はitフォルダ配下のファイルをjarに入れる -->
    <property name="resources.dir" value="${src.dir}/resources/it" />
</target>

<target name="prd-environment" if="env.prd">
        <!-- "prd" の場合はprdフォルダ配下のファイルをjarに入れる -->
    <property name="resources.dir" value="${src.dir}/resources/prd" />
</target>

jarを作る

<!-- dependsに指定してるターゲットを実行してからjar作成開始 -->
<target name="createjar" depends="check,it-environment,prd-environment,compile">
    <jar destfile="./project.jar" basedir="${build.dir}">
        <manifest>
            <attribute name="Main-Class" value="メインクラスのパス" />
        </manifest>
        <fileset dir="${resources.dir}" includes="**/*.*" />
    </jar>
</target>
項目 説明
basedir ビルド処理を行うベースディレクトリ
destdir jarの保存先
manifest attributeにメインクラスを指定
fileset 環境別の設定ファイルを、今回作成するjarの直下に入れる

参考文献

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2