Gradle Goodness Gr8Conf 2014

Hubert Klein Ikkink (mrhaki)

Workshop structure

Groovy history

Groovy is Java on steroids

Getting started

Install - Requirements

Install

  1. Uncompress the zip file to c:\gr8conf\groovy\ (Windows) or ~/gr8conf/groovy (Mac/Linux).
  2. Set a GROOVY_HOME environment variable to the directory from the previous step.
  3. Add the [GROOVY_HOME]\bin directory to your system path.

    • Windows: Use your computer’s properties to set the environment variable PATH and add %GROOVY_HOME%\bin
    • Linux/Mac OS X: open your shell and set the PATH variable

      > export PATH=$PATH:$GROOVY_HOME/bin

Validate installation

Running Groovy code

Transforming Java to Groovy

public class UsingJava {
    private String name;

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    @Override
    public String toString() {
        return "Using Java " + getName();
    }

    public static void main(String[] args) {
        UsingJava usingJava = new UsingJava();
        usingJava.setName("Geeks");
        System.out.println(usingJava);
    }
}

Transforming Java to Groovy

class UsingJava {
    String name

    String toString() {
        "Using Java $name"
    }

    static void main(String[] args) {
        UsingJava usingJava = new UsingJava(name: ‘Geeks’)
        println usingJava
    }
}

Types

String variations and GString

Exercise

  1. Play around with different types in Groovy.
  2. Define a GString with an expression.

Exercise

// dynamic type
def s = 'Hello'
assert s instanceof String
s = 42
assert s instanceof Integer

// GString
def user = 'mrhaki'
def greeting = "Hi, ${user}. Your name has ${user.size()} characters"

assert greeting == 'Hi, mrhaki. Your name has 6 characters'

Regular Expressions

Objects

Exercise

  1. Write a Groovy class with a String property username
  2. Create an instance of the class and set and read a value for the username property.
  3. Add a new method greeting that returns a salutation for the username, like Hi, mrhaki.
  4. Change the method signature and add a argument with a default value for the salutation, eg. the default is Hello

Exercise

class User {
  String username

  String greeting(salutation = 'Hello') {
      "${salutation}, ${username}."
  }
}

User user = new User(username: 'mrhaki')
assert user.username == 'mrhaki'

user.setUsername('Hubert')
user.username = 'Hubert'

assert user.greeting() == 'Hello, Hubert.'
assert user.greeting('Hi') == 'Hi, Hubert.'

Control structures

Operators

Break

Closures

Exercise

  1. Write a 3-argument closure, where the third argument has a default value gr8conf. The closure result returns the concatenation of the 3 arguments.
  2. Print the value of the closure.
  3. Make a second closure, that curries the first closure, so the 2nd argument is set to love.
  4. Make a method, taking two arguments: the closure and a String value, eg. I, we or you. The method should print out the result of the closure we created at the previous step.

Exercise

def gr8 = { arg1, arg2, arg3 = 'gr8conf' ->
    "$arg1 $arg2 $arg3"
}
println gr8("I'm","going to")

def cl = gr8.ncurry(1,'love')

def m(arg, closure) {
    println closure(arg)
}

m('I',cl)

Collections

Exercise

  1. Create a new list with String values, boolean values and numbers.
  2. Use a finder method to find all String values.
  3. Check if any String elements is at least 4 characters.
  4. Create a new Map with some keys and values.
  5. Transform all key/value pairs to a new list where each element in the list is the concatenation of the key and value.

Exercise

def list = ['abc', 100, 'defg', false]

assert list.findAll { it instanceof String } == ['abc', 'defg']
assert list.any { it instanceof String && it.size() >= 4 }

def map = [key: 'value', otherKey: 42, name: 'mrhaki']

assert map.collect { key, value ->
    "${key} = ${value}" } == ['key = value', 'otherKey = 42', 'name = mrhaki']

File I/O

XML

Exercise

  1. Print the list of the book title from this url: http://mrhaki.com/books.xml
  2. Get the title of the book with id == 2

Exercise

import groovy.xml.*

def url = "http://mrhaki.com/books.xml".toURL()

def slurper = new XmlSlurper()

def xml = slurper.parseText(url.text)

println xml.book.collect { it.title }

println xml.book.find { it.@id == '2' }.title

AST (Abstract Syntax Tree)

Metaprogramming

Groovy ecosystem

Thank you

#