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. Excellent blog has been given.very well explanation about plugin enables.
    thanks for sharing this blog.
    oracle fusion procurement online training

  3. 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

  4. All given info was wonderful and it's very helpful for everyone. I read your post is very nice thank you.
    We Have Top Rated CALFRE Search Engine to search about all different courses in Al Karama, Dubai Locations. For Example Search a particular course like Oracle ASCP Training in Location Dubai Know More Click Here

  5. Hi, Thanks for posting a great information of the your blog.
    DOT NET Training Institutes in Ameerpet

  6. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    Oracle Project Accounting Training in Hyderabad

  7. Nice information. I was searching for the same. It helped me a lot and saved my time.
    Oracle Fusion SCM Training in Hyderabad

  8. It's Incredibly grand. Really very helpful article , Thank you for sharing

    Workday Online Training

  9. Thanks for sharing such a wonderful information from this post. We are providing the best services click on below links to visit our website.
    Oracle Fusion HCM Training
    Workday Training
    Okta Training
    Palo Alto Training
    Adobe Analytics Training

  10. thanks for sharing a nice post keep posting

  11. This is the good website and it's a fantastic website Your contribution is greatly appreciated