Groovy Cheat Sheet



// This can also be written as a function - srcDir is a method (Syntax sugar of the Groovy language) souceSets. Thanks for the cheat sheet.

[Total: 2 Average: 4.5/5]

Groovy cheat sheet Groovy cheat sheet Table of contents Core types and operators Type list Coercions Overloadable operators Other operators Flow control Conditions Groovy Truth Loops Exceptions Classes Objects Properties Create your own Create your own. A multi-faceted language for the Java platform. Apache Groovy is a powerful, optionally typed and dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax.It integrates smoothly with any Java program, and immediately delivers to your application powerful. Groovy; Groovy Cheatsheet. Created by Jana Volencova. This section is meant as a quick library of copy/paste code snippets. For details on functions, refer to the API Documentation. Calculated Field Set. Get the current processed item (e.g.

Groovy has been around on the Java scene since 2003. With over a decade’s worth of history, development and experience, it is a Java syntax compatible object oriented programming language that compiles into JVM bytecode.

In a way, Groovy can be viewed as a flavor of Java without being actual Java. This is because it works on the Java platform in a native capacity. Because of the way it works, it interoperates nicely with Java code and its associated libraries. Most valid Java code also translates to valid Groovy code.

Groovy is designed to be both a programming and scripting language. This means that unlike Java, which needs to be compiled, Groovy can be used in conjunction to provide on the spot programmatic processes that doesn’t require a lot of server side heavy-lifting.

The biggest difference between Groovy and Java code is that Groovy is more compact, with less syntax requirements and therefore making it appealing to many developers. It also means that many Java developers going into Groovy will find the process of picking it up super simple. Why? Because fundamentally, most object-oriented based programming languages tend to follow the same ideas. This shared ideology makes it easy for developers to jump between Java and Groovy.

This piece will go over how to do 7 most common things that we encounter when writing code. They are the foundation and general building blocks of any program and often crop up in some form within an object-oriented based thinking.

Gradle Cheat Sheet

1. Installing Groovy

You can install Groovy via your package manager. Alternatively, you can install groovy from their website.

Groovy files are saved with .groovy extension.

To execute files in the command line, you can do so using groovy. For example:

Groovy Cheat Sheet

groovy index.groovy

To run Groovy Shell, you groovysh in the command line.

Groovy Cheat Sheet Fantasy Football

2. List and maps

A list is also commonly known as an array. It stores objects sequentially and can be accessed via integer indices. In Groovy, a list looks something like this:

Groovy Cheat Sheet

A map holds a key-pair value based list where you can attach data to a custom named key. Rather than calling values based on integer based keys, this lets you use the custom named key instead.

We can also add lists into maps like this:

3. Conditionals

The most basic conditionals are if else statements. The result is a boolean that determines which block of code to execute next. An if else statement in Groovy looks like this:

You can also do the expressions that evaluates to a boolean within the if statement. For example:

&& and || operators are conditionals known as ‘and’ and ‘or’. The statement needs to evaluate to true in order to proceed, or it goes down to the else block.

Another condition you can use to give your if else statement options beyond just the two is to use the else if option.

For example:

4. Loops

A loop is a set of code that we want to repeat under certain circumstances. Three common types of loops are: while, collection iteration, and recursion.

Let’s begin with the first of the three: the while loop.

Groovy Cheat Sheet

A while loop runs through a set of execution statements until the condition is satisfied. It accepts an argument to determine the validity of the boolean evaluation.

Writing a while loop in Groovy looks something like this:

A collection iteration is when you work through a list, iterating it until the list is exhausted. To do this, you use the each method like this:

You can also iterate through maps like this:

And finally, we have recursions. A recursion is a function that calls itself when the conditions are met. For example, here is a function that calls itself.

5. Writing JSON

You can write lists and do things to it. But what we really want is to turn this data into something more universally accessible – that is, make it into valid JSON.

Groovy includes simple classes for writing to JSON. All you have to do is import JsonBuilder and use on the list map you want to transform.

The new File() will create a new file object that we assign our transformed families JSON to.

6. Reading JSON

JSON is the most popular method of moving structured data between different applications and networks. Let’s pretend we get given a JSON file containing all a giant list of inventory information.

Groovy cheat sheet

The JsonSlurper lets you create a new instance and parseText method allows you to pass texts into your file, allowing you to modify and do what you want with the data set.

7. HTTP requests

An isolated application that doesn’t communicate with anything isn’t much use in this day and age. there’s a lot of data that gets transferred over the Internet and to do this, we need the ability to create HTTP requests.

Here’s a scaffold of how you’d write it.

Final thoughts

I hope you found this reference helpful. It’s a quick cheat sheet style list of commonly used things you’d find in programming, but in a Groovy flavor.

If you’re coming from a Java background, everything should feel almost at home. If you’re coming from a JavaScript environment, the elements of Groovy are generally identifiable.

The fundamentals of programming are still the same, no matter where you go. Most of the time, once you learn one language, the ideas are transferable and all you have to do is learn the syntax.

From what you can see above, the ideas are mostly the same. So in theory, you can potentially close your eyes and pretend it’s Java or JavaScript but with a few differences in syntax and how certain things are run.

But for the scope of this piece, the content presented here should be enough to get you started on your next Groovy based project.