More Groovy Goodies
I've had quite a few people ask me to put together a follow-up article to my previous post titled What Makes Groovy So...Groovy, so, here it is. Here I'll take a quick look at integrating Groovy with Maven 2, using Groovy for web services, XML parsing, and Hudson's Groovy console integration.
Integrating Groovy with Maven 2
It took me all of about 5 minutes to set up an existing project to use Groovy. The first thing (I suppose) to worry about is the directory structure. I say to do this first simply because it's not completely obvious. Basically, you just put a "groovy" directory at the same level as your "java" directory under src/main and src/test. Like this:
This tells the Maven 2 plugin where to find your .groovy files.
Next, you need to change your pom.xml file to do 2 things: 1) compile the .groovy classes first, and 2) add the groovy jar dependency. Both of these are super simple.
First, modify the "plugins" section of your pom.xml file to include the Groovy plugin. Make sure you put the Groovy plugin before the Java compiler plugin. This will allow your Java classes to transparently use the Groovy classes.
-
-
<plugins>
-
<plugin>
-
<groupid>org.codehaus.mojo.groovy</groupid>
-
<artifactid>groovy-maven-plugin</artifactid>
-
<extensions>true</extensions>
-
</plugin>
-
<plugin>
-
<artifactid>maven-compiler-plugin</artifactid>
-
<version>RELEASE</version>
-
<configuration>
-
<source>1.5</source>
-
<target>1.5</target>
-
</configuration>
-
</plugin>
-
</plugins>
Next you need to add the following to your dependency section (note, you may want to check on the version number. If a more recent version is available, use that instead.)
-
<dependency>
-
<groupid>org.codehaus.groovy</groupid>
-
<artifactid>groovy-all</artifactid>
-
<version>1.1-beta-3</version>
-
</dependency>
And that's it! Really! (I highly recommend getting IDEA 7.0. It offers great Groovy support, and a very cool bi-directional compiler that make it so your Groovy and Java classes can have about the same level of IDE support.)
Groovy Web Services
You won't believe how incredibly easy web services are in Groovy. Web services are suddenly easy to use, and not a major pain in the caboose. If you really want to appreciate what Groovy can to for you, check out how web services in Java are usually handled, using Axis (there's a good article on this at Java Boutique). Traditionally, you need to deal with wsdl files, and stub classes, and all kind of icky, confusing nastiness. Not with Groovy.
Check Groovy's sites for a good overview on Groovy web services. Here's the quick and dirty. You want to create a web service in Groovy? How about something like this:
-
}
-
}
-
-
import groovy.net.soap.SoapServer
-
-
server.setNode("MathService")
-
}
Can you believe that's a web service? Yep, a fully functional web service with an associated WSDL and everything. How does a client interact with that? Oh, you are not going to believe this!
-
import groovy.net.soap.SoapClient
Where's the magic? Your looking at it. We're able to dynamically create the proxy class, and reference the methods exposed in the web service as if they were local methods. Suddenly web services are cool again!
There is a proposal in place for a RESTful web service API, but currently the push for this is more in the context of Grails. However, implementing a RESTful web service would likely be quite easy when you can make use of the object-based switch statements, GStrings, and XML parsers. Not quite as magical as SOAP-based web services, but easy nonetheless.
Groovy XML Parsing
In a rather dated article (2004), Jack Herrington does a good job of explaining Getting Groovy with Java. He outlines how XML is usually handled in Java, and how it evolved to being handled in Groovy. It's certainly worth a read, but let's focus on Groovy today. What kind of options do we have for parsing XML files in Groovy?
Just for kicks, let's stick with the same XML that was generated in my last post on Groovy:
-
-
<people>
-
<person>
-
<firstname>John</firstname>
-
<lastname>Doe</lastname>
-
<iailaddress>john.doe@example.com</iailaddress>
-
</person>
-
<person>
-
<firstname>George</firstname>
-
<lastname>Bush</lastname>
-
<iailaddress>prez@whitehouse.gov</iailaddress>
-
</person>
-
</people>
How would we go about parsing this in Groovy? Well, one way would be to use the Groovy XmlParser class. That code would look something like this:
You'll notice that, through the magic of the dynamic language, we can treat the XML tree as an object.
What about attributes? They are accessed with the '@' identifier. For example, if your emailAddress xml had a "location" attribute, we would access it using:
-
println person.emailAddress.@location
or
Groovy's XmlSlurper is another way to parse XML (and fun to say), but I don't completely understand it yet. I encourage you to search for it. Here's something to get you started.
Hudson's Groovy Console Integration
Hudson is a great Continuous Integration system that's really getting some attention lately. One of the nice features it has is an integrated Groovy console where you can run arbitrary Groovy script. Great for figuring out the state of the build. Here's a screen shot of the console screen.
In Conclusion...
I'm sure there are plenty of other examples of what makes Groovy so groovy, so please, if you have any other ideas leave a comment. I'll be happy to look into your suggestions and expand on them.
Have fun!
