Corneil du Plessis
Gradle: The Build system you have been waiting for!
DevOps &
Automation
Gradle: Introduction

Scope

History Lesson
− Make
− Ant
− Maven

Gradle
− What is it?
− Is Groovy
− Artefacts
− Builtin Support
− Case studies
Gradle: Make

Targets, dependencies, rules

Sample:
CFLAGS ?= -g
all: helloworld
helloworld: helloworld.o
# Commands start with TAB not spaces
$(CC) $(LDFLAGS) -o $@ $^
helloworld.o: helloworld.c
$(CC) $(CFLAGS) -c -o $@ $<

Suffix rules:
.c.o:
$(CC) $(CFLAGS) -c $<
Gradle: Ant

Projects, targets, dependency, built-in tasks, taskdef.
<project name="SampleWebApp" default="all" basedir=".">
<property name="app.name" value="SampleWebApp"/>
<property name="lib.dir" value="${basedir}/lib"/>
<property name="work.dir" value="${basedir}/work"/>
<property name="dist.dir" value="${basedir}/dist"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="web.dir" value="${basedir}/web"/>
<path id="compile.classpath">
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
</path>
Gradle: Ant – cont.
<target name="all" depends="clean,compile,dist" description="Clean dirs,compile,create a WAR"/>
<target name="clean" description="Delete old work and dist directories">
<delete dir="${work.dir}"/>
<delete dir="${dist.dir}"/>
</target>
<target name="prepare" depends="clean" description="Create work dirs copy static files to work dir">
<mkdir dir="${dist.dir}"/>
<mkdir dir="${work.dir}/WEB-INF/classes"/>
<copy todir="${work.dir}">
<fileset dir="${web.dir}"/>
</copy>
</target>
<target name="compile" depends="prepare" description="Compile sources and copy to WEB-INF/classes dir">
<javac srcdir="${src.dir}" destdir="${work.dir}/WEB-INF/classes">
<classpath refid="compile.classpath"/>
</javac>
<copy todir="${work.dir}/WEB-INF/classes">
<fileset dir="${src.dir}" excludes="**/*.java"/>
</copy>
</target>
<target name="dist" depends="compile" description="Create WAR file for binary distribution">
<jar jarfile="${dist.dir}/${app.name}.war" basedir="${work.dir}"/>
</target>
</project>
Gradle: Maven

POM, Goals, Lifecycle, Plugins, Profiles

Maven Repository
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.jumpco.samples</groupId>
<artifactId>SampleWebApp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>SampleWebApp Maven Web App</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.2.4.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<jdk.version>1.8</jdk.version>
</properties>
Gradle: Maven – cont
<dependencies>
<!-- Spring 3 dependencies →
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Gradle: Maven – cont
<build>
<finalName>SampleWebApp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Gradle: What is it?

Gradle is a Groovy DSL for creating build scripts

Gradle has a beautiful designed model for Tasks,
Dependencies, Conventions etc.

Gradle understands Ivy and Maven repositories

Gradle can execute Ant scripts and tasks directly

This is a valid Gradle build script:
apply plugin: 'war'
Gradle: What does it look like?
build.gradle
apply plugin: 'java'
apply plugin: 'war'
repositories {
mavenCentral()
}
group = 'io.jumpco.samples'
version = '1.0-SNAPSHOT'
sourceCompatibility = 1.8
dependencies {
compile “org.springframework:spring-core:$springVersion”
compile “org.springframework:spring-web:$springVersion”
compile “org.springframework:spring-webmvc:$springVersion”
testCompile group: 'junit', name: 'junit', version: junitVersion
}
settings.gradle
rootProject.name = 'SampleWebApp'
gradle.properties
springVersion=4.2.4.RELEASE
junitVersion=4.11
Gradle: Is Groovy

DSL Handlers

Closures
repositories {
maven {
url 'http://download.java.net/mave/2/'
}
}

Strings
'org.springframework:spring-core:' + springVersion
“org.springframework:spring-core:$springVersion”
Gradle: Artifacts
● build.gradle
– buildScript
– configurations
– dependencies
– apply plugin
– artifacts
– sourceSets
– other dsl sections and Groovy
● settings.gradle
– subprojects
● gradle.properties
– Global properties
● buildSrc
– Custom tasks
Gradle: Builtin Support

Ant Projects, Tasks

Java, Jar, War, Ear

Scala, Groovy, GNU Compilers, Clang, Visual C++

Build Reporting

ANTLR

OSGi

Sonar, Pmd, Jacoco, CheckStyle, FindBugs, CodeNarc,
JDepend

Maven Publish, Ivy Publish, Signing, Distribution

Jetty

Announcements (Twitter, Growl, Snarl, notify-send)
Gradle: IDE Support
● Eclipse
● IntelliJ
● Netbeans
● Use your imagination
Gradle: Create scripts

gradle init –type basic|pom|java-library

Create build.gradle and other files
− Basic, Java Library, Scala Library, Groovy Library
− Choose test framework
− Convert pom
●
IDE New Gradle Project
●
Checkout lazybones at
https://github.com/pledbrook/lazybones
Gradle: Wrapper
gradle wrapper
Creates gradlew, gradlew.bat, gradle folder. Small enough to
checkin.
./gradlew build
Downloads Gradle version and executes build task.
Gradle: Other

Gradle UI – Swing application

Gradle Daemon will remain running and will reload only
modified scripts to improve execution time.

--continuous option. Gradle will remain running and trigger
build when input artefacts modified.

New Build Model
Gradle: 3rd
Party plugins

http://plugins.gradle.org

Google Android Development

Bintray publishing.

Artifactory

Spring IO Framework

https://github.com/nebula-plugins

Google App Engine

Tomcat

lessCss, minCss, minJs
Gradle: Cases Studies
● 2000 components, 1000 release builds per day
● Nebula. Patched Gradle.
● Enterprise Projects with > 300 modules
● Partial Local build.
● Generate documentation
● Deployment automation
Gradle: Summary
● Made for Customisation
● Robust Dependency Management
● Build Reporting
● The same flexible Build Model for both continuous
integration and local development.
● One source of the truth
/* THANK YOU*/
Corneil du Plessis
JumpCo (formerly TSC Technologies)
corneil@jumpco.io
corneil.duplessis@gmail.com
https://about.me/corneil
Demo project:
https://github.com/corneil/gradle-docs
http://www.devconf.co.za/Rate

Gradle: The Build System you have been waiting for!

  • 1.
    Corneil du Plessis Gradle:The Build system you have been waiting for! DevOps & Automation
  • 2.
    Gradle: Introduction  Scope  History Lesson −Make − Ant − Maven  Gradle − What is it? − Is Groovy − Artefacts − Builtin Support − Case studies
  • 3.
    Gradle: Make  Targets, dependencies,rules  Sample: CFLAGS ?= -g all: helloworld helloworld: helloworld.o # Commands start with TAB not spaces $(CC) $(LDFLAGS) -o $@ $^ helloworld.o: helloworld.c $(CC) $(CFLAGS) -c -o $@ $<  Suffix rules: .c.o: $(CC) $(CFLAGS) -c $<
  • 4.
    Gradle: Ant  Projects, targets,dependency, built-in tasks, taskdef. <project name="SampleWebApp" default="all" basedir="."> <property name="app.name" value="SampleWebApp"/> <property name="lib.dir" value="${basedir}/lib"/> <property name="work.dir" value="${basedir}/work"/> <property name="dist.dir" value="${basedir}/dist"/> <property name="src.dir" value="${basedir}/src"/> <property name="web.dir" value="${basedir}/web"/> <path id="compile.classpath"> <fileset dir="${lib.dir}"> <include name="*.jar"/> </fileset> </path>
  • 5.
    Gradle: Ant –cont. <target name="all" depends="clean,compile,dist" description="Clean dirs,compile,create a WAR"/> <target name="clean" description="Delete old work and dist directories"> <delete dir="${work.dir}"/> <delete dir="${dist.dir}"/> </target> <target name="prepare" depends="clean" description="Create work dirs copy static files to work dir"> <mkdir dir="${dist.dir}"/> <mkdir dir="${work.dir}/WEB-INF/classes"/> <copy todir="${work.dir}"> <fileset dir="${web.dir}"/> </copy> </target> <target name="compile" depends="prepare" description="Compile sources and copy to WEB-INF/classes dir"> <javac srcdir="${src.dir}" destdir="${work.dir}/WEB-INF/classes"> <classpath refid="compile.classpath"/> </javac> <copy todir="${work.dir}/WEB-INF/classes"> <fileset dir="${src.dir}" excludes="**/*.java"/> </copy> </target> <target name="dist" depends="compile" description="Create WAR file for binary distribution"> <jar jarfile="${dist.dir}/${app.name}.war" basedir="${work.dir}"/> </target> </project>
  • 6.
    Gradle: Maven  POM, Goals,Lifecycle, Plugins, Profiles  Maven Repository <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.jumpco.samples</groupId> <artifactId>SampleWebApp</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>SampleWebApp Maven Web App</name> <url>http://maven.apache.org</url> <properties> <spring.version>4.2.4.RELEASE</spring.version> <junit.version>4.12</junit.version> <jdk.version>1.8</jdk.version> </properties>
  • 7.
    Gradle: Maven –cont <dependencies> <!-- Spring 3 dependencies → <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies>
  • 8.
    Gradle: Maven –cont <build> <finalName>SampleWebApp</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>${jdk.version}</source> <target>${jdk.version}</target> </configuration> </plugin> </plugins> </build> </project>
  • 9.
    Gradle: What isit?  Gradle is a Groovy DSL for creating build scripts  Gradle has a beautiful designed model for Tasks, Dependencies, Conventions etc.  Gradle understands Ivy and Maven repositories  Gradle can execute Ant scripts and tasks directly  This is a valid Gradle build script: apply plugin: 'war'
  • 10.
    Gradle: What doesit look like? build.gradle apply plugin: 'java' apply plugin: 'war' repositories { mavenCentral() } group = 'io.jumpco.samples' version = '1.0-SNAPSHOT' sourceCompatibility = 1.8 dependencies { compile “org.springframework:spring-core:$springVersion” compile “org.springframework:spring-web:$springVersion” compile “org.springframework:spring-webmvc:$springVersion” testCompile group: 'junit', name: 'junit', version: junitVersion } settings.gradle rootProject.name = 'SampleWebApp' gradle.properties springVersion=4.2.4.RELEASE junitVersion=4.11
  • 11.
    Gradle: Is Groovy  DSLHandlers  Closures repositories { maven { url 'http://download.java.net/mave/2/' } }  Strings 'org.springframework:spring-core:' + springVersion “org.springframework:spring-core:$springVersion”
  • 12.
    Gradle: Artifacts ● build.gradle –buildScript – configurations – dependencies – apply plugin – artifacts – sourceSets – other dsl sections and Groovy ● settings.gradle – subprojects ● gradle.properties – Global properties ● buildSrc – Custom tasks
  • 13.
    Gradle: Builtin Support  AntProjects, Tasks  Java, Jar, War, Ear  Scala, Groovy, GNU Compilers, Clang, Visual C++  Build Reporting  ANTLR  OSGi  Sonar, Pmd, Jacoco, CheckStyle, FindBugs, CodeNarc, JDepend  Maven Publish, Ivy Publish, Signing, Distribution  Jetty  Announcements (Twitter, Growl, Snarl, notify-send)
  • 14.
    Gradle: IDE Support ●Eclipse ● IntelliJ ● Netbeans ● Use your imagination
  • 15.
    Gradle: Create scripts  gradleinit –type basic|pom|java-library  Create build.gradle and other files − Basic, Java Library, Scala Library, Groovy Library − Choose test framework − Convert pom ● IDE New Gradle Project ● Checkout lazybones at https://github.com/pledbrook/lazybones
  • 16.
    Gradle: Wrapper gradle wrapper Createsgradlew, gradlew.bat, gradle folder. Small enough to checkin. ./gradlew build Downloads Gradle version and executes build task.
  • 17.
    Gradle: Other  Gradle UI– Swing application  Gradle Daemon will remain running and will reload only modified scripts to improve execution time.  --continuous option. Gradle will remain running and trigger build when input artefacts modified.  New Build Model
  • 18.
    Gradle: 3rd Party plugins  http://plugins.gradle.org  GoogleAndroid Development  Bintray publishing.  Artifactory  Spring IO Framework  https://github.com/nebula-plugins  Google App Engine  Tomcat  lessCss, minCss, minJs
  • 19.
    Gradle: Cases Studies ●2000 components, 1000 release builds per day ● Nebula. Patched Gradle. ● Enterprise Projects with > 300 modules ● Partial Local build. ● Generate documentation ● Deployment automation
  • 20.
    Gradle: Summary ● Madefor Customisation ● Robust Dependency Management ● Build Reporting ● The same flexible Build Model for both continuous integration and local development. ● One source of the truth
  • 21.
    /* THANK YOU*/ Corneildu Plessis JumpCo (formerly TSC Technologies) corneil@jumpco.io corneil.duplessis@gmail.com https://about.me/corneil Demo project: https://github.com/corneil/gradle-docs http://www.devconf.co.za/Rate