LoginSignup
30
32

More than 5 years have passed since last update.

Antことはじめ

Last updated at Posted at 2015-08-15

MacでAntを使ってみたのでメモ。

Apache Ant?

Javaのビルドツール。
詳細は、Wikipedia
主なAntタスクもまとまっています。

環境

  • Mac OSX 10.11 (beta)
  • java version "1.8.0_45"
  • apache ant 1.9.6

インストール

Homebrewでインストール。

$ brew install ant
$ ant -version
Apache Ant(TM) version 1.9.6 compiled on June 29 2015

環境変数も設定しておきます。
(設定していないと、コマンド実行時にエラーになる模様)

~/.zshrc
### Added manually                                                                                                                                    
export ANT_HOME="/usr/local/bin/ant/"
export PATH="$PATH:$ANT_HOME/bin"

実行してみる

定番のHello world。

build.xml

build.xmlは、プロジェクト、ターゲット、タスクの集まりになっています。
(ターゲットは、ある目的を達成する為のタスクのまとまりです。)

build.xml
<?xml version="1.0" encoding="utf-8" ?>

<project name="antsample" default="helloworld">

  <target name="helloworld">
    <echo message="Hello world!" />
  </target>

</project>

実行してみましょう。

$ ant
Buildfile: path/to/ant-sample/build.xml

helloworld:
     [echo] Hello world!

BUILD SUCCESSFUL
Total time: 0 seconds
  • ファイル名
    • "build.xml"であれば、ファイル名の指定は不要。
    • 指定する場合は、$ ant -f <filename>
  • ターゲット名
    • 指定しない場合、"default"に指定したターゲットを実行

Antタスク

Antのタスクについては、Overview of Apache Ant Tasksにまとまっています。

echo

  <target name="helloworld">
    <echo message="Hello world!" />
  </target>

mkdir & javac

classディレクトリを作成し、コンパイル。

  <target name="compile">
    <mkdir dir="./class" />
    <javac srcdir="./src" destdir="./class" />
  </target>

java

javaのクラスを実行。
以下の例では、exec実行時、compileターゲットが実行される(depends)。

  <target name="exec" depends="compile">
    <java classname="HelloWorld" classpath="${class}" />
  </target>

jar

jarファイルを作成。

  <target name="jar">                                                                                                                                                                                                                             
    <jar basedir="class" jarfile="sample.jar" />
  </target>
30
32
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
30
32