Apache Ant is a Java library and and command-line tool.
# Apache Ant allows you to compile, assemble, test and run Java applications.

sudo apt update
sudo apt install ant

ant -version
We want to separate the source from the generated files.
# Add a Simple Text new project.
# Projects > Add folder to project ... myAntTest/

mkdir myAntTest/
mkdir myAntTest/src # Java source files
mkdir myAntTest/build # Generated files (compiled)
mkdir myAntTest/build/jar # Java archived files


Save class to src/messages/ file.
package messages;

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World!");
Compile and run.
cd myAntTest/

javac -sourcepath src/ -d build/classes/ src/messages/
java -cp build/classes messages.HelloWorld # -cp class path

# Hello World!


Compile end run using Ant (create a startable jar file).
echo Main-Class: messages.HelloWorld>myManifest

jar cfm build/jar/HelloWorld.jar myManifest -C build/classes .
java -jar build/jar/HelloWorld.jar

# Hello World!
By default Ant uses build.xml as the name for a buildfile.
# Save the following code to myAntTest/build.xml file


    <target name="clean">
        <delete dir="build"/>

    <target name="compile">
        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>

    <target name="jar">
    <mkdir dir="build/jar"/>
    <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes">
           <attribute name="Main-Class" value="messages.HelloWorld"/>

    <target name="run">
        <java jar="build/jar/HelloWorld.jar" fork="true"/>


Run archive

Now you can compile, package and run the application.
ant compile jar run

# Building jar

# Hello World!
java -jar build/jar/HelloWorld.jar

# Hello World!


Build and copy file to dist/ folder (build.xml)
<target name="-post-jar">
    <copy file="${basedir}/src/myproject/" 

  Last update: 297 days ago