Ant - 外部ライブラリを使ってJarファイルを作成する方法

Ant –外部ライブラリを使用してJarファイルを作成する方法

このチュートリアルでは、Antビルドスクリプトを使用してJarファイルを作成し、プロジェクトの外部ライブラリ/依存関係を操作する方法を示します。

使用される技術:

  1. Eclipse 4.2

  2. JDK 1.7

  3. Ant 1.9.4

  4. アントアイビー2.4

  5. logback 1.1.2

  6. ジョーダタイム2.5

P.S Previous Ant Java project will be reused.

1. プロジェクト構造

図1.1:EclipseIDEでの最終的なプロジェクトディレクトリ構造。

ant-external-libraries-final

2. Javaプロジェクト+外部ライブラリ

Eclipse IDEで、前のJavaプロジェクトAntDateUtilsを再度開き、logbackjoda-timeを使用するようにソースコードを更新します。

src/com/example/core/utils/DateUtils.java

package com.example.core.utils;

import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateUtils {

    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    public static void main(String[] args) {

        logger.debug("[MAIN] Current Date : {}", getLocalCurrentDate());
        System.out.println(getLocalCurrentDate());

    }

    private static String getLocalCurrentDate() {

        LocalDate date = new LocalDate();
        return date.toString();

    }

}

logback.xmlを作成し、プロジェクトのsrcフォルダーに配置します。 図1.1を参照してください

src/logback.xml



    
      

        
            ANT + LogBack : %-5level %logger{36} - %msg%n
        

      
    

    
      
    

3. Ivy –外部ライブラリを取得

Apache Ivyを使用して、プロジェクトの外部ライブラリ/依存関係を取得します。

3.1 Create this file ivy.xml :

ivy.xml


    
    
        
        
        
    

3.2 Update build.xml, add ivy namespace on top, and “ivy” task to download the ivy module, and “resolve” task to ask Ivy module to download the external libraries.

build.xml



    
    
    
        
    

    
    
        
        
    
    

初めて、ivyモジュールをMavenセンターリポジトリからローカル${user.home}/.ant/lib/ivy.jarにダウンロードします。

$ ant ivy

外部ライブラリをダウンロードするには、タスク「解決」を実行します。 宣言されたライブラリは、プロジェクトのlibフォルダーにダウンロードされます。

$ ant resolve

4. build.xml

更新されたbuild.xmlスクリプトを確認し、自明のコメントを読んでください。

主なポイント:

  1. Apache Ivyを使用してプロジェクトの外部ライブラリを管理し、上部のivy名前空間を確認して、タスクを「解決」します。

  2. ソースコードをコンパイルするには、クラスパスを宣言する必要があります。 タスク「compile」および「classpathref」属性を確認します。

  3. 「jar」タスクで、外部ライブラリのリスト全体を作成し、それをmanifest.mfファイルに配置します。

  4. 「jar」タスクでは、プロジェクトjarがフォルダー「dist」にパッケージ化され、外部ライブラリ全体が「lib」から「dist / lib」にコピーされます。

build.xml


    
        Create a Java Project (JAR) with Ant build script
    

    
    
    
    
    
    
    

    
    
    
        
    

    
    
        
        
    
    

    
        
    

    
    
        
            
            
            
        
    

    
    
        
    

    
    
        
        
            
                
                
            
        
    

    
        
            
        
    

    
    

        

        
        

        
            
                
                
            
        
    

    
        
        
    

    
    

5. Test

Antビルドスクリプトを使用してJavaプロジェクトをテストします。

5.1 Jar it.

$ pwd
/Users/example/Documents/workspace/AntDateUtils

$ 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

copy-dependencies:
     [copy] Copying 12 files to /Users/example/Documents/workspace/AntDateUtils/dist/lib

jar:
     [echo] classpath.name : ... lib/joda-time-2.5.jar lib/logback-classic-1.1.2.jar lib/logback-core-1.1.2.jar lib/mail-1.4.jar ...

      [jar] Building jar: /Users/example/Documents/workspace/AntDateUtils/dist/DateUtils.jar

main:

BUILD SUCCESSFUL
Total time: 1 second

5.2 Inspects the generated jar file.

$ jar -tf dist/DateUtils.jar

META-INF/
META-INF/MANIFEST.MF
com/
com/example/
com/example/core/
com/example/core/utils/
com/example/core/utils/DateUtils.class

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.4
Created-By: 1.7.0_05-b05 (Oracle Corporation)
Main-Class: com.example.core.utils.DateUtils
Class-Path: lib/activation-1.1.jar lib/commons-compiler-2.6.1.jar lib/
 geronimo-jms_1.1_spec-1.0.jar lib/groovy-all-2.0.7.jar lib/janino-2.6
 .1.jar lib/joda-convert-1.2.jar lib/joda-time-2.5.jar lib/logback-cla
 ssic-1.1.2.jar lib/logback-core-1.1.2.jar lib/mail-1.4.jar lib/servle
 t-api-2.5.jar lib/slf4j-api-1.7.6.jar

5.3 Run the Jar file.

$ pwd
/Users/example/Documents/workspace/AntDateUtils

$ java -jar dist/DateUtils.jar

16:28:43.957 [main] DEBUG com.example.core.utils.DateUtils - [MAIN] Current Date : 2014-11-21
2014-11-21

5.4 Run the Jar file again, with logback.xml.

$ pwd
/Users/example/Documents/workspace/AntDateUtils

$ java -jar -Dlogback.configurationFile=src/logback.xml dist/DateUtils.jar

16:34:43,746 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [src/logback.xml] at [file:/Users/example/Documents/workspace/AntDateUtils/src/logback.xml]
//...

ANT + LogBack : DEBUG com.example.core.utils.DateUtils - [MAIN] Current Date : 2014-11-21
2014-11-21

ソースコードをダウンロード

ダウンロード–AntDateUtils-External-Libraries.zip(8 KB)