使用 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のインストール
$ 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」の設定
export JAVA_HOME=/usr/java/jdk1.7.0_55/bin/java
export PATH=$JAVA_HOME/bin:$PATH
    alternativesの設定
$ 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

$ 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」へ登録します。
ANT_HOME=/path/to/ant
export PATH=$PATH:$ANT_HOME/bin

再登录后,我们会检查一下设置。

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

Java的构建

    Javaのプロジェクトを作成します。
$ mkdir -p projects/sample/src/main/java
$ mkdir -p projects/sample/src/test/java
$ cd projects/sample
    HelloWrold.javaを実装します。
public class HelloWorld {

  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}
    antの設定ファイル 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をコンパイルします。
$ 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を実行します。
$ java -cp target/classes HelloWorld
Hello, World!
    ライブラリ(commons-lang)を追加します。
$ 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を修正します。
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 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をコンパイルします。
$ 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を実行します。
$ 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の生成)を行います。
$ 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
    パッケージを確認します。
$ 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

 

    Commons Lang
广告
将在 10 秒后关闭
bannerAds