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


Saturday 11 July 2015

Spring : Configuring web.xml programatically

Spring 3.1introdced the support for Servlet 3's code-based configuration, which can be used as an alternative to the web deployment descriptor file web.xml. For this purpose, Spring has come up with 
an interface namely WebApplicationInitializer. 


The traditional web.xml file I have with me is as follows : 



And my servlet configuration file assortedmind-servlet.xml is as follows :



The servlet configuration is kept simple for the time sake. 

With these traditional xmls in mind, let us try to achieve the same purpose through a code based approach. The following piece of code can be used to replace web.xml



And the assortedmind-servlet.xml can be coded as follows :



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. 



Sunday 12 April 2015

Simple fix for mysql " Access denied for user 'root'@'localhost' " issue

There are numerous instances where this issue can be faced. In my case, I faced this while installing mysql in my RHEL machine . I installed rpm as root user, and after installation , I faced this issue while logging into mysql .
ERROR 1045(28000) : Access denied for user 'root@localhost' (using password: no )

Even
 mysql -u root -p ***
produced the same error.

This issue can be fixed in a very quick and easy solution .

If you have started mysql , stop it.
Now restart your server with  --skip-grant-tables option
eg : in my case  I used the command
sudo /etc/init.d/mysql start --skip-grant-tables

Now you dont need a username password combination to open mysql.
simply, use mysql to login to mysql prompt. 
 shell> mysql

now load the grant tables :

mysql> FLUSH PRIVILEGES;

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('HelloWorld');

This command will help you set your new password.

Dont forget to restart your mysql server with the new user-name and password combination, as opening mysql with --skip-grant-tables is insecure.

hope this helps you guys out :) Good luck.