Ant - So erstellen Sie ein Java-Projekt
In diesem Tutorial zeigen wir Ihnen, wie Sie mit dem Ant-Build-Tool ein Java-Projekt verwalten, kompilieren und in eine Jar-Datei packen.
Verwendete Technologien:
-
Eclipse 4.2
-
Ant 1.9.4
-
JDK 1.7
1. Erstellen Sie ein Java-Projekt
Erstellen Sie in Eclipse IDE ein neues Java-Projekt mit dem Namen "AntDateUtils".
2. Java-Quellcode
Erstellen Sie eine neue Java-Klasse, um das aktuelle Datum auszudrucken:
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
Erstellen Sie ein neuesbuild.xml
im Projektstammordner, lesen Sie den Kommentar zur Selbsterklärung.
build.xml
Create a Java Project (JAR) with Ant build script
4. Ant-Build-Skripte
Probieren Sie ein paar Befehle von Ant aus
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
Ausgabe
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
Endgültige Verzeichnisstruktur
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
Quellcode herunterladen
Laden Sie es herunter -AntDateUtils.zip (6 KB)