Zum Inhalt

Ant Cheatsheet

Überblick

Apache Ant ist ein Java-basiertes Build-Tool, das XML verwendet, um den Build-Prozess und seine Abhängigkeiten zu beschreiben. Es ist plattformunabhängig und entworfen, um durch Java-Klassen zu erweitern.

Installation

Paketmanager

# macOS
brew install ant

# Ubuntu/Debian
sudo apt install ant

# CentOS/RHEL
sudo yum install ant

# Windows (Chocolatey)
choco install ant

# Manual installation
wget https://archive.apache.org/dist/ant/binaries/apache-ant-1.10.12-bin.tar.gz
tar -xzf apache-ant-1.10.12-bin.tar.gz
export ANT_HOME=/path/to/apache-ant-1.10.12
export PATH=$PATH:$ANT_HOME/bin
```_

### Überprüfung
```bash
ant -version
```_

## Grundkonzepte

### Schlüsselbegriffe

Build File # XML file (usually build.xml) Project # Root element of build file Target # Set of tasks to execute Task # Unit of work (compile, copy, etc.) Property # Name-value pair for configuration


### Projektstruktur

project/ ├── build.xml ├── src/ │ ├── main/ │ │ └── java/ │ └── test/ │ └── java/ ├── lib/ ├── build/ └── dist/ ```_

Basic build.xml

Minimales Beispiel

```xml

A simple Java project

<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="$\\\\{build.dir\\\\}/classes"/>

<target name="init">
    <mkdir dir="$\\\\{build.dir\\\\}"/>
    <mkdir dir="$\\\\{classes.dir\\\\}"/>
</target>

<target name="compile" depends="init">
    <javac srcdir="$\\\\{src.dir\\\\}" destdir="$\\\\{classes.dir\\\\}"/>
</target>

<target name="clean">
    <delete dir="$\\\\{build.dir\\\\}"/>
</target>

```_

Komplettes Beispiel

```xml

Complete Java project build file

<property name="src.dir" value="src/main/java"/>
<property name="test.src.dir" value="src/test/java"/>
<property name="resources.dir" value="src/main/resources"/>
<property name="lib.dir" value="lib"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="$\\\\{build.dir\\\\}/classes"/>
<property name="test.classes.dir" value="$\\\\{build.dir\\\\}/test-classes"/>
<property name="dist.dir" value="dist"/>
<property name="jar.file" value="$\\\\{dist.dir\\\\}/$\\\\{ant.project.name\\\\}.jar"/>

<path id="classpath">
    <fileset dir="$\\\\{lib.dir\\\\}">
        <include name="*.jar"/>
    </fileset>
</path>

<path id="test.classpath">
    <path refid="classpath"/>
    <pathelement location="$\\\\{classes.dir\\\\}"/>
    <pathelement location="$\\\\{test.classes.dir\\\\}"/>
</path>

<target name="init">
    <mkdir dir="$\\\\{build.dir\\\\}"/>
    <mkdir dir="$\\\\{classes.dir\\\\}"/>
    <mkdir dir="$\\\\{test.classes.dir\\\\}"/>
    <mkdir dir="$\\\\{dist.dir\\\\}"/>
</target>

<target name="compile" depends="init">
    <javac srcdir="$\\\\{src.dir\\\\}"
           destdir="$\\\\{classes.dir\\\\}"
           classpathref="classpath"
           debug="true"
           includeantruntime="false"/>
    <copy todir="$\\\\{classes.dir\\\\}">
        <fileset dir="$\\\\{resources.dir\\\\}"/>
    </copy>
</target>

<target name="compile-tests" depends="compile">
    <javac srcdir="$\\\\{test.src.dir\\\\}"
           destdir="$\\\\{test.classes.dir\\\\}"
           classpathref="test.classpath"
           debug="true"
           includeantruntime="false"/>
</target>

<target name="test" depends="compile-tests">
    <junit printsummary="yes" haltonfailure="yes">
        <classpath refid="test.classpath"/>
        <formatter type="plain"/>
        <batchtest fork="yes" todir="$\\\\{build.dir\\\\}">
            <fileset dir="$\\\\{test.src.dir\\\\}">
                <include name="**/*Test.java"/>
            </fileset>
        </batchtest>
    </junit>
</target>

<target name="jar" depends="compile">
    <jar destfile="$\\\\{jar.file\\\\}" basedir="$\\\\{classes.dir\\\\}">
        <manifest>
            <attribute name="Main-Class" value="com.example.Main"/>
            <attribute name="Class-Path" value=". lib/"/>
        </manifest>
    </jar>
</target>

<target name="build" depends="test,jar"/>

<target name="clean">
    <delete dir="$\\\\{build.dir\\\\}"/>
    <delete dir="$\\\\{dist.dir\\\\}"/>
</target>

<target name="rebuild" depends="clean,build"/>

```_

Eigenschaften

Objektdefinition

```xml

```_

Eigentumsrechte

```properties

build.properties

version=1.0.0 src.dir=src/main/java test.dir=src/test/java lib.dir=lib build.dir=build ```_

Aufgaben

Dateioperationen

```xml

```_

Aufgaben der Erstellung

```xml

```_

Prüfaufgaben

```xml

<test name="com.example.MyTest" todir="$\\\\{test.reports.dir\\\\}"/>

<batchtest fork="yes" todir="$\\\\{test.reports.dir\\\\}">
    <fileset dir="$\\\\{test.src.dir\\\\}">
        <include name="**/*Test.java"/>
        <exclude name="**/Abstract*Test.java"/>
    </fileset>
</batchtest>

```_

Ziele und Abhängigkeiten

Zielabhängigkeiten

```xml

```_

Bedingte Ausführung

```xml

```_

Makros und benutzerdefinierte Aufgaben

Macrodefs

```xml

<attribute name="srcdir" default="src/@\\\{module\\\}/java"/> <attribute name="destdir" default="build/@\\\{module\\\}/classes"/> <mkdir dir="@\\\{destdir\\\}"/> <javac srcdir="@\\\{srcdir\\\}" destdir="@\\\{destdir\\\}" classpathref="classpath" includeantruntime="false"/>

<classpath refid="test.classpath.@\\\{module\\\}"/> ```_

Kundenspezifische Aufgaben

```xml

```_

Erweiterte Funktionen

Parallele Ausführung

```xml

```_

Teilprojekte

```xml

```_

Import und Include

```xml

<echo message="Custom compilation step"/>

```_

Verwendung der Befehlszeile

Grundlegende Befehle

```bash

Run default target

ant

Run specific target

ant compile ant clean ant test

Run multiple targets

ant clean compile test

List available targets

ant -projecthelp ant -p

Verbose output

ant -verbose compile ant -v compile

Debug output

ant -debug compile ant -d compile

Quiet output

ant -quiet compile ant -q compile ```_

Einstellungen

```bash

Set properties from command line

ant -Dversion=2.0.0 -Ddebug.enabled=true compile

Use different build file

ant -buildfile mybuild.xml compile ant -f mybuild.xml compile

Set logger

ant -logger org.apache.tools.ant.DefaultLogger compile

Find build file

ant -find build.xml compile ```_

Erweiterte Optionen

```bash

Keep going on failure

ant -keep-going build

Use input handler

ant -inputhandler org.apache.tools.ant.input.DefaultInputHandler

Set log level

ant -loglevel info compile

Disable input

ant -noinput compile

Show version

ant -version ```_

Integration

IDE Integration

```xml

<replace file=".classpath" token="@PROJECT_NAME@" value="$\\\{ant.project.name\\\}"/>

```_

CI/CD Integration

```xml

```_

Versionskontrolle

```xml

```_

Best Practices

Projektorganisation

```xml

```_

Fehlerbehebung

```xml

```_

Fehlerbehebung

Gemeinsame Themen

```bash

Check Ant installation

ant -version

Verify Java installation

java -version

Check classpath issues

ant -debug compile 2>&1|grep -i classpath

Validate build file

ant -projecthelp

Check property values

ant -Dant.echo.properties=true compile ```_

Debug Techniques

```xml

```_

Leistungsspitzen

```xml

```_

Ressourcen