- ANT - Listeners and Loggers
- ANT - Custom Components
- ANT - Using If Else arguments
- ANT - Using Command Line Arguments
- ANT - Using Token
- ANT - Extending Ant
- ANT - JUnit Integration
- ANT - Eclipse Integration
- ANT - Executing Java code
- ANT - Deploying Applications
- ANT - Packaging Applications
- ANT - Create WAR Files
- ANT - Creating JAR files
- ANT - Build Documentation
- ANT - Building Projects
- ANT - Data Types
- ANT - Property Files
- ANT - Property Task
- ANT - Build Files
- ANT - Environment Setup
- ANT - Introduction
- ANT - Home
Apache ANT Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Ant - Create WAR Files
Creating Web Archive (WAR) files with Ant is extremely simple, and very similar to the creating JAR files task. After all, WAR file, pke JAR file is just another ZIP file.
The WAR task is an extension to the JAR task, but it has some nice additions to manipulate what goes into the WEB-INF/classes folder, and generating the web.xml file. The WAR task is useful to specify a particular layout of the WAR file.
Since, the WAR task is an extension of the JAR task, all attributes of the JAR task apply to the WAR task.
Sr.No | Attributes & Description |
---|---|
1 | webxml Path to the web.xml file. |
2 | pb A grouping to specify what goes into the WEB-INFpb folder. |
3 | classes A grouping to specify what goes into the WEB-INFclasses folder. |
4 | metainf Specifies the instructions for generating the MANIFEST.MF file. |
Continuing our Hello World Fax Apppcation project, let us add a new target to produce the jar files. But before that, let us consider the war task.
Consider the following example −
<war destfile="fax.war" webxml="${web.dir}/web.xml"> <fileset dir="${web.dir}/WebContent"> <include name="**/*.*"/> </fileset> <pb dir="thirdpartyjars"> <exclude name="portlet.jar"/> </pb> <classes dir="${build.dir}/web"/> </war>
As per the previous examples, the web.dir variable refers to the source web folder, i.e., the folder that contains the JSP, css, javascript files etc.
The build.dir variable refers to the output folder. This is where the classes for the WAR package can be found. Typically, the classes will be bundled into the WEB-INF/classes folder of the WAR file.
In this example, we are creating a war file called fax.war. The WEB.XML file is obtained from the web source folder. All files from the WebContent folder under web are copied into the WAR file.
The WEB-INF/pb folder is populated with the jar files from the thirdpartyjars folder. However, we are excluding the portlet.jar as this is already present in the apppcation server s pb folder. Finally, we are copying all classes from the build directory s web folder and putting them into the WEB-INF/classes folder.
Wrap the war task inside an Ant target (usually package) and run it. This will create the WAR file in the specified location.
It is entirely possible to nest the classes, pb, metainf and webinf directors, so that they pve in scattered folders anywhere in the project structure. But, best practices suggest that your Web project should have the Web Content structure that is similar to the structure of the WAR file. The Fax Apppcation project has its structure outpned using this basic principle.
To execute the war task, wrap it inside a target, most commonly, the build or package target, and run them.
<target name="build-war"> <war destfile="fax.war" webxml="${web.dir}/web.xml"> <fileset dir="${web.dir}/WebContent"> <include name="**/*.*"/> </fileset> <pb dir="thirdpartyjars"> <exclude name="portlet.jar"/> </pb> <classes dir="${build.dir}/web"/> </war> </target>
Running Ant on this file will create the fax.war file for us.
The following outcome is the result of running the Ant file −
C:>ant build-war Buildfile: C:uild.xml BUILD SUCCESSFUL Total time: 12.3 seconds
The fax.war file is now placed in the output folder. The contents of the war file will be as mentioned below −
fax.war: +---jsp This folder contains the jsp files +---css This folder contains the stylesheet files +---js This folder contains the javascript files +---images This folder contains the image files +---META-INF This folder contains the Manifest.Mf +---WEB-INF +---classes This folder contains the compiled classes +---pb Third party pbraries and the utipty jar files WEB.xml Configuration file that defines the WAR packageAdvertisements