Install Apache Ant, Maven And Tomcat On CentOS 6.5

by
Share this Article: Facebook265Google+0Twitter12LinkedIn5Reddit2StumbleUpon1

In this Handy tutorial, let us see how to install Apache siblings Ant, Maven and Tomcat on CentOS 6.5 server. These three siblings can be used while building Java based applications. This how-to will work on all RPM based distributions such as RHEL/Scientific Linux 6.x and Fedora.

Prerequisites

Before installing the Apache siblings, make sure you have installed Java JDK on your system. To install Java JDK on CentOS, RHEL, Scientific Linux, refer the following link.

The installation of Apache Ant, Maven and Tomcat are almost identical and very easy.

Install Apache Ant

From the Ant homepage,

Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.

Ant is written in Java. Users of Ant can develop their own “antlibs” containing Ant tasks and types, and are offered a large number of ready-made commercial or open-source “antlibs”. Ant is extremely flexible and does not impose coding conventions or directory layouts to the Java projects which adopt it as a build tool.

Let us download the latest version from the project download page. At the time of writing this article, the stable Ant version is 1.9.4. For the purpose of this tutorial, I will be using Apache Ant binary file in zip format.

wget http://apache.tradebit.com/pub//ant/binaries/apache-ant-1.9.4-bin.zip

Extract it using command:

unzip apache-ant-1.9.4-bin.zip

Move the extracted contents to /opt/ directory.

mv apache-ant-1.9.4/ /opt/ant

Make a symlink to ant/bin folder as shown below.

ln -s /opt/ant/bin/ant /usr/bin/ant

Setup ANT environment variable

Create a file called ant.sh under /etc/profile.d/ directory.

vi /etc/profile.d/ant.sh

Add the following contents:

#!/bin/bash
ANT_HOME=/opt/ant
PATH=$ANT_HOME/bin:$PATH
export PATH ANT_HOME
export CLASSPATH=.

Save and close the file. Make it executable using the following command.

chmod +x /etc/profile.d/ant.sh

Then, set the environment variables permanently by running the following command:

source /etc/profile.d/ant.sh

Log out or reboot your system.

Now, check the ant version using command:

ant -version

Sample output:

Apache Ant(TM) version 1.9.4 compiled on April 29 2014

Check the path and environment variables:

echo $ANT_HOME

Sample output:

/opt/ant

Or

echo $PATH

Sample output:

/usr/lib/qt-3.3/bin:/usr/java/jdk1.8.0_05/bin:/opt/ant/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Install Maven

From the Apache maven project Homepage,

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

Let us download the latest version from the project download page. At the time of writing this article, the stable Ant version is 3.2.1. For the purpose of this tutorial, I will be using Apache Maven binary file in zip format.

wget http://www.motorlogy.com/apache/maven/maven-3/3.2.1/binaries/apache-maven-3.2.1-bin.zip

Extract it using command:

unzip apache-maven-3.2.1-bin.zip

Move the extracted contents to /opt/ directory.

mv apache-maven-3.2.1/ /opt/maven

Make a symlink to maven/bin folder as shown below.

ln -s /opt/maven/bin/mvn /usr/bin/mvn

Setup Maven environment variable

Create a file called maven.sh under /etc/profile.d/ directory.

vi /etc/profile.d/maven.sh

Add the following contents:

#!/bin/bash
MAVEN_HOME=/opt/maven
PATH=$MAVEN_HOME/bin:$PATH
export PATH MAVEN_HOME
export CLASSPATH=.

Save and close the file. Make it executable using the following command.

chmod +x /etc/profile.d/maven.sh

Then, set the environment variables permanently by running the following command:

source /etc/profile.d/maven.sh

Log out or reboot your system.

Now, check the ant version using command:

mvn -version

Sample output:

Apache Maven 3.2.1 (ea8b2b07643dbb1b84b6d16e1f08391b666bc1e9; 2014-02-14T23:07:52+05:30)
Maven home: /opt/maven
Java version: 1.8.0_05, vendor: Oracle Corporation
Java home: /usr/java/jdk1.8.0_05/jre
Default locale: en_IN, platform encoding: UTF-8
OS name: "linux", version: "2.6.32-431.1.2.0.1.el6.i686", arch: "i386", family: "unix"

Check the environment variables:

echo $MAVEN_HOME

Sample output:

/opt/maven

Or

echo $PATH

Sample output:

/usr/lib/qt-3.3/bin:/opt/maven/bin:/usr/java/jdk1.8.0_05/bin:/opt/ant/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Install Tomcat

From the Apache Tomcat project home page,

Apache Tomcat is an open source software implementation of the Java Servlet and JavaServer Pages technologies. The Java Servlet and JavaServer Pages specifications are developed under the Java Community Process. Apache Tomcat is developed in an open and participatory environment and released under the Apache License version 2. Apache Tomcat is intended to be a collaboration of the best-of-breed developers from around the world. Apache Tomcat powers numerous large-scale, mission-critical web applications across a diverse range of industries and organizations.

First, download the latest version from the project download page. At the time of writing this article, the stable Tomcat version is 7.0.53. For the purpose of this tutorial, I will be using Apache Tomcat binary zip file.

wget http://www.gaidso.com/apache/tomcat/tomcat-7/v7.0.53/bin/apache-tomcat-7.0.53.zip

Extract it using command:

unzip apache-tomcat-7.0.53.zip

Move the extracted contents to /opt/ directory.

mv apache-tomcat-7.0.53/ /opt/tomcat

Setup Tomcat environment variable

Create a file called tomcat.sh under /etc/profile.d/ directory.

vi /etc/profile.d/tomcat.sh

Add the following contents:

#!/bin/bash
CATALINA_HOME=/opt/tomcat
PATH=$CATALINA_HOME/bin:$PATH
export PATH CATALINA_HOME
export CLASSPATH=.

Save and close the file. Make it executable using the following command.

chmod +x /etc/profile.d/tomcat.sh

Then, set the environment variables permanently by running the following command:

source /etc/profile.d/tomcat.sh

Now, we have to start the Tomcat server. Before starting Tomcat, give executable permission to the following script files.

chmod +x $CATALINA_HOME/bin/startup.sh
chmod +x $CATALINA_HOME/bin/shutdown.sh
chmod +x $CATALINA_HOME/bin/catalina.sh

Start Tomcat server by running the following command from your Terminal.

cd $CATALINA_HOME/bin
./startup.sh

Sample output:

Using CATALINA_BASE:   /opt/tomcat
Using CATALINA_HOME:   /opt/tomcat
Using CATALINA_TMPDIR: /opt/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_05
Using CLASSPATH:       /opt/tomcat/bin/bootstrap.jar:/opt/tomcat/bin/tomcat-juli.jar
Tomcat started.

As you see in the above output, the Tomcat server has been started. Now, open up the browser and navigate to http://ip-address:8080/.

Make sure you’ve allowed the Tomcat default port 8080 through iptables.

To do that, edit file /etc/sysconfig/iptables,

vi /etc/sysconfig/iptables

Add the following line:

[...]
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
[...]

Save and close the file and restart the iptables service.

service iptables restart

Now, navigate to http://ip-address:8080 from your web browser. You should see the following screen.

Apache Tomcat-7.0.53 - Mozilla Firefox_001

Voila! Congratulations! The Tomcat server is up and working.

To stop the server, just enter the following commands:

cd $CATALINA_HOME/bin
./shutdown.sh

Autostart Tomcat

We haven’t finished yet. The Tomcat server won’t start after the system reboots. You have to make it to start automatically on every reboot. It’s not that difficult to do. Just follow the steps given below.

Create a file called tomcat under /etc/init.d/ directory.

vi /etc/init.d/tomcat

Copy/paste the following lines in it. Replace the JAVA and Tomcat paths and environment variables with your own.

#!/bin/sh
# chkconfig: 2345 80 20

# Description: Tomcat Start/Shutdown script

export JAVA_HOME=/usr/java/jdk1.8.0_05

case $1 in
start)
cd /opt/tomcat/bin/
./startup.sh
;;
stop)
cd /opt/tomcat/bin/
./shutdown.sh
;;
restart)
cd /opt/tomcat/bin/
./shutdown.sh
cd /opt/tomcat/bin/
./startup.sh
;;
esac
exit 0
Save and close the file. Set executable permission to the above file.

chmod a+x /etc/init.d/tomcat

Run the following command to add the tomcat service.

chkconfig --add tomcat

NB: Add double dash(–) in front of add.

Now, start the tomcat service and make it to start automatically as shown below.

service tomcat start
chkconfig tomcat on

That’s all now. We have successfully installed the Apache siblings Ant, Maven, and Tomcat.

Cheers!

For questions please refer to our Q/A forum at : http://ask.unixmen.com/

Share this Article: Facebook265Google+0Twitter12LinkedIn5Reddit0StumbleUpon1