Making Your Life Simpler—How Groovy Can Leverage Your Existing Java Skill

DZone 's Guide to

Making Your Life Simpler—How Groovy Can Leverage Your Existing Java Skill

· Java Zone ·
Free Resource
Java is a large, powerful language, but it’s showing its age. Decisions made early in its development are not necessarily appropriate now, and over time it has accumulated problems and inconsistencies. Still, Java is everywhere, and its tools, libraries, and infrastructure are both useful and convenient.

The Groovy version of a Java class is almost always simpler and cleaner. Groovy is far less verbose and generally easier to read.  As true as that statement is, though, it’s a bit misleading here. We’re not advocating rewriting all your Java code in Groovy. Quite the contrary; if your existing Java code works, that’s great, though you might want to consider adding test cases in Groovy if you don’t already have them. We’re much more interested in helping Java than replacing it.

Groovy features that help Java

What does Groovy offer Java? Here’s a short list of topics that support our argument.

1. Groovy ads new capabilities to existing Java classes.
Groovy includes a Groovy JDK, which documents the methods added by Groovy to the Java libraries. The various sort methods added to the Collection interface that we used for strings was a simple example. We can also use Java classes with Groovy, and add features like operator overloading to Java.

2. Groovy leverages Java libraries.
Practically every Groovy class relies on the Java libraries, with or without Groovy additions. That means virtually every Groovy class is already an integration story, mixing Groovy and Java together. One nice use case for Groovy is to experiment with Java libraries you haven’t used before.

3. Groovy makes working with XML easy.
Here is an area where Groovy shines. Groovy includes a class called MarkupBuilder, which makes it easy to generate XML. It also has classes called XmlParser and XmlSlurper, which convert XML data structures into DOM structures in memory.

4. Groovy includes simplified data source manipulation.
The groovy.sql.Sql class provides a very simple way to work with databases.

5. Groovy’s metaprogramming streamlines development.
The builder classes are an example of Groovy metaprogramming.

6. Groovy tests work for Java code.
The Spock testing tool is a great way to test Java systems.

7. Groovy build tools work on Java (and mixed) projects.
AntBuilder and Gant bring Groovy to Ant builds.

8. Groovy projects like Grails and Griffon make developing web and desktop applications easier.
The Grails project is a complete-stack, end-to-end framework for building web applications, based on Spring and Hibernate. Griffon brings the same convention over configuration ideas to desktop development.

When looking at the sort of problems Java developers typically encounter, this list can serve as a source of ideas for making implementations simpler, easier to read and understand, and faster to implement.

Java use cases and how Groovy helps

Groovy developers work on the same sorts of problems that Java developers do, so many higher-level abstractions have been created to make addressing those problems easier.

Here is a list of the areas where Groovy will help.

Spring Framework support for Groovy

One of the most successful open source projects in industry today is the Spring framework. It’s the Swiss Army chainsaw of projects—it’s pervasive throughout the Java world and has tools for practically every purpose.

No one is ever going to suggest rewriting Spring in Groovy. It works fine in Java as is. Nor is there a need to port it to Groovy. As far as Groovy is concerned, it’s just another set of byte codes, so who cares where they came from? Groovy can use Spring as though it were just another library.

The developers of Spring, however, are well aware of Groovy and the special built-in capabilities for working with it. Spring bean files can contain inline scripted Groovy beans. Spring also allows you to deploy Groovy source code rather than compiled versions, as so-called refreshable beans. Spring periodically checks the source code of refreshable beans for changes and, if it finds any, rebuilds them and uses the updated versions. This is a very powerful capability.

Finally, the developers of the Grails project also created a class called BeanBuilder, which is used to script Spring beans in Groovy. That brings Groovy capabilities to Spring bean files much the way Gant or Gradle enhances XML build files.

Simplified database access

Virtually all Java developers work with databases. Groovy has a special set of classes to make database integration easy. Also, from the Grails world comes GORM, the Grails Object-Relational Mapping tool, a DSL for configuring Hibernate.

Building and accessing web services

Another area of active development today is in web services. Java developers work with both SOAP-based and RESTful services, the former involving auto-generated proxies and the latter leveraging HTTP as much as possible. If a little care is applied, the existing Java tools work just fine with Groovy implementations.

Web application enhancements

Groovy includes a groovlet class, which acts like a Groovy-based servlet. It receives HTTP requests and returns HTTP responses and includes prebuilt objects for requests, responses, sessions, and more. We can use it in Java web applications easily. Of course, one of the most successful instances of Groovy and Java integration, and arguably the killer app for Groovy, is the Grails framework, which brings extraordinary productivity to web applications.

Desktop Groovy applications

Though Java has been most successful on the server side, there is a small but enthusiastic group of developers who focus on client side GUI development. Groovy annotations help implement good Model-View-Controller architectures, eventually resulting in Griffon, the project that promises to bring Grails-like productivity to desktop development.

In each of these use cases, Groovy can work with the existing Java tools, libraries, and infrastructure. In some situations, Groovy will simplify the required code. In other cases, the integration is more deeply embedded and will provide capabilities far beyond what Java alone includes. In all of them, the productivity gains are both obvious and dramatic.


As powerful as Groovy is (and as fun as it is to use), we still don’t recommend replacing your existing Java with Groovy. We advocate a blended approach. Our philosophy is to use Java wherever we can, which mostly means using its tools and libraries and deploying to its infrastructure. We add Groovy to Java wherever it helps the most.

For source code, sample chapters, the Online Author Forum, and other resources, go to

DZone members also have a 40% discount on various Manning titles (including Making Java Groovy)!  Just type "dzone40" into the promotion code box when you go to purchase a book.

Here are some other Manning titles you might be interested in:

Groovy in Action, Second Edition
Dierk König, Guillaume Laforge, Paul King, Jon Skeet

The Well-Grounded Java Developer
Benjamin J. Evans and Martijn Verburg

Unit Testing in Java
Roy Osherove and Lasse Koskela


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}