Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gradle - Build Automation Made Easy

Gradle - Build Automation Made Easy

JAXJUG Presentation on Gradle

Gary Hale

May 11, 2011
Tweet

More Decks by Gary Hale

Other Decks in Technology

Transcript

  1. Agenda ◦ Why Gradle? ◦ Gradle Concepts ◦ Writing Gradle

    Scripts • Custom Tasks • Multi-project Builds • Customizing Distributions • Dependency Management ◦ Interesting Plugins ◦ Q & A
  2. 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.
  3. 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
  4. 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
  5. 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)
  6. Why Gradle? ◦ Availity’s build scripts were reduced in size

    by roughly 66% on average and as much as 95%*. * Results are not typical.
  7. Why Gradle? ◦ Who’s using Gradle? • Hibernate • Groovy

    • Grails • Spring Integration • Spring Security • Many others
  8. 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
  9. Gradle Concepts ◦ Informational commands: $ gradle tasks $ gradle

    dependencies $ gradle projects $ gradle --profile <task>
  10. Gradle Concepts ◦ Build Lifecycle • Initialization ◦ What projects

    are in the build? • Configuration ◦ How are those projects configured? • Execution ◦ Where the rubber meets the road
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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 + '!' } }
  16. 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‘) }
  17. 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’ }
  18. 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’ } }
  19. 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.
  20. 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.
  21. Customizing Distributions ◦ Configurations define sets of dependencies ◦ Dependencies

    couple libraries to configurations ◦ Artifacts couple configurations to assembly tasks
  22. Customized Distributions task(‘myCustomConfigZip’, type: Zip) { from configurations.myCustomConfig } configurations

    { myCustomConfig extends runtime } dependencies { myCustomConfig ‘some:special-jar:1.0’ } artifacts { myCustomConfig myCustomConfigZip }
  23. 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') }
  24. 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.
  25. Integrating with Ant ◦ Ant snippet: <fileset dir= "src" casesensitive="

    yes"> <include name="**/*.java"/> <exclude name="**/*Test*"/> </fileset>
  26. Integrating with Ant ◦ Gradle equivalent: ant.fileset(dir: ‘src’, casesensitive: ‘true’)

    { include(name: ‘**/*.java’) exclude(name: ‘**/*Test*’) }
  27. Interesting Plugins ◦ Eclipse plugin apply plugin: ‘eclipse’ • Adds

    tasks: ◦ eclipseClasspath ◦ eclipseProject ◦ eclipseJdt ◦ eclipseWtpComponent ◦ eclipseWtpFacet (WAR Only)
  28. Interesting Plugins ◦ WAR plugin apply plugin: ‘war’ • Adds

    tasks: ◦ war • Adds configurations: ◦ providedCompile ◦ providedRuntime
  29. Interesting Plugins ◦ Jetty plugin apply plugin: ‘jetty’ • Adds

    tasks: ◦ runJetty • Allows war to be run in a local jetty container.
  30. Interesting Plugins ◦ Maven plugin apply plugin: ‘maven’ • Adds

    tasks: ◦ install • Allows maven metadata to be generated and published to a maven repository.
  31. Interesting Plugins ◦ Application plugin apply plugin: ‘application’ • Adds

    tasks: ◦ run ◦ startScripts ◦ Install ◦ distZip
  32. Interesting Plugins ◦ Sonar plugin apply plugin: ‘sonar’ • Adds

    tasks: ◦ sonar • Allows code quality statistics to be generated and published to a Sonar instance.
  33. 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