Ant – Javaプロジェクトの作成方法
このチュートリアルでは、Antビルドツールを使用してJavaプロジェクトを管理し、コンパイルし、Jarファイルにパッケージ化する方法を示します。
使用される技術:
-
Eclipse 4.2
-
Ant 1.9.4
-
JDK 1.7
1. Javaプロジェクトを作成する
Eclipse IDEで、「AntDateUtils」という名前の新しいJavaプロジェクトを作成します。

2. Javaソースコード
現在の日付を出力する新しいJavaクラスを作成します。
src/com/example/core/utils/DateUtils.java
package com.example.core.utils;
import java.util.Date;
public class DateUtils {
public static void main(String[] args) {
System.out.println(getLocalCurrentDate());
}
private static Date getLocalCurrentDate() {
return new Date();
}
}
3. build.xml
プロジェクトのルートフォルダに新しいbuild.xmlを作成し、コメントを読んで説明します。
build.xml
Create a Java Project (JAR) with Ant build script
4. Antビルドスクリプト
完了、Antのコマンドをいくつか試してください
4.1 Compile the source code
$ ant compile
build.xml
4.2 Package the project into an executable Jar file
$ ant dist
build.xml
4.3 Delete folders
$ ant clean
build.xml
4.4 If no options, the default target will be executed, in this example, the default target is main
build.xml
...
$ ant
出力
Buildfile: /Users/example/Documents/workspace/AntDateUtils/build.xml
clean:
[delete] Deleting directory /Users/example/Documents/workspace/AntDateUtils/bin
[delete] Deleting directory /Users/example/Documents/workspace/AntDateUtils/dist
init:
[mkdir] Created dir: /Users/example/Documents/workspace/AntDateUtils/bin
compile:
[javac] Compiling 1 source file to /Users/example/Documents/workspace/AntDateUtils/bin
dist:
[mkdir] Created dir: /Users/example/Documents/workspace/AntDateUtils/dist
[jar] Building jar: /Users/example/Documents/workspace/AntDateUtils/dist/DateUtils-20141030.jar
main:
BUILD SUCCESSFUL
Total time: 1 second
最終的なディレクトリ構造

5. Test
5.1 Run a class inside a Jar file.
$ pwd /Users/example/Documents/workspace/AntDateUtils $ java -cp dist/DateUtils-20141030.jar com.example.core.utils.DateUtils Thu Oct 30 17:39:21 MYT 2014
5.2 Run the executable Jar file
$ pwd /Users/example/Documents/workspace/AntDateUtils $ java -jar dist/DateUtils-20141030.jar Thu Oct 30 17:40:21 MYT 2014
ソースコードをダウンロード
ダウンロード–AntDateUtils.zip(6 KB)