Sie sind auf Seite 1von 4

31/12/2016 TheGreatAntTutorialAGreatJumpStart|Techtracer

ApacheAntisapowerfulwaytoconvertyourdevelopmentalstructurestodeploymentstructures.Itisdeclarativeand
allthecommandlinetasksusedfordeployinganapplicationarerepresentedbysimpleXMLelements. Without much
details, this tutorial would breeze you through the steps on how to build a web application using a single XML build
file and nothing else. If you have not yet understood what is the use of Ant, read my article on Development and
DeploymentStructurestheperfectwaytobuildwebapplications.

I would use the same analogy of my development structure as mentioned in the above linked article i.e. my
developmentstructureconsistsofthefollowingdirectories:

1. webplaceforallmyJSP,HTML,JavaScriptsandStylesheets.Youcanprovidesubdirectoriesasrequiredfor
eachofthedifferentsetoffiles

2. srcplaceformyjavaclassfilesconsistingofPOJOsorservlets.

3. etcplaceforallconfigfileslikethemostcommonweb.xml
4. libplaceforallthenecessaryjarfilestorunmyapplication.Ievenhaveincludedservletapi.jarinthis
directorysinceyoumayhavetodeployyourwebapplicationinaremoteserveraftercompiling.
5. buildatemporarydirectoryforkeepingthecompiledfiles

6. distaplacetoputinthefinallypackagedwarfile(distribution)

7. antplaceformybuild.xmlfileandexternalpropertiesfile

AllthesedirectoriesarepresentinaparentdirectorycalledWebApp.Thethreethingsyouhavetokeepinmindfor
makinganAntbuildfileare

1. taskswhichcorrespondtoyourcommandlinetaskslikejavac,waretc.Agroupoftaskscanbeexecutedina
sequencebyspecifyingtargets.

2. targetItislikeafunctionwhereyouputreusabletaskssothatitcanbecalledlaterwithoutduplicatingthem.

3. propertythiselementisusedtodefinevariablesinyourbuildfilewhichisusefulwhenthevalue
likeproject_nameorfolder_namekeepsonchanging.

OneofthemostawesomefeaturesofAntisthatyoucankeepthepropertiesfileexternallyinsideofdefiningallthe
variables within the build file. This properties file consists of all the required variables and their values in the form
ofnamevaluepairsandissimpletextfile.InthistutorialIwouldbeusinganexternalpropertiesfile.

ForthetutorialIhaveusedthefollowing:

1. Javaversion1.5

2. ApacheTomcat5.0
3. ApacheAnt1.6.5

OkaywearenowgoodtogowiththegreatAnttutorial.Forthismakeafilewiththenamebuild.properties. This
wouldbeourexternalvariablesfile.

root.dir=..
lib.dir=lib
src.dir=com
conf.dir=etc
web.content=web
project.name=WebApp
build.dir=build

http://techtracer.com/2007/04/16/thegreatanttutorialagreatjumpstart/ 1/4
31/12/2016 TheGreatAntTutorialAGreatJumpStart|Techtracer

NOTE:youmusthittheENTERkeyforstartinganewlineafterthelastlineorAntwillgiveyouanerrorBUILDFAILED
forsomeunknownreason

Itisnotnecessarytoputinthenamewiththedot.YoucanhaveanameasprojectName but there should be no


quotesforthevalue.i.eitshouldNOTbeprojectName=WebApp

Tomakeabuildfilewehavetorememberwhatistobedoneforthedeployments.Tomakethingssimplejustmakethe
modules(targets)necessaryforthecompletewebapplicationdeploymentwhichare

1. cleanremoveallpriordeploymentsofthesameapplicationifmade
2. initmakethenecessarystructurefordeploymentasperthevendors

3. compilecompileyourservletsorPOJOs
4. copyputthecompiledfilesandwebcontentinthedeploymentstructreascreatedduringinit

5. warmakethewarfileandopenthebrowser

Startmakingthebuild.xmlfileintheantfolderasfollows:

<projectname="AppBuilder"default="war"basedir="..">
<propertyfile="ant/build.properties"/>
</project>

Now we have to set theclasspathfortheservletapi.jarto compile our servlets so put the servletapi.jar in the lib
directory of your development structure. Check the property default in the <project> element. It states the LAST
module(target)insidethebuildfile.Inourcaseitiswar

All the following XML elements will now go inside the <project></project>element created above. So put the
classpathsettingelement,

<pathid="classpath">
<filesetdir="${lib.dir}"includes="servletapi.jar"/>
</path>

Here you need to set all the necessary jars to your classpath for successful compilation of the java source
files.${lib.dir}isusedtoretrivethevalueofthelib.diri.elib

Nowwewillstartwithouttargets(modules)asmentionedinthelistabove:

1.clean

<targetname="clean">
<echo>Cleaningthe${build.dir}</echo>
<deletedir="${build.dir}"/>
</target>

HereIamremovingmybuilddirincaseitwaspresentearlierwithcompiledfiles
The<echo>elementisonlyfordisplayingwhatyouaredoinginthecommandline.

2.init

http://techtracer.com/2007/04/16/thegreatanttutorialagreatjumpstart/ 2/4
31/12/2016 TheGreatAntTutorialAGreatJumpStart|Techtracer

<targetname="init"depends="clean">
<echo>Creatingthebuilddirectory</echo>
<mkdirdir="${build.dir}/WEBINFclasses"/>
<mkdirdir="${build.dir}/WEBINFlib"/>
</target>

Here I am creating the normal deployment structure required for tomcat namely WebApp/WEBINF/classes, etc. It
doesntmatterifthefolderWEBINFexistedbeforemakingthefolderclasses.Antautomaticallycreatesallthenecessary
parentfoldersiftheydontexist.

3.compile

<targetname="compile"depends="init">
<echo>Compilethesourcefiles</echo>
<javacsrcdir="${src.dir}"destdir="${build.dir}/WEBINF/classes">
<classpathrefid="classpath"/>
</javac>
</target>

Most important step and the most error giving step. Make sure that all the classpath have been set in the
above <path> element. If all are proper then the all files in src.dir will be compiled successfully and moved to
thebuild\WebApp\WEBINF\classes.Justcheckthepropertydependsofthe<target>element here. This property is
usedtochainthemodules(targets)inasequentialmanner.

4.copy

<targetname="copy"depends="compile">
<copytodir="${build.dir}/WEBINF">
<filesetdir="${conf.dir}"/>
</copy>
<copytodir="${build.dir}">
<filesetdir="${web.content}"/>
</copy>
<copytodir="${build.dir}/WEBINF/lib">
<filesetdir="${lib.dir}"/>
</copy>
</target>

HereIamjustcopyingmycompliedclassesandwebcontentfilesinsidethecorrespondingdeploymentstructure.

5.war

<targetname="war"depends="copy">
<echo>Buildingthewarfile</echo>
<wardestfile="${dist.dir}/${project.name}.war"webxml="${build.dir}/WEBINF/web.xml"
<filesetdir="${build.dir}"/>
</war>
</target>

Thisisthefinalmodule(target)inmybuild.xmlfilewhichmakestheWebApp.warrequiredforthedeployment.war
is an Ant task for which you provide the path of the web.xml file and the directory which contains the deployment

http://techtracer.com/2007/04/16/thegreatanttutorialagreatjumpstart/ 3/4
31/12/2016 TheGreatAntTutorialAGreatJumpStart|Techtracer

structure i.e in our case build directory. The destfile is the final location and name of the war file which would
becomedist\WebApp.warafterthescripthasrun.

Runningtheabovescript
Keepthebuild.propertiesandbuild.xmlfilesintheantfolder.MakesurethattheANT_HOMEenvironmentvariableis
settoyourantsinstallationbindirectory.Now,allyouhavetodoisruntheantcommandonthebuildfileas,

C:\>cdWebAppant\ant
C:\WebAppant\ant>ant

Thatsit.IfsuccessfulyourdevelopmentwillfinallybecomeafullypackagedWebApp.war.

FinalWords
Thus, we have made a complete packaged war file from our source with a single XML file and the wonderfulAnt tool
whichisreadytobedeployedinthetomcatmanager.Wecanfurtherdigdeeperandmakecomplexbuild.xmlusing
thecompletesetofelementsthatAntprovides.ButthistutorialwasjusttogetyoustartedwithusingApacheAnt.If
youhaveanydifficultyinunderstandingtheindividualelementspleasereadtheirdetailedexplanationintheApacheAnt
documentation.

Resources
Ifyouhaveanyprobleminmakingthebuild.xmlorbuild.propertiespleasedownloadthemfromhere.(Right click and
selectSaveas.)

build.xml

build.properties

http://techtracer.com/2007/04/16/thegreatanttutorialagreatjumpstart/ 4/4

Das könnte Ihnen auch gefallen