Ant
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
Compile
Save class to src/messages/HelloWorld.java 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/HelloWorld.java
java -cp build/classes messages.HelloWorld # -cp class path
# Hello World!
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
<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>
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)
<target name="-post-jar">
<copy file="${basedir}/src/myproject/config.properties"
tofile="${dist.dir}/config.properties">
</target>
Last update: 496 days ago