Monday, 19 November 2012

Groovy + Javascript == Grooscript 0.1

Last weekend Grooscript 0.1 was released. Grooscript is a library that compiles Groovy to Javascript using the AST tree as the source for the conversion.

There's been a while since I was waiting something like this landing in the Groovy scene. Why couldn't we  have a Javascript "compiler" to use it in the views as well?

Although Grooscript still needs time to be mature it's really promising. I have played with it a little bit and it's really funny the things  you can do in Groovy. The most important feature in version 0.1 is that you can call native javascript inside your methods. The syntax to do it is the following:

      @org.grooscript.GsNative def myMethod(parameter){/*
         //Javascript Code Here

This syntax has been chosen among others mainly because the lack of native method support in Groovy. We couldn't have used the word 'native' as GWT does when invoking javascript native methods.

By the time I'm writing this entry I have found one issue in the native support in Grooscript, and it seems that the workaround is pretty easy, you only need to add a dummy method at the end of your script to make the native support works.
      @org.grooscript.GsNative def myMethod(parameter){/*

      @org.grooscript.GsNative def dummy(){/**/}

Because I really liked the project I started creating a Grails plugin to be able to use it any Grails project. It is not available yet in the Grails repository, but I guess it will be eventually. You can clone it, package it and install it in your Grails application, the code is at github.

The plugin enables you to declare Groovy scripts as static resources and compile those scripts to Javascript code.

Groovy files must have the suffix '.gs'. I still don´t know why files with '.groovy' suffix can´t be 'watched' (Grails doesn´t see any changes made to that files). In the meanwhile I opened an issue at JIRA (The more votes the faster it could be resolved :-) )

You must declare the dependency of the 'grooscript' module in those modules you're declaring any groovy scripts (I also have added here the jquery dependecy because I use it in the example).

modules = {
      dependsOn 'jquery'
      dependsOn 'grooscript'

      resource url:'js/gs/'

In order to proof the native support of Grooscript I did a really simple example. I made a script that changes the Grails logo in your Grails application for the Grooscript logo. Here's the code of the GrooscriptNative.groovy

import org.grooscript.asts.*

class GrooscriptNative{ 
 @GsNative def changeImage(selector,image){/*
     /* Workaround for an issue */
 @GsNative def dummy(){/* */}

class ImageHolder{
    String url

def selector = "#grailsLogo a img"
def image = new ImageHolder(url:"")
new GrooscriptNative().changeImage(selector,image)

If you've installed the plugin properly, declared the ".gs" file in the resources file and on running the application you opened the browser you should see the following:

Cool!! isn't it?

You can get Grooscript from its site or getting the jar adding the public sonatype repository as an extra maven repository to your project.


  1. provides GROOVY in hyderabad with best faculties on real time projects. We give the best online trainingamong the GROOVY in Hyderabad. Classroom Training in Hyderabad India

  2. hi...Im student from Informatics engineering nice article,
    thanks for sharing :)

  3. Excellent blog has been given.very well explanation about plugin enables.
    thanks for sharing this blog.
    oracle fusion procurement online training

  4. Oracle fusion financials online training institute we have our branch over all the india.
    Oracle Fusion Cloud HCM Online Training in Hyderabad, Bangalore, Delhi, Chennai, Kolkata, Pune, Mumbai, Ahmedabad, Gurgon, Noida, India, Dubai, UAE, USA, Kuwait, UK, Singapore, Saudi Arabia, Canada, Oracle Fusion HCM Online Trainings

    Oracle fusion Financials Online Training

    Oracle Fusion Financials online Training