I have big plans with groovy. After playing around with grails and doing some groovy scripting I was sure. I want more groovy. To be able to do more with groovy, I needed to learn more about groovy. One way to do this is to start reading and experimenting. This blog post discusses a few experiments as well as some of the learning sources.

The best learning source for me consists of a number of manning books:

The first thing I did was creating some scripts, I mainly copied some sources from others. I also wrote a blog post about the groovy goodness:


Again I was amazed by the way groovy is used to obtain a webpage and print the contents of the page. The following code block shows the script.

#!/usr/bin/env groovy  
def NEW_LINE = System.getProperty("line.separator")
def address = "http://www.applenieuws.nl/?feed=rss"
def urlInfo = address.toURL()
println "URL: ${address}${NEW_LINE}"
println "URL Text: ${urlInfo.text}${NEW_LINE}"
println "Host/Port: ${urlInfo.host}/${urlInfo.port}${NEW_LINE}"
println "Protocol: ${urlInfo.protocol}${NEW_LINE}"
println "User Info: ${urlInfo.userInfo}${NEW_LINE}"
println "File: ${urlInfo.file}${NEW_LINE}"

But since you are reading this I guess you are already interested in groovy. This post is not for experts in groovy. It is more focussed on people trying to find out if groovy is going to help them work more effectively or just for the fun of it.


I love the integration of groovy and java. You can implement a java interface with groovy. You can call a groovy class from java code. One of the things you are able to do is use spring annotations like @Autowired and @Component in groovy. The following code block shows a listener that can function within an Axon application to receive events. Nice stuff isn’t it?

class GroovyAddressListener implements org.axonframework.core.eventhandler.EventListener {

  GroovyAddressListener(SimpleEventBus eventBus) {
    eventBus.subscribe this

  void handle(Event event) {
    if (event instanceof AddressRegisteredEvent) {
      println "a new address in city ${event.address.getCity()} is received."

If you are not familiar with the Axon Framework, check the website www.axonframework.org. I am also planning a blog post around a sample I am creating with a tight integration of Axon and Grails.

Due to the easy integration of groovy and java, people that like groovy create wrappers around existing java libraries to make them better useable. On of those examples is the HttpBuilder project. This is a project that wraps the apache http client.

Building groovy

Using maven

What is with software that it needs to be build. In java land we have maven. Nowadays (maven 3), maven is even powered by groovy. If you want to integrate groovy with java, a maven build seems very logical. There is a good maven plugin for building groovy. Checkout the following page to learn about the maven configuration.


If you want to compile groovy classes into java classes and use them in your own application, you need to add the groovy library to your classpath. The groovy maven plugin itself is easy to configure. The following code block shows an example that makes use of the groovy maven plugin.


Using grails

Cannot get easier than this, just create a project and use the grails commands. Grails also makes it very easy to add libraries to the project. These libraries can be used in the normal grails classes: domain, controller, service. But you can also use them in your own groovy classes. I use this extensively in the grails/axon integration sample.

In your grails project go to the directory grails-app/conf. There you can find the BuildConfig.groovy. Within this file you can find the dependencies. The following code block gives you an example to include the axon framework as a compile time dependency and xstream as a runtime dependency. To be able to use this you have to uncomment the mavenLocal() in de dependencies part of the same file.

  dependencies {
    compile 'org.axonframework:axon-core:0.5-SNAPSHOT'
    runtime 'com.thoughtworks.xstream:xstream:1.3.1'

Of course you could obtain libraries from other maven repositories, but since we use the snapshot that is not available in a repository we use the local maven repository.

The complete dependency model of grails is pretty extensive and a bit to much to discuss in detail.

Using Gradle

If you are only doing groovy, maven might not be the best choice. Even if you are combining java and groovy some people say maven is not the right choice. I think it is a matter of taste although I do think maven is pretty verbose. An alternative in groovy landscape is Gradle. Gradle uses convention over configuration. It needs less lines of code than ant (without custom libraries) and gives more freedom than maven. Maybe the best about grails is that it does not use xml, but it uses groovy instead.

Peter Ledbrook is doing a good job in using gradle to build grails projects. Read his blog post : http://www.cacoethes.co.uk/blog/groovyandgrails/building-a-grails-project-with-gradle. Personally I do think this is one step to far. For me the grails build system is good enough. I also had a lot of tooling problems when combining gradle and grails in intellij. I did not get the classpath like I wanted to. Using the mechanism as described in the grails part of build works for me. I do think they borrowed some good ideas (dependency management) from gradle.


The last tool you can use when running groovy I would like to mention is Grapes. I like the idea for dependency management in scripts. There is a very easy way to add libraries to your classpath by using the grab mechanism. I like the example as published on the page about the groovy Http builder. I am not going to reprint the example here. It all has to do with the grab annotation. The following line only shows the grab command.

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )

So even in a command line script you can now use libraries managed by Grapes.

That is it for now, more to follow the next months.

Doing more with groovy
Tagged on: