Ant - Erstellen einer Jar-Datei mit externen Bibliotheken

Ant - So erstellen Sie eine Jar-Datei mit externen Bibliotheken

In diesem Tutorial zeigen wir Ihnen, wie Sie mit dem Ant-Build-Skript eine Jar-Datei erstellen und mit den externen Bibliotheken / Abhängigkeiten des Projekts arbeiten.

Verwendete Technologien:

  1. Eclipse 4.2

  2. JDK 1.7

  3. Ant 1.9.4

  4. Ameisen-Efeu 2.4

  5. logback 1.1.2

  6. Joda-Zeit 2.5

P.S Previous Ant Java project will be reused.

1. Projektstruktur

Abbildung 1.1: Die endgültige Projektverzeichnisstruktur in Eclipse IDE.

ant-external-libraries-final

2. Java Project + Externe Bibliotheken

Öffnen Sie in der Eclipse-IDE das vorherige Java-ProjektAntDateUtils erneut und aktualisieren Sie den Quellcode, umlogback undjoda-time zu verwenden.

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();

    }

}

Erstellen Sie einlogback.xml und legen Sie es im Ordnersrcdes Projekts ab. Siehe Abbildung 1.1

src/logback.xml



    
      

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

      
    

    
      
    

3. Ivy - Externe Bibliotheken abrufen

Wir verwenden Apache Ivy, um die externen Bibliotheken / Abhängigkeiten des Projekts abzurufen.

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



    
    
    
        
    

    
    
        
        
    
    

Laden Sie zum ersten Mal das Ivy-Modul aus dem Maven Center-Repository in die lokalen${user.home}/.ant/lib/ivy.jar herunter.

$ ant ivy

Führen Sie die Task "resolve" aus, um die externen Bibliotheken herunterzuladen. Die deklarierten Bibliotheken werden in den Ordner des Projektslibheruntergeladen.

$ ant resolve

4. build.xml

Überprüfen Sie das aktualisiertebuild.xml-Skript und lesen Sie die Kommentare, um sich selbst zu erklären.

Hauptpunkte :

  1. Verwalten Sie die externen Projektbibliotheken mit Apache Ivy, überprüfen Sie den Efeu-Namespace oben und führen Sie die Aufgabe „Auflösen“ aus.

  2. Um den Quellcode zu kompilieren, müssen Sie den Klassenpfad deklarieren. Überprüfen Sie das Attribut "compile" und "classpathref" der Aufgabe.

  3. Erstellt in der Aufgabe "jar" die gesamte Liste der externen Bibliotheken und fügt sie in die Dateimanifest.mfein.

  4. In der Aufgabe "jar" wird das Projekt-JAR in den Ordner "dist" gepackt und die gesamten externen Bibliotheken werden von "lib" nach "dist / lib" kopiert.

build.xml


    
        Create a Java Project (JAR) with Ant build script
    

    
    
    
    
    
    
    

    
    
    
        
    

    
    
        
        
    
    

    
        
    

    
    
        
            
            
            
        
    

    
    
        
    

    
    
        
        
            
                
                
            
        
    

    
        
            
        
    

    
    

        

        
        

        
            
                
                
            
        
    

    
        
        
    

    
    

5. Test

Testen Sie das Java-Projekt mit dem Ant-Build-Skript.

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

Quellcode herunterladen

Laden Sie es herunter -AntDateUtils-External-Libraries.zip (8 KB)