Groovy Gradle

Patrick van Dissel

groovy logo colored
A multi-faceted language
for the Java platform
— groovy-lang.org

Groovy is a powerful,
optionally typed and dynamic language,
with static-typing and static compilation
capabilities, for the Java platform

aimed at
multiplying developers’ 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 features,

including scripting capabilities,
Domain-Specific Language authoring,
runtime and compile-time meta-programming and
functional programming.

So, what is Groovy?

  • Java compatible

  • Enhanced Java-syntax

  • Dynamic

  • Scriptable

Superset of Java
— Groovy' description back in the days

Groovy vs Java

groovyduke

Default imports

import java.io.*;
import java.lang.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.*;
import java.util.*;
import groovy.lang.*;
import groovy.util.*;

Multi-methods

int method(String arg) { return 1; }
int method(Object arg) { return 2; }
Object o = "Object";
int result = method(o);
assertEquals(2, result);

vs

assertEquals(1, result);

Array initializers

int[] array = { 1, 2, 3 };

vs

int[] array = [ 1, 2, 3 ];

Package scope visibility

class Person {
    String name
}

vs

class Person {
    @PackageScope String name
}

ARM blocks

Path file = Paths.get("/path/to/file");
Charset charset = Charset.forName("UTF-8");
try (BufferedReader reader = Files.newBufferedReader(file, charset)) {
    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}

vs

new File('/path/to/file').eachLine('UTF-8') {
   println it
}

Inner classes

inner class

Lambdas

Runnable run = () -> System.out.println("Run");
list.forEach(System.out::println);

vs

Runnable run = { println 'run' }
list.each(this.&println)
list.each { println it }           // preferred, for simple cases
list.each { item -> println item } // preferred

GStrings

int val = 2;
assert "c".getClass() == String.class;
assert false == "c${val}".equals("c2");
assert true == ("c" + val).equals("c2");

vs

def val = 2
assert 'c'.class == String
assert "c".class == String
assert "c${val}".class in GString

String and Character literals

// for single char strings, both are the same
assert ((char) "c").class == Character
assert ("c" as char).class == Character

// for multi char strings they are not
try {
    ((char) 'cx') == 'c'
    assert false: 'will fail - not castable'
} catch (GroovyCastException e) {
}
assert ('cx' as char) == 'c'
assert 'cx'.asType(char) == 'c'

Behaviour of ==

String a = new String("a");
String b = new String("a");
assert a != b;
assert true == a.equals(b);

vs

String a = new String("a")
String b = new String("a")
assert a == b
assert true == a.equals(b)
assert false == a.is(b)

Additional keywords

  • in

  • trait

gradle big
Open Source Build Automation
— gradle.org

From command line to IDE
to continuous integration, only one
Enterprise build automation system to rule them all.

Declare and execute all tasks necessary to
compile, test, package and ship
multi-language multi-platform
multi-project and multi-channel
software, SaaS and Mobile Apps.

Lets do this together

We’ve got a Maven, Java, Junit project:

Lets migrate it to Gradle, Groovy, Spock:

gradle big

Features

gradle features

Lessons learned

gradle learned from others

Integration

gradle integration

Expressive build language

gradle api

High performance builds

  • Cache everything

  • Incremental build

  • Parallelize

  • Gradle Daemon

~/.gradle/gradle.properties
org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.parallel=true

Evolving

ReleaseInitialCycle

Java

1995

~2 years

Ant

2000

month <-> year

Maven

2002

month <-> year

Ivy

2004

month <-> year

Gradle

2009

~6 weeks

sbt

2011

days <-> months

Use cases

LinkedIn
Netflix
groovy logo colored
  • Java compatible

  • Enhanced Java-syntax

  • Dynamic

  • Scriptable

gradle big
  • Polyglot builds

  • Tool integrations

  • Robust dependency management

  • Powerful yet concise logic

  • High performance builds

Resources

Thanks