Apache Ant – Build Web Application And Deploy To Tomcat Server

 

Step 1 Create New Dynamic Web Project

  • Go to File->New->Others… Select Dynamic Web Project under Web category
  • Click Next.
  • Type AntWebDemo or whatever name you want in the Project name field.
  • Click Finish.

Step 2 Servlet Class

Create a java class that extends javax.servlet.HttpServlet class.

  • Right click to the project or src folder, select New -> Class from the menu.
  • In Java package, enter info.java.tips.servlet.
  • In Class Name, type HttpServletDemo or any name you want.
  • Click Finish.
  • Add extends HttpServlet to the class.
  • Implement doGet() method.

Step 3 Configuring Servlet

From servlet 3.0 you can register servlet in web.xml or annotating the servlet with @WebServlet. In this example, I configure the servlet using web.xml which is located in the WEB-INF directory of your Web application.

For each servlet, servlet-name must be the same for <servlet> & <servlet-mapping> elements.

Step 4 Configure Log4j

This step is just to show you how to configure external libraries.

Step 4.1 Download and copy jar file to the project

  1. Download Apache log4j 1.2.17; and then to unpack it.
  2. Create server_lib folder by right click to the project select New -> Folder. Copy the log4j-1.2.17.jar file from the unpacked folder; and paste to the server_lib/for_deployment folder.

Step 4.2 Log4j.properties

Create an empty file named log4j.properties under config folder; and paste the content below:

Step 5 Copy JSP/Servlet API Jar Files

These files are used for Ant to compile only. It’s not necessary to copy them to deployment package. Go to {tomcat home directory}/lib folder; copy jsp-api.jar and servlet-api.jar then paste them to the  server_lib/for_compilation folder.

Step 6 Configure Ant

Step 6.1 Create build.xml

  • Right click to the project; select New -> Others… 
  • Select XML File under XML category
  • Click Next.
  • Type build.xml in File Name field.
  • Click Finish.

Step 6.2 Configure Runtime Environment

  • Right click to HelloWorld in Ant window; select Run As -> External Tools Configurations
  • Select JRE tab
  • Click Installed JREs… button to add/remove or edit JRE definition.
  • Click Add button.
  • Select Standard VM; then click Next.
  • Click Directory button to select the root folder of JDK installation. Select the folder then click Ok.
  • Click Finish.
  • Check the JDK then click Ok.
  • CLick Apply.
  • Click Close.

Step 6.3 Run Ant

  • From menu bar; select Windows -> Show Views ->  Others…
  • Select Ant under Ant category.
  • Right click to Ant window; select Add buildfiles…
  • Select the build.xml file then click Ok.

You can see 3 targets in build.xml are:

  • clean to delete the generated folder and everything inside.
  • compile to create folder named build, compile code and copy necessary files to deployment folder including properties and jar files.
  • createDir to create deployment directory structure
  • createWar to create war folder in build folder, generate Web Archive (war) file from files and folders in build folder that’s generated by compile target.

Double click to createWar target; Ant will compile and generate files and folder as defined in build.xml and generate the war file. The file name will be {ant project name}.war.

Step 7 Deploy The Application

Step 7.1 Deploy the package to tomcat server

There’re 2 ways to do this:

  1. Copy war file to {tomcat home}/webapps folder. Tomcat will automatically unpack the web folder with name same as war package.
  2. Or copy all sub folders & files in build folder except war folder to {tomcat home}/webapps folder.

Step 7.2 Configure Context

  • Open server.xml in {tomcat home}/conf
  • Under <Host> element, add context definition

docBase refers to the web folder name in {tomcat home}/webapps folder. And path is the context path to access from web browser.

Step 7 Start Tomcat Server

You can start tomcat server by running script or start tomcat service.

Step 8 Run The Servlet

We assume that you install tomcat on default port (8080) and context path is AntWebDemo. Open web browser and type:

http://localhost:8080/AntWebDemo/httpServletDemo

Output

Also check the log file that’s configure in log4j.properties for log information.

Note: you can see some compilation errors in your code as in this example; I don’t configure build path for the project. So you still can build the project using ant without any problem.

Project Structure

Deployment Package Structure