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.

Example:

index.jsp
<html>
<head>
<title>Ant Create War Example</title>
</head>
 
<body>
This is an Ant Create War Example.
</body>
</html>
build.xml
<?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>
 
<!-- Target for creating the new directories-->
<target name="makedir">
<mkdir dir="${classes.dir}" />
<mkdir dir="${lib.dir}" />
<mkdir dir="${dist.dir}" />
</target>
 
<!-- Target for compiling the java code-->
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${classes.dir}">
</javac>
</target>
 
<!-- Target for createing the deployable jar file -->
<target name="war" depends="compile">
<war destfile="${dist.dir}\armstrong.ant.teat.war"
webxml="WebRoot/WEB-INF/web.xml">
<classes dir="WebRoot/WEB-INF/classes"/>
<fileset dir="${dist.dir}"/>
<lib dir="${lib.dir}"/>
<manifest>
<attribute name="AntCreateWar" value="ant.Create.War" />
</manifest>
</war>
</target>
 
<!-- Defualt target to run all targets-->
<target name="mainTarget" depends="compile, war">
<description>Main target</description>
</target>
 
</project>

Console:

Buildfile: D:\TestWorkspace\AntCreateWar\build.xml
clean:
[delete] Deleting directory
D:\TestWorkspace\AntCreateWar\WebRoot\WEB-INF\classes
[delete] Deleting directory
D:\TestWorkspace\AntCreateWar\WebRoot\WEB-INF\lib
[delete] Deleting directory
D:\TestWorkspace\AntCreateWar\dist
makedir:
[mkdir] Created dir:
D:\TestWorkspace\AntCreateWar\WebRoot\WEB-INF\classes
[mkdir] Created dir:
D:\TestWorkspace\AntCreateWar\WebRoot\WEB-INF\lib
[mkdir] Created dir: D:\TestWorkspace\AntCreateWar\dist
compile:
[javac] D:\TestWorkspace\AntCreateWar\build.xml:26:
warning: 'includeantruntime' was not set, defaulting to
build.sysclasspath=last; set to false for repeatable builds
war:
[war] Building war:
D:\TestWorkspace\AntCreateWar\dist\armstrong.ant.teat.war
mainTarget:
BUILD SUCCESSFUL
Total time: 350 milliseconds

No comments: