PROGRAMMING

MINTE9
REMEMBERS




Last update:   07-09-2021

Ant

Apache Ant is a Java library and and command-line tool. It 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.
 
mkdir myAntTest/
mkdir myAntTest/src # Java source files
mkdir myAntTest/build # Generated files (compiled)
mkdir myAntTest/build/jar # Java archived files
 
Projects > Add folder to project ... myAntTest/

Compile

Save class to src/messages/HelloWorld.java file, then try to compile and run.
 RunCode
package messages;

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Hello World!");
    }
}
 
cd myAntTest/

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

# Hello World!
Build    (2/3)

Build

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.
 CodeCopy
<project>

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

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

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

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

</project>
... 14 lines
˄˄˄

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!

Distribution

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

MAVEN

VSC Command panel:
 
Java: Create Java Project
    Maven create from archetype

Java: Export Jar

Questions    
20. Exceptions
19 . Apache
Last update:   07-09-2021

        A B C D E F
🔔
1/3