Core Java

Groovy for Java Developers?! Meet Gradle, Grails and Spock

What are some of the most interesting Groovy use cases for Java developers?

Although it’s been around for a while, it seems that only lately Groovy has been getting its Groove on. It is mostly just another esoteric JVM language for some, but it’s more popular than you think thanks to some popular tools that use it to help you with your Java.

In the following post we’ll check out what’s the connection between Groovy and Java, and try our best to hold ourselves with the groovy puns. We apologize in advance.

It’s almost as groovy as Java

What makes Groovy so, well, groovy? It’s an object-oriented JVM language. This dynamic language is open source, and supported by Apache and the Groovy community. This means that you’ll find options, features and tools around it, that include web frameworks, desktop application framework, concurrency / asynchronous / parallelism library, testing frameworks and more.

Groovy uses the curly bracket syntax, which makes it easier for Java developers to learn and use. However, it includes a list of features that differentiate it from Java, such as:

On the official Groovy site it’s pointed out that the language is aimed at improving developer productivity due to a concise, familiar and easy to learn syntax. Or in other words, if you know Java you can learn and use Groovy in no time.

You can integrate Groovy with any Java program to get scripting capabilities, domain-specific language authoring, runtime and compile-time meta-programming and functional programming. Other features include closures, builders, type inference, and static compilation.

Groovy also has “Scripting and testing glue”. Meaning that Groovy is a good choice for writing “compact” and maintainable tests, as well as build and automation tasks. But the biggest issue here is the Domain-Specific Language authoring (DSL), that lets you create keywords and syntax to express a higher abstract representation of a problem domain.

It’s easier to think of it as a solution for non-programmers to get information. If you have analysts in the company that need to pull data, create cuts or do any other tasks, you can create a custom language for them to do it. That way, they don’t have to know how to program, but just be familiar with the needed syntax for the desired tests.

Here are some DSL examples to make it even more clear:

// equivalent to: turn(left).then(right)
turn left then right

// equivalent to: take(2.pills).of(chloroquinine).after(6.hours)
take 2.pills of chloroquinine after 6.hours

// equivalent to: select(all).unique().from(names)
select all unique() from names

show = { println it }
square_root = { Math.sqrt(it) }

def please(action) {
  [the: { what ->
    [of: { n -> action(what(n)) }]
  }]
}

// equivalent to: please(show).the(square_root).of(100)
please show the square_root of 100
// ==> 10.0

If you want to get started with Groovy, we’d recommend checking out the official documentation.

The numbers don’t lie

Just in case you’re still on the fence about Groovy, you can also take a quick look at the TIOBE index that measures the popularity of programming languages. In it you’ll find that Groovy is one of the 20th most popular programming languages for July 2016.

There’s also interest around it in the job market. In one of our latest posts we crunched 351,799 job openings, and found out there’s a rising demand for Groovy developers.

JVM
US JVM Language Job Trends

Where does Groovy fit in our Java project?

When it’s time to start a brand new Java project, the first go-to tools most of us pick are Maven and JUnit. But after a while as the project grows, the requirements change and we find ourselves wanting more customization than before. At that point, Maven often gets replaced by Gradle. Or at least that’s the journey we went through while developing Takipi.

Gradle is a tool built in Groovy that helps us with our Java. Sounds tricky, but it’s actually pretty interesting using one language to work with a different one. Also, after using it for a while and learning about Groovy, we have a new language to play with and that’s always fun for us. Now the only remaining question is what else can we use it for? The simple answer is tools. But which ones?

Starting with Gradle

gradle-logo-300x65@2x

Gradle is an open source build automation system that uses domain-specific language written in Groovy, instead of Maven’s XML configurations. It can help you automate different tasks, such as building, testing, publishing, deployment and more. You can also create other types of projects to generate static websites, documentation or anything else.

It was designed for multi-project builds that has the potential to grow and become pretty large, while still offering support for incremental builds, in which you add small pieces of software until you reach your final goal. Gradle can determine which parts of the build tree are up-to-date, so that any task dependent upon those parts will not need to be re-executed.

That way, Gradle eliminates code freeze and allows the QA team to check a certain part while you’re working on another. You can even use it to coordinate individual developer builds to the Continuous Integration (CI) build down to the IDE level.

If you catch your teammates messing around with their nerf guns or simply slacking off, Gradle also helps in that area. With it, you can ship quickly and not waste too much time “compiling” in the office, so you can actually get some work done.

If you’re a Maven fan or still not sure how moving to Gradle can help your project, the latter created a full feature comparison between the two, in the following link.

Not Just DSLs: The Grails Web Framework

grails-logo

Grails web framework wants to help you multiply your productivity through Convention-over-Configuration paradigm. How? By giving you a standalone development environment while hiding a big part of the configuration details, allowing you to focus on what matters. Or in other words; it aims to make development as simple as possible.

It enables re-using existing Java technologies such as Hibernate and Spring under a single interface, offers customizable and extensible Ajax support and basically provides a complete development mode, including a web server and automatic reload of resources.

On the official website you’ll be able to find the major sites who chose Grails, that include Netflix Asgard, the Netflix open source web interface for application deployments and cloud management in Amazon Web Services, Vodafone and others.

Moving to Spock

If Gradle is already a part of your company, it’s time to check out what other tools Groovy has to offer. Spock has been turning heads lately as the mocking and testing framework of choice by many Java developers.

Created in 2008 by Peter Niederwieser and Luke Daley, Spock is made for both Java and Groovy application. This unit testing tool is compatible with most IDEs, build tools, and continuous integration servers, capable of handling the full life cycle of a software application. It extends JUnit runner, which is used for running test classes, so it can fit with the current tools you use in the JUnit framework. If you’re moving from one framework to the other, it’s a pretty smooth ride.

DSL has a big part in this framework, allowing you to write your tests in a simple syntax. This means that tests can be done by anyone in the company, and not just by the developers. You can test Spock via its web console.

Check these out as well

While we chose to focus just on these 3 Groovy related projects, there are plenty other options worth checking out. Thanks to the community, you can find a long list of Groovy related projects, such as:

  • GPars – Intuitive and safe ways to handle Java or Groovy tasks concurrently
  • Ratpack – Toolkit for creating high performance web applications
  • Griffon – Application framework for developing desktop applications in the JVM
  • Sdkman – A tool for managing parallel versions of multiple Software Development Kits on most Unix based systems

These are just the tip of the iceberg, and there are plenty more Groovy projects out there. The best way to start is by checking out the trending Groovy projects on Github.

Final thoughts

Java developers have a lot of options when it comes to frameworks, tools and utilities available, not all written in Java. In fact, Java developers have a pretty good chance to come across Groovy during one project or another, due to the impressive tools written in it.

Since the language is pretty similar to Java, a great perk here is the fact that it’s easy to learn and use in no time. Keeping all that in mind, the final choice whether to use Gradle, Spock, Grails or any other Groovy or non Groovy framework is yours, according to the needs and demands for your current project.

If you came across any other Groovy (or simply groovy) frameworks, we’d love to hear about them in the comments below.

Henn Idan

Henn works at OverOps, helping developers know when and why code breaks in production. She writes about Java, Scala and everything in between. Lover of gadgets, apps, technology and tea.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button