• TwitterFacebookGoogle PlusLinkedInRSS FeedEmail

Ant Build Script For War File

15.08.2019 
Ant Build Script For War File 3,7/5 8139 votes
  1. Ant Script Tutorial
Simple Ant Build Script for Eclipse Dynamic Web Project #java #ant #snippet

You'll probably have to know a little bit about Java, Ant, and build files for this sample build.xml file to be any use to you, but if you're looking for a sample Ant build script that can be used to create a war file, or one that simple uses a war task, this example might work for you. I'm not going to provide any. ANT Tutorial 05 - war, deploy, and start/stop tomcat from ANT Script. 03:40 What is the root tag of an ant build file? 05:00 How to point to the directory where the build file is in an ant.

build.xml
<?xml version='1.0' encoding='UTF-8'?>
<projectbasedir='.'default='package'name='PROJECT_NAME'>
<!--
This script assumes:
1) CATALINA_HOME environment variable points to tomcat's directory
2) Following Folder Structure
./ (project root)
- src/ (project.src.dir - source folder)
- build/
- classes/ (project.classes.dir)
- WebContent/ (project.web.dir)
- lib/ (project.lib.dir)
- PROJECT_NAME.war (project.war)
3) JAVA_HOME is set and point to JDK 1.6 and above
-->
<propertyenvironment='env' />
<propertyname='TOMCAT_HOME'value='${env.CATALINA_HOME}' />
<propertyname='debuglevel'value='source,lines,vars' />
<propertyname='target'value='1.6' />
<propertyname='source'value='1.6' />
<propertyname='project.name'value='${ant.project.name}' />
<propertyname='project.web.dir'value='WebContent' />
<propertyname='project.src.dir'value='src' />
<propertyname='project.classes.dir'value='build/classes' />
<propertyname='project.lib.dir'value='${project.web.dir}/WEB-INF/lib' />
<propertyname='project.war'value='${project.name}.war' />
<propertyname='project.runtime.lib'value='${TOMCAT_HOME}/lib' />
<propertyname='project.deploy.location'value='${TOMCAT_HOME}/webapps' />
<pathid='classpath.runtime'>
<filesetdir='${project.runtime.lib}'includes='*.jar' />
</path>
<pathid='classpath.lib'>
<filesetdir='${project.lib.dir}'includes='*.jar' />
</path>
<pathid='project.classpath'>
<pathelementlocation='${project.classes.dir}' />
<pathrefid='classpath.runtime' />
<pathrefid='classoath.lib' />
</path>
<targetname='init'>
<mkdirdir='${project.classes.dir}' />
<copyincludeemptydirs='false'todir='${project.classes.dir}'>
<filesetdir='src'>
<excludename='**/*.java' />
</fileset>
</copy>
</target>
<targetname='clean'>
<deletedir='${project.classes.dir}' />
<deletedir='${project.war}' />
</target>
<targetdepends='init'name='build'description='Compiling all java files in ${project.src.dir}'>
<echomessage='${project.name}: ${ant.file}' />
<javacdebug='true'debuglevel='${debuglevel}'destdir='${project.classes.dir}'includeantruntime='false'source='${source}'target='${target}'>
<srcpath='src' />
<classpathrefid='project.classpath' />
</javac>
</target>
<targetdepends='build'name='package'description='Packagign all files into ${project.war}'>
<wardestfile='${project.war}'index='true'needxmlfile='fasle'>
<classesdir='${project.classes.dir}' />
<libdir='${project.lib.dir}' />
<filesetdir='${project.web.dir}'>
<includename='**/*.*' />
</fileset>
</war>
</target>
<targetdepends='package'name='deploy'description='Copying ${project.war} to ${project.deploy.location}'>
<copyfile='${project.war}'todir='${project.deploy.location}' />
</target>
</project>

commented Feb 15, 2015

There is a typo in line 44
should be
But then it works perfect =)

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
Active2 years, 2 months ago

I have created sample/default test Web project in eclipse.

Then I am using Ant to build and deploy. I export default build.xml. but this ant doesn't have task to create war file. So, I have to write task for creating war.

I used below code.

When I copy and paste the generated war in webapps. After starting Tomcat server, I found that this war is corrupt. War file is just 1KB file that contains web.xml and manifest.mf.

I would like to know:

  1. What WAR file contains and its directory structure? What are optional and required files/folders?
  2. Task syntax to create and war through Ant build.xml

Ant Script Tutorial

I read SO answer answer1 and answer2 , but couldn't help in my project.

Community
Umesh PatilUmesh Patil
8,09813 gold badges43 silver badges72 bronze badges
Example

1 Answer

Your WAR file doesn't look right to me.

  1. Content and web.xml should be at the root.
  2. WEB-INF is at the root.
  3. .class files in WEB-INF/classes
  4. .jar files in WEB-INF/lib
duffymoduffymo
277k38 gold badges327 silver badges517 bronze badges
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.

Not the answer you're looking for? Browse other questions tagged eclipsetomcatant or ask your own question.