Latest

Monday, 17 December 2018

Install and Build with Apache ANT on RHEL/CentOS 7

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 machine and create a build.xml file to compile and build a simple Java application.

 

This Article Provides:

install-and-build-with-apache-ant-on-rhel-centos-7.6

System Specification:

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

  • Hostname - ant-server.itlab.com
  • IP Address - 192.168.116.28/24
  • Operating System - Red Hat Enterprise Linux (RHEL) 7.6

 

Install Apache ANT on RHEL/CentOS 7.6:

Connect to ant-server.itlab.com using ssh.

Install Apache ANT package with yum command.

# yum install -y ant

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.

# mkdir -p ~/TestApp/{src/test,build/classes,build/jar}

Here,

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.

# cd TestApp
# ls
build  src

Create a file in src/test directory.

# vi src/test/TestApp.java

and write some Java code here.

package test;

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

Compile TestApp.java and check for any possible errors.

# javac -sourcepath src -d build/classes src/test/TestApp.java
# ls build/classes/
test
# ls build/classes/test/
TestApp.class

Our code has been successfully compiled.

Now, execute the compiled application.

# 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.

# echo "Main-Class:test.TestApp" > mymanifest

Create a JAR file now.

# 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.

# 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.

# 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.

# 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 machine, but the same build.xml file can be used to compile and build our Java application on any other platform.

If you find this article useful? Don't you mind support us by Buy Me A Coffee


No comments:

Post a Comment

© 2022 CentLinux. All Rights Reserved.