Tải bản đầy đủ - 0 (trang)
Chapter 4. Environment Setup: JDK, Ant, and JUnit

Chapter 4. Environment Setup: JDK, Ant, and JUnit

Tải bản đầy đủ - 0trang

thetoolscoveredinourbookprovideyouwithample,up-todatesetupinstructions.



What'sCoveredinThisChapter

Inthischapter,webeginthesetupofourdevelopment

environmentwiththefollowingcoretoolsrequiredforourJava

developmentthroughtheremainderofthisbook:

JavaPlatformStandardEdition(JSE)DevelopmentKit(JDK)

setupBecausethisbookisaboutJavatechnologies,thisis

thefirstsoftwareweneedtohaveworkingbeforewecan

doanythingelse.

AntThisisthedefactoutilityforbuildinganddeploying

Java-basedapplications.

JUnitThisisasimpleunit-testingframeworkandstandard

wayofunittestingJavacodethesedays.

MakeItAllWorkTogetherFinally,wewillputtogetherall

threetechnologiesandtryoutasimpleunittest.



Note

Thecompletecodefortheexamplesusedinthischaptercanbe

foundwithinthisbook'scodezipfile(availableonthebook's

website).



Inlaterchapters,wewilladdtothisenvironmentbyinstalling

productssuchasadatabase,awebserver,anIDE,tag

libraries,andmore.







JavaPlatformStandardEditionDevelopmentKit

(JDK)

BecausewearedoingJavadevelopment,itwouldmakesense

tohavethenecessaryJavatools(forexample,compiler)setup.

IfyoudonotalreadyhavetheJDKinstalledonyourmachine,

orifyouhaveanolderversionthanonesrequiredbyJUnitand

Ant,youshouldgetthelatestversionfromthejava.sun.com

websiteandhaveitsetuponyourmachinesothatcommands

suchasjavaareinyourpath.

AfterdownloadingandinstallingJava,youshouldbeableto

typethecommandjavaversiontotestyoursetupandensureyou

havethecorrectversionoftheJDK,asshownnext:



C:\anil\rapidjava\timex>java-version

javaversion"1.5.0_06"

Java(TM)2RuntimeEnvironment,StandardEdition(build1.5.0_0

JavaHotSpot(TM)ClientVM(build1.5.0_06-b05,mixedmode,sha



DirectoryStructure

Let'srevisitthedirectorystructurewecoveredintheprevious

chapter.Figure4.1showsthedirectorystructure.Itis

importanttoseethisagainnowbeforewejumpintotheAnt

discussion.Let'sreviewsomeofthemorenotable

subdirectorieshere:

srcdirectorywillcontainalltheJava,HTML/Javascript,XML,



andallotherinput/sourcefilesthatwewilldevelop

ourselves.

buildwillcontaintheoutputofourbuilds(forexample,



compilation,copyingofwebandlibraryfiles,andsoon).

libwillcontainallexternalJARfilesrequiredtorunour



application.

distwillcontainourwebarchive(.war)file,whichcontains



alltheweb-relatedfiles,compiled.classfiles,.jarlibrary

files,andmore.



Figure4.1.Developmentdirectorystructure

forTimeExpression.



Ant

IwouldnotbeexaggeratingbyclaimingthatAnt

(ant.apache.org)isperhapsthesinglemostimportantand

widelyusedtoolintheworldofJavatoday!Therefore,

masteringthistoolisthekeytorapidJavadevelopment.So,it

isnosurprisethatI'mcoveringthistooldirectlyaftertheJDK

section,becauseIconsiderAntthemostvitaltooltoinstall

afteryouaredonewiththebasicJavasetup.

BynowyouprobablyrealizetheimportantroleAntplaysin

Javadevelopment.WewilluseAntextensivelyinthisbook!For

example,wewilluseittobuildourapplication,deployit,run

variousJavaprograms,createourdatabase,runourtests,and

more.

AntwasoriginallydevelopedbyJamesDuncanDavidson,from

theOpenSourceProgramOfficeatSunMicrosystems.Antisa

cross-platformbuildtoolthateliminatesalotofcomplexities

andquirksthatcanbefoundintoolssuchasUnixmake.

Insteadofusingshellcommandsproprietarytotheoperating

system,AntusesXMLfilestospecifyvarioustasks.Antisa

highlyextensibletool,mainlybecauseofthehugemarketof

builtinandexternal(opensourceandcommercial)tasks

availableforAnt,whichmakesitsopowerful.Inaddition,you

caneasilywriteyourowncustomextensions.

GiventhatAntitselfisdevelopedinJava,itisportable,and

accordingtotheAntwebsite,ithasbeentestedonvariousUnix

systems,MicrosoftWindows,MacOSX,andothers.The

ant.apache.orgwebsiteprovidesample(andup-to-date)

informationonhowtogetAntsetuponyoursystem;ifyoudo

notalreadyhaveAntinstalledonyoursystem,youshouldgo

aheadanddosoatthispoint.

WhenyoudohaveAntsetupsuccessfully,youshouldbeable



toruntheantcommandwithoutspecifyingthefullpath.That

is,theantcommandshouldbeinyourpathbecausethe

remainderofourbookwillreferenceantwithoutthefullpath.

Forexample,ifyoutypedant-versiononthecommandline,you

wouldseesomethingsimilartowhatisshowninFigure4.2.



Figure4.2.Testingtheantsetupbyrunningantversion.



[Viewfullsizeimage]



ASimpleAntBuildFile

Ant,bydefault,expectsabuild.xmlfileinthecurrentdirectory,if

youdonotprovidetheantcommandwithanyarguments.Let's



tryoutasamplebuild.xmlfile;youmightrecallthefollowing

tinyAntscriptfromearlierinthisbook.Itprovidesatargetthat

executestheechotask:















Forexample,ifwesavedthisminimalXMLcodeinafilenamed

build.xmlandinthesamedirectorytypeant,thecommandand

itsoutputwouldlookasfollows:

>ant

Buildfile:build.xml

printmessage:

[echo]Helloworld!

BUILDSUCCESSFUL

Totaltime:0seconds



AComprehensiveAntBuildFile

"Helloworld"examplesaregood,butlet'smovetowardbuilding

acomprehensiveAntbuildscriptforoursampleapplication.



Note

Thisbook'sdownloadablecodeshowsthecompletebuildscript

foroursampleapplication,build.xml,alongwithlocal.properties,

afileusedbybuild.xmltoloadsomeexternalproperties.Bothof



thesefileswillbeplacedinthetop-leveldirectory,timex/.

Notethattheuseoflocal.propertiesheredemonstratesahandy

wayofhavingdifferentpropertyfilesfordifferentconfiguration

managementenvironmentssuchasdevelopment,test,staging,

andproduction.



AntConcepts

Beforeweinspectourbuild.xmlfilestep-by-step,let'sreview

somebasicconceptsaboutAnt.

ThekeyconceptsinAntincludeaproject,properties,targets,

tasks,andelements.Propertiesarevariablesyoucansetfor

theantsession.TargetscontainblocksofXMLcodethatget

executedintheformoftasks.Tasksaretheactualexecutables,

suchasthebuilt-injavactask.Tasksinturncancontain

elements(forexample,dirsetorfileset).



Step-by-StepWalkthrough

Now,wewillreviewthekeytargetsinourbuild.xmlfile,butfirst,

let'slookatagraphicalrepresentationofthisfile,shownin

Figure4.3.YoumayalsowanttoreviewFigure4.1onemore

timebeforewebeginthiswalkthrough,becauseourAntbuild

scriptiscloselytiedtothisdevelopmentdirectorystructure.



Figure4.3.HierarchicalviewofourAntbuild.xml

file.



ThefirstXMLelementthatmustappearinanAntfileisproject,

asshownhere:





Thenextfewlinesessentiallysetinternalvariables(properties)

forourscript.Mostofthesepropertiesarerelatedtothe

varioussourceanddestinationdirectorieswewilluse,asshown

inthisexcerpt(noticehowwecanuseinternalvariables,

surroundedbyadollarsignandbraces;forexample,

${dist.dir}):







Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 4. Environment Setup: JDK, Ant, and JUnit

Tải bản đầy đủ ngay(0 tr)

×