Apache AntでJavaをビルドする


## はじめに

Apache Antを使用して、Javaをビルドする方法を記述します。

## 環境

* CentOS 6.5
* jdk 1.7.0_55
* Apaache Ant 1.9.4
* commons-lang 3.3.2

## Javaの設定

* jdkのインストール

```shell
$ curl -LO -b "oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.rpm"
$ sudo yum localinstall jdk-7u55-linux-x64.rpm
$ java -version
java version "1.7.0_55"
Java(TM) SE Runtime Environment (build 1.7.0_55-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.55-b03, mixed mode)
```
* 環境変数「JAVA_HOME」及び「PATH」の設定

```bash:~/.bash_profile
export JAVA_HOME=/usr/java/jdk1.7.0_55/bin/java
export PATH=$JAVA_HOME/bin:$PATH
```

* alternativesの設定

```shell
$ sudo alternatives                                                              \
  --install /usr/bin/java java /usr/java/jdk1.7.0_55/bin/java 1700               \
  --slave /usr/bin/keytool keytool /usr/java/jdk1.7.0_55/bin/keytool             \
  --slave /usr/bin/orbd orbd /usr/java/jdk1.7.0_55/bin/orbd                      \
  --slave /usr/bin/pack200 pack200 /usr/java/jdk1.7.0_55/bin/pack200             \
  --slave /usr/bin/keytool keytool /usr/java/jdk1.7.0_55/bin/keytool             \
  --slave /usr/bin/policytool policytool /usr/java/jdk1.7.0_55/bin/policytool    \
  --slave /usr/bin/rmid rmid /usr/java/jdk1.7.0_55/bin/rmid                      \
  --slave /usr/bin/rmiregistry rmiregistry /usr/java/jdk1.7.0_55/bin/rmiregistry \
  --slave /usr/bin/servertool servertool /usr/java/jdk1.7.0_55/bin/servertool    \
  --slave /usr/bin/tnameserv tnameserv /usr/java/jdk1.7.0_55/bin/tnameserv       \
  --slave /usr/bin/unpack200 unpack200 /usr/java/jdk1.7.0_55/bin/unpack200

$ sudo alternatives --set java /usr/java/jdk1.7.0_55/bin/java
$ sudo alternatives --display java
java - status is manual.
 link currently points to /usr/java/jdk1.7.0_55/bin/java
/usr/java/jdk1.7.0_55/bin/java - priority 1700
 slave keytool: /usr/java/jdk1.7.0_55/bin/keytool
 slave orbd: /usr/java/jdk1.7.0_55/bin/orbd
 slave pack200: /usr/java/jdk1.7.0_55/bin/pack200
 slave keytool: /usr/java/jdk1.7.0_55/bin/keytool
 slave policytool: /usr/java/jdk1.7.0_55/bin/policytool
 slave rmid: /usr/java/jdk1.7.0_55/bin/rmid
 slave rmiregistry: /usr/java/jdk1.7.0_55/bin/rmiregistry
 slave servertool: /usr/java/jdk1.7.0_55/bin/servertool
 slave tnameserv: /usr/java/jdk1.7.0_55/bin/tnameserv
 slave unpack200: /usr/java/jdk1.7.0_55/bin/unpack200
Current `best' version is /usr/java/jdk1.7.0_55/bin/java.
```

## Apache Antのインストール

```shell
$ curl -LO http://ftp.yz.yamagata-u.ac.jp/pub/network/apache//ant/binaries/apache-ant-1.9.4-bin.tar.gz
$ tar zxvf apache-ant-1.9.4-bin.tar.gz
```
* antを環境変数「PATH」へ登録します。

```bash:~/.bash_profile
ANT_HOME=/path/to/ant
export PATH=$PATH:$ANT_HOME/bin
```
再ログイン後、設定を確認します。

```shell
$ ant -version
Apache Ant(TM) version 1.9.4 compiled on April 29 2014
```

## Javaのビルド

* Javaのプロジェクトを作成します。

```shell
$ mkdir -p projects/sample/src/main/java
$ mkdir -p projects/sample/src/test/java
$ cd projects/sample
```

* HelloWrold.javaを実装します。

```java:sample/src/main/java/HelloWorld.java
public class HelloWorld {

  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}
```
* antの設定ファイル build.xmlを作成します。

```xml:sample/build.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project name="sample" basedir="." default="package">

  <property name="project" value="sample" />
  <property name="version" value="0.0.1" />
  <property name="src.dir" location="src/main/java" />
  <property name="build.dir" location="target/classes" />

  <target name="echo">
    <echo message="Hello, World!" />
  </target>

  <target name="package" depends="compile">
    <jar destfile="${project}-${version}.jar" basedir="${build.dir}" />
  </target>

  <target name="compile" depends="clean">
    <mkdir dir="${build.dir}" />
    <javac includeAntRuntime="false"
      srcdir="${src.dir}"
      destdir="${build.dir}"
    />
  </target>

  <target name="clean">
    <delete dir="${build.dir}" />
    <delete file="${project}-${version}.jar" />
  </target>

</project>
```

* HelloWorld.javaをコンパイルします。

```shell
$ ant compile
Buildfile: /path/to/projects/sample/build.xml

clean:
   [delete] Deleting directory /path/to/projects/sample/target/classes
   [delete] Deleting: /path/to/projects/sample/sample-0.0.1.jar

compile:
    [mkdir] Created dir: /path/to/projects/sample/target/classes
    [javac] Compiling 1 source file to /path/to/projects/sample/target/classes

BUILD SUCCESSFUL
Total time: 1 second
```
* HelloWorldを実行します。

```shell
$ java -cp target/classes HelloWorld
Hello, World!
```
* ライブラリ(commons-lang)を追加します。

```shell
$ curl -LO http://ftp.jaist.ac.jp/pub/apache//commons/lang/binaries/commons-lang3-3.3.2-bin.tar.gz
$ tar zxvf commons-lang3-3.3.2-bin.tar.gz
$ cp commons-lang3-3.3.2/commons-lang3-3.3.2.jar /path/to/lib
```

* HelloWorldを修正します。

```java:sample/src/main/java/HelloWorld.java
import org.apache.commons.lang3.SystemUtils;

public class HelloWorld {

  public static void main(String[] args) {
    System.out.println("Hello, World!");
    System.out.println("* Java version : " + SystemUtils.JAVA_VM_VERSION);
    System.out.println("* OS name :" + SystemUtils.OS_NAME);
    System.out.println("* OS version :" + SystemUtils.OS_VERSION);
  }
}
```
* build.xmlへライブラリの記述を追加します。

```xml:sample/build.xml
<?xml version="1.0" encoding="UTF-8" ?>
<project name="sample" basedir=".">

  <property name="project" value="sample" />
  <property name="version" value="0.0.1" />
  <property name="src.dir" location="src/main/java" />
  <!-- 追加 -->
  <property name="lib.dir" location="/path/to/lib" />
  <property name="build.dir" location="target/classes" />

  <!-- 追加 -->
  <path id="classpath">
    <pathelement location="${lib.dir}/commons-lang3-3.3.2.jar" />
  </path>

  <target name="echo">
    <echo message="Hello, World!" />
  </target>

  <target name="package" depends="compile">
    <jar destfile="${project}-${version}.jar" basedir="${build.dir}" />
  </target>

  <target name="compile" depends="clean">
    <mkdir dir="${build.dir}" />
    <!-- classpathrefを追加 -->
    <javac includeAntRuntime="false"
      srcdir="${src.dir}"
      destdir="${build.dir}"
      classpathref="classpath"
    />
  </target>

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

</project>
```
* HelloWorldをコンパイルします。

```shell
$ ant compile
Buildfile: /path/to/projects/sample/build.xml

clean:
   [delete] Deleting directory /path/to/projects/sample/target/classes

compile:
    [mkdir] Created dir: /path/to/projects/sample/target/classes
    [javac] Compiling 1 source file to /path/to/projects/sample/target/classes

BUILD SUCCESSFUL
Total time: 1 second
```
* HelloWorldを実行します。

```shell
$ java -cp target/classes:/path/to/lib/commons-lang3-3.3.2.jar HelloWorld
Hello, World!
* Java version : 24.55-b03
* OS name :Linux
* OS version :2.6.32-431.el6.x86_64
```

* パッケージング(jarの生成)を行います。

```shell
$ ant
Buildfile: /path/to/projects/sample/build.xml

clean:
   [delete] Deleting directory /path/to/projects/sample/target/classes

compile:
    [mkdir] Created dir: /path/to/projects/sample/target/classes
    [javac] Compiling 1 source file to /path/to/projects/sample/target/classes

package:
      [jar] Building jar: /path/to/projects/sample/sample-0.0.1.jar

BUILD SUCCESSFUL
Total time: 1 second
```

* パッケージを確認します。

```shell
$ ls -l /path/to/porjects/sample
total 16
-rw-rw-r--. 1 admin admin 1011 Sep 26 01:07 build.xml
-rw-rw-r--. 1 admin admin 1219 Sep 26 01:26 sample-0.0.1.jar
drwxrwxr-x. 4 admin admin 4096 Sep 25 05:31 src
drwxrwxr-x. 3 admin admin 4096 Sep 26 01:26 target

$ java -cp /path/to/projects/sample/sample-0.0.1.jar:/path/to/lib/commons-lang3-3.3.2.jar HelloWorld
Hello, World!
* Java version : 24.55-b03
* OS name :Linux
* OS version :2.6.32-431.el6.x86_64
```
## 参考

* [Apache Ant](http://ant.apache.org/)
* [Commons Lang](http://commons.apache.org/proper/commons-lang/)