Thursday, January 8, 2009

A Tutorial for Adding Groovy to a Java Project

I program primarily in Java and have done so for close to ten years. Before that I programed in C++, C, Pascal, Prolog (yes, I had a real job using Prolog!), and even Fortran and assembler. So I know that languages wax and wane in their importance. So, I decided to add a class or two written in the Groovy language to one of my side projects.

For those not familiar with Groovy its one of a surprisingly large group of languages whose source code is different from Java, and yet runs on the Java runtime. Java source code is compiled into Java bytecode which is executed by the Java Virtual Machine (JVM) on your computer. Groovy source code is also compiled into Java bytecode which then runs on the very same JVM. This allows the two languages to interoperate in interesting ways.

As a starting point for this tutorial I assume you have an existing Java based project, use Ant to build your system and use Eclipse as your development environment. None of that is required for Groovy but it is a fairly standard setup and its what I use, both at home and at work.

The steps are basically
a) download and install the Groovy GDK
b) add a how-to-compile-groovy task to your Ant build.xml (the task is included in the GDK)
c) add the groovy-all.jar file to your library path
d) add the groovy-plugin to your Eclipse project
e) enable groovy-nature in your Eclipse project
f) write a Groovy class and use it.


Overall this should take no more than twenty minutes or so. One of the really cool things is that once you have a Groovy class you can use it from your Java classes just like any other class...your Java code has no idea its using a class written in Groovy.
Now lets look at the steps listed above in cookbook manner.

a) go to http://groovy.codehaus.org/Download and select the latest stable release (currently 1.5.7). Click on the download link and follow the very simple instructions.

b) add the following task to your build.xml to define how to compile groovy code: You then need to actually compile your groovy source files. A standard build.xml will have a compile target that invokes the javac task compile your java source files. I changed that so the my compile task was empty but depended on java_compile and groovy_compile targets. See the full definition of the groovyc task options here: http://groovy.codehaus.org/The+groovyc+Ant+Task


c) the Groovy GDK includes a groovy-1.5.7-all.jar file. Add this to your library and execute paths

d) From Eclipse, select Help, Software Updates, Find and Install, Search for new features to install, next.Now select New Remote Site. Enter Groovy as the Name and http://dist.codehaus.org/groovy/distributions/update as the URL.Press Ok and you should find Groovy added to the list of Sites to include in search. Press Finish.It should find Groovy, check the features you want and press Next.Accept the license terms and press Finish. At this point the plugin will be downloaded and installed.


e) From Eclipse select your proejct, right click and select Groovy->Add Groovy Nature. Eclipse will now understand files with the .groovy extension.

f) Remember that any Java file is also a Groovy file, so if you want to start with baby steps just rename one of your java files to have a groovy extension. Eclipse and Ant will recognize it and compile it to bytecode and then allow you to use it.


You could then take another small step and remove most of the semi-colons, because Groovy doesn't require them.If your class has import statements from various java packages you can probably remove them as well since Groovy automatically imports them.

Clearly you then want to write a class that actually uses Groovy features but that's beyond the scope of HelloGroovyInAJavaProjectWorld.


Enjoy.

20 comments:

  1. I highly recommend that you use Groovy-Eclipse 2.0 available from this update site:
    http://dist.codehaus.org/groovy/distributions/greclipse/snapshot/e3.5/

    Please do not use the update site listed in this blog post, it is for Groovy-Eclipse 1.5.7 (out of date now).

    ReplyDelete
  2. Interesting blog, i usally be aware all about all different kind of sofware. i am online all the time, and this action allow me to see a site costa rica homes for sale and i like it too much. beyond all doubt without my computer i never would have seen this site too.

    ReplyDelete
  3. Groovy is indeed a great addition to make your Java app more powerful.

    ReplyDelete
  4. Very interesting information, worth recommending. However, I recommend this: additional reading

    ReplyDelete
  5. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have you bookmarked your site to check out the new stuff you post. login no HappyMod

    ReplyDelete
  6. I am unable to read articles online very often, but I’m glad I did today. This is very well written and your points are well-expressed. Please, don’t ever stop writing. Writing Tools

    ReplyDelete
  7. I am unable to read articles online very often, but I’m glad I did today. This is very well written and your points are well-expressed. Please, don’t ever stop writing. writing software

    ReplyDelete
  8. Here you will learn what is important, it gives you a link to an interesting web page: CBT Bulk Email Sender
    Here you will learn what is important, it gives you a link to an interesting web page: CBT Bulk Email Sender

    ReplyDelete
  9. If you are looking for more information about flat rate locksmith Las Vegas check that right away. پروژه ستین

    ReplyDelete
  10. WhatsApp Group Links
    blog posts. Any way I'll be subscribing to your feed and I hope you post again soon
    WhatsApp Group Links

    ReplyDelete
  11. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. flexispy

    ReplyDelete
  12. As a seller of legal steroids, you can buy Crazy Bulk products, explore stacks and finally get the body you’ve always wanted vape attic olathe

    ReplyDelete
  13. This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. spy apps for android

    ReplyDelete
  14. This is a smart blog. I mean it. You have so much knowledge about this issue, and so much passion. You also know how to make people rally behind it, obviously from the responses. top trending twitter

    ReplyDelete
  15. Thank you, for technologies and academic projects visit our website
    Java Projects for Engineering Students on Takeoff Edu Group.

    ReplyDelete