RapidRESTDevelopmentwithmavenjettyplugin
http://jlorenzen.blogspot.com/2010/02/rapid-rest-development-with-maven-jetty.html
Tuesday,February16,2010
RapidRESTDevelopmentwithmavenjettyplugin
EverwonderhowmuchtimeiswastedbyJavadevelopersrebuildingandredeployingwebapplications?FormealoneIcan'timagineit.Infact,I'dbeembarrassedifGoogleBuzzmadeitpublicknowledgewithoutmyconsent.TwoyearsagoIwroteanarticle"
NomoreJ2EEApps"andIreceivedalotofgreatfeedback.LetmefirstsaythatIthink,IMHO,JavaDevelopersareatadisadvantagewhenitcomestorapidlydevelopingwebapplicationswithastaticlanguage.Developersusingpython,php,rails,orgrailsreallydon'tevenhavetospendasecondtryingtosolvethisproblem.Ontheotherhand,JavaDevelopershavetofigureouthowtobestaccomplishthis,andeverysituationseemstobedifferent:JBoss,Weblogic,Eclipse,Idea,Netbeans,Jetty,JRebel.OfallthesolutionsIthinkJRebelprovidesthebestchanceforsuccessthatsolvesanyenvironmentnomatterthewebcontainerordevelopersIDEofchoice.
Ihaven'treallydonemuchhardcorejavadevelopmentinawhile,butinordertoimprovemyteam'sproductivity,Iamgoingtobeexploringbestpracticesthenextcoupleofmonthsconcerningthisarea.Firstup,IamgoingtoexplainhowIgotthemavenjettyplugintoworkwithourRESTServicesWARandthestepsnecessarytoredeploychanges.Thenicethingaboutjettyisit'seasytousefrommavenandwecoulduseitinourCIenvironmenttopossiblyreduceourbuildtimesandprovidequickerfeedback.ThedownsideiseachchangerequiresjettytohotdeploythenewWAR.EndtheendIthinkthebestsolutionwillbeacombinationofJBoss+JRebel.ButIwon'tgettoforawhile.
MavenJettyPlugin
Myfirstprototypeusesthemaven-jetty-pluginversion6.TheapplicationwearetestingisaWARcontainingRESTServicesbuiltwithJersey(JAX-RS).Hereisagoodpostingbymyco-workerJeffBlack"Jersey...JettyandMavenstyle!".Thisexampledidn'tworkformebecauseforsomeinsanereasontheinit-param,com.sun.ws.rest.config.property.packages,doesnotworkinWebSphere.SoIhadtodosomeslightmodificationstogetittoworkwithoutthatbeingdeclared.Mypom.xmlandjetty.xmlfilesarebelow.Most"normal"non-Jerseyapplicationsdon'tneedallofthis,butitwasnecessarytogetourlegacyWARworkingwithjetty.
Herearethestepsinvolvedtostartjettyandredeploychanges:
mvninstalljetty:run-thiswillfirstbuildtheWARandthenstartjettywhilealsodeployingtheWAR.RunninginstallwasnecessarybecauseIreferencetheexplodedWARdirectoryundertargetinthejettyconfiguration.
Makechangestosourcecode
Run"mvncompilewar:exploded"inaseparateterminaltocompileyourchangesandcopythenewclassfilestothelocationJerseyexpectstofindthem.Whichinmycaseis/target/myapp/WEB-INF/classes
ClickbacktotheterminalrunningjettyandhittheENTERkey.ThiscausesjettytoreloadtheWAR.ThisisbecausebydefaultIsetthescanintervalto0andjetty.reloadtomanual,soIcanbatchupmultiplechangesbeforereloading.
OverallIamhappywiththeresultssofar.Previously,ittook1-2minutesandsometimesmoretorebuildthewarandhotdeploytoJBoss.Usingthejettypluginthisnowtakesaround30seconds.Again,IthinkthiscouldbefurtherimprovedbyusingJRebel.
Tips
IdidhavetoupdatemyMAVEN_OPTSenvironmentvariabletoincreaseJava'sPermGenSpacesincejettyreloadstheWAReachtimeandyou'llquicklyrunoutofmemory.ThiswassomethingIwasalreadydoinginJBoss.Hereiswhatitissetto:
exportMAVEN_OPTS="-Xmx1024m-XX:PermSize=256m-XX:MaxPermSize=512m"
SampleFiles
Hereismypom.xml
02 | < groupId >org.mortbay.jetty</ groupId > |
03 | < artifactId >maven-jetty-plugin</ artifactId > |
04 | < version >6.1.22</ version > |
06 | < jettyConfig >${project.build.testOutputDirectory}/jetty.xml</ jettyConfig > |
07 | < scanIntervalSeconds >${jetty.scan.sec}</ scanIntervalSeconds > |
08 | < useTestClasspath >true</ useTestClasspath > |
10 | < baseResource implementation = "org.mortbay.resource.ResourceCollection" > |
11 | < resourcesAsCSV >${project.build.directory}/myapp</ resourcesAsCSV > |
16 | < name >jetty.port</ name > |
17 | < value >${jetty.port}</ value > |
22 | < name >jetty.reload</ name > |
23 | < value >${jetty.reload}</ value > |
29 | < groupId >commons-dbcp</ groupId > |
30 | < artifactId >commons-dbcp</ artifactId > |
31 | < version >1.2.1</ version > |
34 | < groupId >net.sourceforge.jtds</ groupId > |
35 | < artifactId >jtds</ artifactId > |
39 | < groupId >com.oracle.jdbc</ groupId > |
40 | < artifactId >ojdbc14</ artifactId > |
41 | < version >10.2.0</ version > |
47 | < jetty.port >8080</ jetty.port > |
48 | < jetty.scan.sec >10</ jetty.scan.sec > |
49 | < jetty.reload >manual</ jetty.reload > |
Hereismyjetty.xmllocatedunder/src/test/resourcesusedtodefinetheDatasource.
02 | <!DOCTYPEConfigurePUBLIC"-//MortBayConsulting//DTDConfigure//EN""http://jetty.mortbay.org/configure.dtd"> |
04 | < Configure id = "Server" class = "org.mortbay.jetty.Server" > |
06 | < New id = "MYAPP-DS" class = "org.mortbay.jetty.plus.naming.Resource" > |
07 | < Arg >jdbc/MYAPP-DS</ Arg > |
09 | < New class = "org.apache.commons.dbcp.BasicDataSource" > |
10 | < Set name = "driverClassName" >oracle.jdbc.driver.OracleDriver</ Set > |
11 | < Set name = "url" >jdbc:oracle:thin:@localhost:1521:XE</ Set > |
12 | < Set name = "username" >user</ Set > |
13 | < Set name = "password" >password</ Set > |
Postedbyjlorenzenat11:45PM
Labels:jersey,jetty,maven,rest
Disqus
Like
Dislike
LoginAddNewComment
Image
Showing3comments
RonnyNicepost:)
Ifindusingmvnjettypluginormvntomcatpluginarealtimesaver.
Justaminornote;Ithinkoracle.jdbc.driver.OracleDriverisdeprecated,infavoroforacle.jdbc.OracleDriver.Like
Reply
1yearago
jlorenzenThanksRonny.Iactuallywasn'tawarethatdriverwasdeprecated.Thanksforthetip.DoyouhaveanyreferenceinformationIcouldreadtogetcaughtup?Like
Reply
1yearago
inreplytoRonny
RonnyLookslikeithasbeendeprecatedforawhile,andisnowremovedin11g.
http://www.oracle.com/technolo...,section"Desupportoforacle.jdbc.driver"
Aneasychangethough,justusepackageoracle.jdbcinsteadoforacle.jdbc.driverLike
Reply
1yearago
inreplytojlorenzen
MSubscribebyemail
SRSS
blogcommentspoweredbyDisqusLinkstothispost
CreateaLink