Friday 11 December 2015

Introduction to Gradle Jetty plugin


Introduction : 


Gradle comes with an inbuilt jetty plugin which can be used to deploy your web applications without any hassles. Your web application is build and deployed with a single gradle command, and this indeed makes a developer's  life less complicated.

Usage : 

Add the following to the build,gradle file.

apply plugin: 'jetty'  

Now, you can deploy your application in gradle using :

c:\assortedmind\gradle>  gradle jettyRunWar  
OR

c:\assortedmind\gradleProject> gradle jettyRun


As the name suggests, jerryRunWar will ideally deploy your war into the jetty server while jettyRun task uses the compiled files to be deployed into the server.

You don't need to give a build command before giving a "gradle jettyRun" command. This command takes care of compiling , building and deploying your web application into an embedded jetty server.

The URL is displayed at the command prompt window . The default http port used will be 8080.

Tips And tricks : 


Changing the default http port : 

Add the following lines to build.gradle file :

jettyRun {
httpPort = 9000
}


Debugging port : 

Wondering how to start jettyRun  task in debugging mode?  You need to set the GRADLE_OPTS variable  like this (in windows ):

C:\assortedmind\gradleProject>set GRADLE_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n

Now enable remote debugging in your IDE  by providing port number as 9999.
Now you are ready to go.

Need an upgraded version of jetty ? 

I was pretty happy using my jetty plugin tasks until I was  hit by a brutal  reality !! Gradle ships in an old version of jetty (version 6) which doesnt support servlet 3.0 and above. 
I would recommend using open source plugins for better support in terms of using latest versions of jetty or even tomcat server if needed. 
Plugins like Gretty is pretty impressive. I have tried it out and it works awsome! 


Referrences :

Gradle basics