Install and Build with Apache ANT on RHEL/CentOS 7.6 - CentLinux


Monday, 17 December 2018

Install and Build with Apache ANT on RHEL/CentOS 7.6


Apache ANT (Another Neat Tool) is a Java Library and Command-line tool to automate software build processes. It is mostly used by open source community to compile and build Java applications. Apache ANT is platform independent. It uses a XML file build.xml to create script for the build process. The same build.xml file can be used on any platform to build the Java application.

In this article, we will install Apache ANT on RHEL/CentOS 7.6 machine and create a build.xml file to compile and build a simple Java application.


This Article Provides:


System Specification:

Apache ANT does not have any special system requirements. We are using a Linux machine with following specification.

  • Hostname -
  • IP Address -
  • Operating System - Red Hat Enterprise Linux (RHEL) 7.6


Install Apache ANT on RHEL/CentOS 7.6:

Connect to using ssh.

Install Apache ANT package with yum command.

[root@ant-server ~]# yum install -y ant Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. Resolving Dependencies --> Running transaction check ---> Package ant.noarch 0:1.9.4-2.el7 will be installed --> Processing Dependency: java-devel >= 1.5.0 for package: ant-1.9.4-2.el7.noarch --> Processing Dependency: xerces-j2 for package: ant-1.9.4-2.el7.noarch --> Processing Dependency: xml-commons-apis for package: ant-1.9.4-2.el7.noarch --> Running transaction check ---> Package java-1.8.0-openjdk-devel.x86_64 1: will be installed --> Processing Dependency: java-1.8.0-openjdk(x86-64) = 1: for package: 1:java-1.8.0-openjdk-devel- ---> Package xerces-j2.noarch 0:2.11.0-17.el7_0 will be installed --> Processing Dependency: xalan-j2 >= 2.7.1 for package: xerces-j2-2.11.0-17.el7_0.noarch --> Processing Dependency: xml-commons-resolver >= 1.2 for package: xerces-j2-2.11.0-17.el7_0.noarch --> Processing Dependency: osgi(org.apache.xml.resolver) for package: xerces-j2-2.11.0-17.el7_0.noarch --> Processing Dependency: osgi(org.apache.xml.serializer) for package: xerces-j2-2.11.0-17.el7_0.noarch ---> Package xml-commons-apis.noarch 0:1.4.01-16.el7 will be installed --> Running transaction check ---> Package java-1.8.0-openjdk.x86_64 1: will be installed ---> Package xalan-j2.noarch 0:2.7.1-23.el7 will be installed ---> Package xml-commons-resolver.noarch 0:1.2-15.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: ant noarch 1.9.4-2.el7 localyum 2.0 M Installing for dependencies: java-1.8.0-openjdk x86_64 1: localyum 251 k java-1.8.0-openjdk-devel x86_64 1: localyum 9.8 M xalan-j2 noarch 2.7.1-23.el7 localyum 1.9 M xerces-j2 noarch 2.11.0-17.el7_0 localyum 1.1 M xml-commons-apis noarch 1.4.01-16.el7 localyum 227 k xml-commons-resolver noarch 1.2-15.el7 localyum 108 k Transaction Summary ================================================================================ Install 1 Package (+6 Dependent packages) Total download size: 15 M Installed size: 47 M Downloading packages: -------------------------------------------------------------------------------- Total 195 MB/s | 15 MB 00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:java-1.8.0-openjdk- 1/7 Installing : xml-commons-apis-1.4.01-16.el7.noarch 2/7 Installing : xml-commons-resolver-1.2-15.el7.noarch 3/7 Installing : xalan-j2-2.7.1-23.el7.noarch 4/7 Installing : xerces-j2-2.11.0-17.el7_0.noarch 5/7 Installing : 1:java-1.8.0-openjdk-devel- 6/7 Installing : ant-1.9.4-2.el7.noarch 7/7 Verifying : xml-commons-apis-1.4.01-16.el7.noarch 1/7 Verifying : 1:java-1.8.0-openjdk-devel- 2/7 Verifying : xml-commons-resolver-1.2-15.el7.noarch 3/7 Verifying : 1:java-1.8.0-openjdk- 4/7 Verifying : ant-1.9.4-2.el7.noarch 5/7 Verifying : xalan-j2-2.7.1-23.el7.noarch 6/7 Verifying : xerces-j2-2.11.0-17.el7_0.noarch 7/7 Installed: ant.noarch 0:1.9.4-2.el7 Dependency Installed: java-1.8.0-openjdk.x86_64 1: java-1.8.0-openjdk-devel.x86_64 1: xalan-j2.noarch 0:2.7.1-23.el7 xerces-j2.noarch 0:2.11.0-17.el7_0 xml-commons-apis.noarch 0:1.4.01-16.el7 xml-commons-resolver.noarch 0:1.2-15.el7 Complete!

Apache ANT requires Java Development Kit (JDK) therefore, it has installed the available version of OpenJDK from yum repository.


Create a Java Project:

We will first create a simple Java application and then compile and build the JAR by using Java commands.

Let's create required directory structure for our project.

[root@ant-server ~]# mkdir -p ~/TestApp/{src/test,build/classes,build/jar}


src/test directory is used for keeping the source code of our Java Application i.e. TestApp.
build/classes directory is used for place the compiled Java classes.
build/jar directory is used to generate JAR (Java Archive) from the compiled Java classes.

Check the directory structure.

[root@ant-server ~]# cd TestApp [root@ant-server TestApp]# ls build src [root@ant-server TestApp]#

Create a file in src/test directory.

[root@ant-server TestApp]# vi src/test/

and write some Java code here.

package test; public class TestApp { public static void main(String[] args) { System.out.println("Testing Apache Ant..."); } }

Compile and check for any possible errors.

[root@ant-server TestApp]# javac -sourcepath src -d build/classes src/test/ [root@ant-server TestApp]# ls build/classes/ test [root@ant-server TestApp]# ls build/classes/test/ TestApp.class

Our code has been successfully compiled.

Now, execute the compiled application.

[root@ant-server TestApp]# java -cp build/classes/ test.TestApp Testing Apache Ant...

Since, our application is running fine, we can now create a JAR file from compiled classes.

To create a JAR, we need to create a JAR manifest for our Java Project.

[root@ant-server TestApp]# echo "Main-Class:test.TestApp" > mymanifest

Create a JAR file now.

[root@ant-server TestApp]# jar cvfm build/jar/test.jar mymanifest -C build/classes/ . added manifest adding: test/(in = 0) (out= 0)(stored 0%) adding: test/TestApp.class(in = 434) (out= 299)(deflated 31%)

Execute JAR file.

[root@ant-server TestApp]# java -jar build/jar/test.jar Testing Apache Ant...

We have successfully compiled our Java code and then build a JAR file by using Java commands.


Compile and Build With Apache ANT:

Now, we will compile the same application by using Apache ANT commands.

Create an Apache ANT Build File build.xml for our project.

[root@ant-server TestApp]# vi build.xml

Define some basic Targets and Tasks therein.

<project default="run"> <target name="clean"> <delete dir="build" /> </target> <target name="compile" depends="clean"> <mkdir dir="build/classes" /> <javac srcdir="src" destdir="build/classes" includeantruntime="yes" /> </target> <target name="jar" depends="compile"> <mkdir dir="build/jar" /> <jar destfile="build/jar/test.jar" basedir="build/classes"> <manifest> <attribute name="Main-Class" value="test.TestApp" /> </manifest> </jar> </target> <target name="run" depends="jar"> <java jar="build/jar/test.jar" fork="true" /> </target> </project>

Run the Project using Apache ANT.

[root@ant-server TestApp]# ant Buildfile: /root/TestApp/build.xml clean: [delete] Deleting directory /root/TestApp/build compile: [mkdir] Created dir: /root/TestApp/build/classes [javac] Compiling 1 source file to /root/TestApp/build/classes jar: [mkdir] Created dir: /root/TestApp/build/jar [jar] Building jar: /root/TestApp/build/jar/test.jar run: [java] Testing Apache Ant... BUILD SUCCESSFUL Total time: 1 second

We have successfully compiled and build our Java application by using Apache ANT commands.

Here, we used a RHEL/CentOS 7.6 machine, but the same build.xml file can be used to compile and build our Java application on any other platform.

No comments:

Post a Comment