Slide 1

Slide 1 text

Gradle Build Automation Made Easy Gary Hale Availity, LLC

Slide 2

Slide 2 text

Agenda ○ Why Gradle? ○ Gradle Concepts ○ Writing Gradle Scripts ● Custom Tasks ● Multi-project Builds ● Customizing Distributions ● Dependency Management ○ Interesting Plugins ○ Q & A

Slide 3

Slide 3 text

Why Gradle? ○ Evolutionary Path: Dark Ages -> Ant -> Maven -> Gradle ○ Gradle = Flexibility of Ant + Convenience of Maven + Power of Groovy ○ Gradle makes simple things simple and complicated things possible.

Slide 4

Slide 4 text

Why Gradle? “There is no one-size-fits-all process for builds. Therefore Gradle does not impose a rigid process over people. Yet we think finding and describing YOUR process is very important. And so, Gradle has the very best support for describing it.” http://www.gradle.org

Slide 5

Slide 5 text

Why Gradle? ○ Problems with Availity’s previous build system (Ant-based): ● Duplication of information ● Dependencies were informal and arbitrary ● Scripts were convoluted and unreadable ● Scripts were unnecessarily large ● Changes to a core project could require changes to dozens of other projects

Slide 6

Slide 6 text

Why Gradle? ○ How does Gradle fix this? ● One place to change anything (DRY) ● Shortest path to what you want to do ● Concise, readable, powerful scripts ● Dependencies are real and consistent ● Full lifecycle functionality (build, test, packaging, upload, IDE metadata generation)

Slide 7

Slide 7 text

Why Gradle? ○ Availity’s build scripts were reduced in size by roughly 66% on average and as much as 95%*. * Results are not typical.

Slide 8

Slide 8 text

Why Gradle? ○ Who’s using Gradle? ● Hibernate ● Groovy ● Grails ● Spring Integration ● Spring Security ● Many others

Slide 9

Slide 9 text

Gradle Concepts ○ Groovy-based scripts with Gradle DSL ○ Convention over configuration ○ Highly declarative but any functionality can be overridden and customized ○ Based on Ant + Ivy

Slide 10

Slide 10 text

Gradle Concepts ○ Informational commands: $ gradle tasks $ gradle dependencies $ gradle projects $ gradle --profile

Slide 11

Slide 11 text

Gradle Concepts ○ Build Lifecycle ● Initialization ○ What projects are in the build? ● Configuration ○ How are those projects configured? ● Execution ○ Where the rubber meets the road

Slide 12

Slide 12 text

Gradle Concepts ○ Basic Concepts ● Tasks ● Dependencies ● Configurations

Slide 13

Slide 13 text

Gradle Tasks task(‘first’) << { println ‘first task!’ } task(‘second’) << { println ‘second task!’ } second.dependsOn first $ gradle second :first first task! :second second task! BUILD SUCCESSFUL Total time: 1.389 secs

Slide 14

Slide 14 text

Gradle Tasks task('myCopy', type: Copy) { from('dir1') into('dir2') doFirst { println 'Before Copy:' fileTree('dir2').each { println '\t' + it.name } } doLast { println 'After Copy:' fileTree('dir2').each { println '\t' + it.name } } } $ gradle myCopy :myCopy Before Copy: After Copy: a.txt b.txt c.txt BUILD SUCCESSFUL Total time: 2.264 secs

Slide 15

Slide 15 text

Gradle Tasks ○ Plugins add tasks to the build ● apply plugin: ‘java’

Slide 16

Slide 16 text

Gradle Tasks apply plugin: 'java' task('listResources') << { sourceSets.main.resources.srcDirs. collect { fileTree(it).each { println '\t' + it.name } } } processResources { exclude('*.xml') eachFile { println '\tprocessing ' + it. path } dependsOn listResources } $ gradle processResources :listResources a.properties b.xml c.properties :processResources processing a.properties processing c.properties BUILD SUCCESSFUL Total time: 2.107 secs

Slide 17

Slide 17 text

Gradle Tasks ○ Custom Task ● Place in a special directory called buildSrc ● Must extend DefaultTask and implement a task annotated with TaskAction ● Can be abstracted to a separate project and imported at build time like any other dependency

Slide 18

Slide 18 text

Gradle Tasks package org.gradle.hello import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction class HelloTask extends DefaultTask { def String yourname = "whoever you are" @TaskAction def hello() { println 'Hello ' + yourname + '!' } }

Slide 19

Slide 19 text

Dependency Management ○ Dependencies ● Project: dependencies { compile project(‘:common’) } ● External: dependencies { compile ‘commons-lang:commons-lang:2.4’ } ● File-based: dependencies { compile files(opensource_lib + ‘/json-lib/xom-1.2.6.jar‘) }

Slide 20

Slide 20 text

Dependency Management ○ Dependencies have different scopes: dependencies { compile ‘org.springframework:spring-core:3.0’ providedCompile ‘javax.servlet:servlet-api:2.5’ runtime ‘commons-vfs:commons-vfs:1.0’ testCompile ‘junit:junit:4.8’ }

Slide 21

Slide 21 text

Dependency Management ○ Dependencies can exclude transitive dependencies: dependencies { compile(‘commons-dbcp:commons-dbcp:1.3’) { transitive false } } dependencies { compile(‘commons-dbcp:commons-dbcp:1.3’) { exclude module: ‘commons-pool’ } }

Slide 22

Slide 22 text

Multi-project Builds ○ Ideally, a single root project with children in a hierarchy is desirable. ○ Gradle also supports flat layouts. ○ During initialization, settings.gradle sets up what projects are included in the build.

Slide 23

Slide 23 text

Multi-project Builds Root Child 1 Child 2 build.gradle settings.gradle build.gradle build.gradle

Slide 24

Slide 24 text

Multi-project Builds ○ allprojects – applies the configuration to all projects including the root ○ subprojects – applies the configuration to just the subprojects ○ Subprojects can override any or all configuration injected by the root.

Slide 25

Slide 25 text

Customizing Distributions ○ Configurations define sets of dependencies ○ Dependencies couple libraries to configurations ○ Artifacts couple configurations to assembly tasks

Slide 26

Slide 26 text

Customized Distributions task(‘myCustomConfigZip’, type: Zip) { from configurations.myCustomConfig } configurations { myCustomConfig extends runtime } dependencies { myCustomConfig ‘some:special-jar:1.0’ } artifacts { myCustomConfig myCustomConfigZip }

Slide 27

Slide 27 text

Customized Jars Child2/build.gradle: task(‘testArtifactJar’, type: Jar) { from sourceSets.test.classes classifier = 'test‘ } configurations { testArtifacts extends testRuntime } artifacts { testArtifacts testArtifactJar } Child1/build.gradle: dependencies { project(path: ':child2', configuration: ‘testArtifacts') }

Slide 28

Slide 28 text

Integrating with Ant ○ Gradle considers Ant a “first-class citizen”. ○ Your ant expertise can be directly applied in a Gradle implementation. ○ Ant tasks can be imported into Gradle from a custom script and executed.

Slide 29

Slide 29 text

Integrating with Ant ○ Ant snippet:

Slide 30

Slide 30 text

Integrating with Ant ○ Gradle equivalent: ant.fileset(dir: ‘src’, casesensitive: ‘true’) { include(name: ‘**/*.java’) exclude(name: ‘**/*Test*’) }

Slide 31

Slide 31 text

Interesting Plugins ○ Eclipse plugin apply plugin: ‘eclipse’ ● Adds tasks: ○ eclipseClasspath ○ eclipseProject ○ eclipseJdt ○ eclipseWtpComponent ○ eclipseWtpFacet (WAR Only)

Slide 32

Slide 32 text

Interesting Plugins ○ WAR plugin apply plugin: ‘war’ ● Adds tasks: ○ war ● Adds configurations: ○ providedCompile ○ providedRuntime

Slide 33

Slide 33 text

Interesting Plugins ○ Jetty plugin apply plugin: ‘jetty’ ● Adds tasks: ○ runJetty ● Allows war to be run in a local jetty container.

Slide 34

Slide 34 text

Interesting Plugins ○ Maven plugin apply plugin: ‘maven’ ● Adds tasks: ○ install ● Allows maven metadata to be generated and published to a maven repository.

Slide 35

Slide 35 text

Interesting Plugins ○ Application plugin apply plugin: ‘application’ ● Adds tasks: ○ run ○ startScripts ○ Install ○ distZip

Slide 36

Slide 36 text

Interesting Plugins ○ Sonar plugin apply plugin: ‘sonar’ ● Adds tasks: ○ sonar ● Allows code quality statistics to be generated and published to a Sonar instance.

Slide 37

Slide 37 text

Links ○ Gradle Homepage ● http://gradle.org/ ○ Gradle User Guide ● http://www.gradle. org/current/docs/userguide/userguide_single.html ○ Gradle DSL ● http://www.gradle.org/current/docs/dsl/index.html ○ Gradle Wiki ● http://wiki.gradle.org/display/GRADLE/Home ○ Gradle Cookbook ● http://wiki.gradle.org/display/GRADLE/Cookbook ○ Other Good stuff ● http://wiki.gradle.org/display/GRADLE/External+Resources