Showing posts with label Gradle. Show all posts
Showing posts with label Gradle. Show all posts

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


Sunday, 7 June 2015

Gradle basics : Building war file using gradle

Gradle is very similar to the commonly used build management/dependency systems like ant scripts, pom.xml or apache ivy. But what attracted me towards gradle is its simplicity and availability of inbuild plugins which definitely makes our life easier. Also Gradle is readable , unlike its xml variants. 



Installing Gradle is pretty easy : 

Step 1: Download Gradle from the link : download link

Step 2: Unzip gradle to some folder of your choice .

Step 3: You can see the bin folder inside the unzipped file contents. You need to add this folder to the classpath so that you can run gradle command from any where on your system 

Step 4: now try running gradle from the command prompt. A similar output is shown: 


Writing your Gradle Build Script :

Gradle uses a Domain Specific Language (DSL) based on Groovy to declare builds. Groovy syntax is very similar to Java syntax, so it increases the readability of our build scripts. 
The Build script is a file named build.gradle.

The sample build.gradle file for my spring mvc application looks like this. Place the gradle file into the root of your web project folder.



apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'

repositories {
    mavenLocal()
    mavenCentral()
}

war {
    baseName = 'assortedmind-gradle'
    version =  '0.1.0'
}

dependencies {
    compile 'org.springframework:spring-webmvc:4.1.1.RELEASE'
    compile 'org.springframework:spring-context:4.1.1.RELEASE'
    compile 'org.springframework:spring-aop:4.1.1.RELEASE'
    compile 'org.springframework:spring-beans:4.1.1.RELEASE'
    compile 'org.springframework:spring-core:4.1.1.RELEASE'
    compile 'org.springframework:spring-expression:4.1.1.RELEASE'
    providedCompile 'javax.servlet:servlet-api:2.5'
  
}

task deploy(type: Copy) {
    from 'build/libs'
    into 'C:/Anushya/myJboss-jboss-as-7.1.1.Final/jboss-as-7.1.1.Final/standalone/deployments'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.3'
}


You might feel confused in the beginning, but later you will be overwhelmed by how simple this file really is...


A gradle script is a collection of tasks.  Gradle comes with inbuilt plugins , which are actually a collection of pre-defined tasks.  In our file,  the line 

apply plugin: 'java'
 is all it needs to add build related tasks. Type gradle task in the command line , and you can see the tasks that is been added as part of Java plugin. 

























Note :  To use the Java plugin for gradle effectively, make sure that you place your source files in src/main/java folder and non-java resources in src/main/resources. In this way , gradle will take care of packaging your war file by placing the class files and resources files in right manner. 

The Dependencies{ }  block lets you mention your compile time and runtime dependencies. 

The repositories{ } block lets you mention which repository needs to be referenced during tasks execution. First it will look for the jar files in local repository , and if not available , will check for maven central repository.

Type gradle build to build a war file. The war file will be present in the build/lib/ folder. 

My deploy task copies the war file into my jboss standalone deployment folder.

The complete web application project structure is as follows : 





















Finally the wrapper task lets you run this gradle script in machines that does not have gradle installed.  Run the task and it will install gradle for you.