• December 17, 2014
  • Ant
  • Comments Off on How to create war file using ant?

How to create war file using ant?

What is war file?

WAR stands for Web application Archive. WAR is a compressed file format like the ZIP file which includes jsp, servlet, jar and static web resources etc in a specific hierarchical directory structure called WEB-INF. It is like jar file but follow a specific hierarchical directory structure. WAR files are deployed to the application server like Tomcat or Java EE server like JBoss etc.

Note: WAR files have the .war extension and contains the whole web application.

How to create war file using ant?

As we discussed all basic concepts of Apache ant, let us see the below example to create war file using ant.

In build.xml file:

1. src.dir: It specify the project source folder.
2. classes.dir: It specify the classes compilation output folder.
3. lib.dir: It specify the output folder for the jar files.
4. dist.dir: It specify the output folder for the created war.

Example explanation:

When we run the build.xml file, control first go on the project element and look for the default target. In our example mainTarget is the default target so control go on the mainTarget. Then control looks for those targets on which mainTarget depends. The mainTarget depends on compile and war target. The war target depends on compile target which is further depends upon clean and makedir target. So first clean target will execute followed by makedir, compile, war and mainTarget. The war task in the war target is responsible for creating the war file.



  	<title>Ant Create War Example</title>
    This is an Ant Create War Example.


<?xml version="1.0" encoding="UTF-8"?>
 <!-- Set the project name, basedir and default target to be executed-->
<project name="Ant-Create-War-Test" default="mainTarget" basedir=".">
  <!-- Sets the properties here-->
  <property name="src.dir" location="src" />
  <property name="classes.dir" location="WebRoot/WEB-INF/classes"/>
  <property name="lib.dir" location="WebRoot/WEB-INF/lib" />
  <property name="dist.dir" location="dist" />
  <!-- Target for deleting the existing directories-->
  <target name="clean">
    <delete dir="${classes.dir}" />
    <delete dir="${lib.dir}" />
  	<delete dir="${dist.dir}" />
  <!-- Target for creating the new directories-->
  <target name="makedir">
    <mkdir dir="${classes.dir}" />
    <mkdir dir="${lib.dir}" />
  	<mkdir dir="${dist.dir}" />
  <!-- Target for compiling the java code-->
  <target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${classes.dir}">
  <!-- Target for createing the deployable jar file  -->
  <target name="war" depends="compile">
    <war destfile="${dist.dir}\armstrong.ant.teat.war" 
    	<classes dir="WebRoot/WEB-INF/classes"/>
    	<fileset dir="${dist.dir}"/>
    	<lib dir="${lib.dir}"/>
        <attribute name="AntCreateWar" value="ant.Create.War" />
  <!-- Defualt target to run all targets-->
  <target name="mainTarget" depends="compile, war">
    <description>Main target</description>


Buildfile: D:\TestWorkspace\AntCreateWar\build.xml
   [delete] Deleting directory 
   [delete] Deleting directory 
   [delete] Deleting directory 
    [mkdir] Created dir: 
    [mkdir] Created dir: 
    [mkdir] Created dir: D:\TestWorkspace\AntCreateWar\dist
    [javac] D:\TestWorkspace\AntCreateWar\build.xml:26: 
    warning: 'includeantruntime' was not set, defaulting to 
    build.sysclasspath=last; set to false for repeatable builds
      [war] Building war: 
Total time: 350 milliseconds

Download this example.
Next Topic: How to run junit test case using ant with example?
Previous Topic: How to create jar file using ant with example?

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Comments are closed.