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

Introduction to Gradle

Gasol Wu
September 15, 2012

Introduction to Gradle

Gasol Wu

September 15, 2012
Tweet

More Decks by Gasol Wu

Other Decks in Programming

Transcript

  1. Introduction to Gradle
    Gasol Wu
    2012/9/15

    View Slide

  2. Who am I
    • Work at KKBOX
    • http://github.com/Gasol
    • Twitter @gasolwu

    View Slide

  3. Build System History
    3
    2
    +

    View Slide

  4. The Problem is ...
    !

    View Slide



































  5. <fileset dir="${srcTest}">

    fileset>















    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">
    4.0.0
    grId
    coc-comparison
    jar
    1.0-SNAPSHOT


    commons-lang
    commons-lang
    2.5


    junit
    junit
    4.8.1
    test




    1.6

    1.6


    apply plugin: 'java'
    version="1.0-SNAPSHOT"
    group="grId"
    archivesBaseName="coc-comparison"
    repositories {
    mavenCentral()
    }
    dependencies {
    compile 'commons-lang:commons-lang:2.5'
    testCompile 'junit:junit:4.8.1'
    }
    2
    http://kaczanowscy.pl/tomek/2010-11/build-script-length-maven3-polyglot-maven-gradle-ant

    View Slide

  6. Size Comparison
    Size Comparison
    Size Comparison
    chars lines
    Ant 1733 102
    Maven 2 904 31
    Gradle 224 17

    View Slide

  7. • Quite verbose
    • Expressiveness of XML is limited
    • It’s hard to trace

    View Slide

  8. So,
    What We Really Want?

    View Slide

  9. • Easy to customize and extend
    • Abstraction and structuring

    View Slide

  10. A Better Way To Build!

    View Slide

  11. Gradle
    • Ease of migration
    • Declarative builds and build-by-convention
    • Rich DSL based on Groovy
    • Leverage both Ant and Maven
    • Gradle Wrapper

    View Slide

  12. Gradle is Groovy
    Groovy is Java

    View Slide

  13. // build.gradle
    System.out.println(“Java Way”);
    4.times {
    print it
    }

    View Slide

  14. > gradle
    Java Way
    0
    1
    2
    3
    :help
    Welcome to Gradle 1.2.
    To run a build, run gradle ...
    To see a list of available tasks, run gradle tasks
    To see a list of command-line options, run gradle --help
    BUILD SUCCESSFUL
    Total time: 2.454 secs

    View Slide

  15. Task
    task myTask
    task myTask { configure closure }
    task myType << { task action }
    task myTask(type: SomeType)
    task myTask(type: SomeType) { configure closure}

    View Slide

  16. Hello World
    // build.gradle
    task hello {
    doLast {
    println ‘Hello World!’
    }
    }
    > gradle -q hello
    Hello World!

    View Slide

  17. A shortcut task
    definition
    task hello << {
    println ‘Hello World!’
    }

    View Slide

  18. Build scripts are code
    task upper << {
    String someString = ‘mY_nAmE’
    println “Original: $someString“
    println “Upper: “ + someString.toUpperCase()
    }
    > gradle -q upper
    Original: mY_nAmE
    Upper: MY_NAME

    View Slide

  19. Groovy in Gradle’s
    tasks
    task count << {
    4.times {
    print “$it “
    }
    }
    > gradle -q count
    0 1 2 3

    View Slide

  20. Task dependencies
    task hello << {
    println ‘Hello World’
    }
    task intro(dependsOn: hello) << {
    println “I’m Gradle”
    }
    > gradle -q intro
    Hello World!
    I’m Gradle

    View Slide

  21. Lazy dependsOn
    task X(dependsOn: ‘Y’) << {
    println ‘X’
    }
    task Y << {
    println ‘Y’
    }
    > gradle -q X
    Y
    X

    View Slide

  22. Dynamic tasks
    4.times { counter ->
    task “task$counter” << {
    println “I’m task number $counter”
    }
    }
    > gradle -q task1
    I’m task number 1

    View Slide

  23. Default tasks
    defaultTasks ‘clean’, ‘run’
    task clean << {
    println ‘Default Cleaning!’
    }
    task run << {
    println ‘Default Running!’
    }
    task other << {
    println “I’m not a default task!”
    }
    > gradle -q
    Default Cleaning!
    Default Running!

    View Slide

  24. Configure by DAG
    task distribution << {
    println "We build the zip with version=$version"
    }
    task release(dependsOn: 'distribution') << {
    println 'We release now'
    }
    gradle.taskGraph.whenReady {
    if (it.hasTask(release)) {
    version = '1.0'
    } else {
    version = '1.0-SNAPSHOT'
    }
    }

    View Slide

  25. Build Phases
    • Initialization
    • Configuration
    • The build scripts of all projects which are part
    of the build are executed.
    • Execution

    View Slide

  26. Build phases (cont.)
    // settings.gradle
    println ‘initialization phase’
    // build.gradle
    println ‘configuration phase’
    task configured {
    println ‘configuration phase’
    }
    task test << {
    println ‘execution phase’
    }

    View Slide

  27. Build phases (output)
    > gradle test
    initialization phase
    initialization phase
    execution phase
    :test
    BUILD SUCCESSFUL
    Total time: 1 secs

    View Slide

  28. Ant are first class citizens
    // build.xml






    // build.gradle
    ant.importBuild ‘build.xml’
    task hello(dependsOn: helloFromAnt) << {
    println ‘Hello ‘ + ant.gradleName
    }

    View Slide

  29. Ant are first class
    citizens (cont.)
    > gradle tasks --all
    /* skip */
    Other tasks
    ----------
    hello
    helloFromAnt
    > gradle hello
    :helloFromAnt
    [ant:echo] Hello Ant!
    :hello
    Hello Gradle
    BUILD SUCCESSFUL
    Total time: 2.015 secs

    View Slide

  30. A basic Java project
    apply plugin: ‘java’

    View Slide

  31. Directory Structure By Default
    .
    ├── build.gradle
    └── src
    ├── main
    │ ├── java
    │ └── resources
    └── test
    ├── java
    └── resources
    sourceSets {
    main {
    java {
    srcDir ‘src/main/java’
    }
    resources {
    srcDir ‘src/main/resources’
    }
    }
    test {
    java {
    srcDir ‘src/test/java’
    }
    resources {
    srcDir ‘src/test/resources’
    }
    }
    }

    View Slide

  32. > gradle build
    :compileJava
    :processResources
    :classes
    :jar
    :assemble
    :compileTestJava
    :processTestResources
    :testClasses
    :test
    :check
    :build
    BUILD SUCCESSFUL
    Total time: 1 secs

    View Slide

  33. External Dependencies
    repositories {
    mavenCetral()
    }
    dependencies {
    compile group: ‘common-collections’, name: ‘common-
    collections’, version: ‘3.2’
    testCompile ‘junit:junit:4.+’
    }

    View Slide

  34. More dependencies
    dependencies {
    compile project(':shared')
    compile files('libs/a.jar',
    'libs/b.jar')
    runtime fileTree(dir: 'libs',
    include: '*.jar')
    compile gradleApi()
    groovy localGroovy()
    }

    View Slide

  35. More repositories
    repositories {
    mavenCentral()
    mavenLocal()
    maven {
    url "http://repo.mycompany.com/maven2"
    }
    ivy {
    url "http://repo.mycompany.com/maven2"
    layout "maven"
    credentials {
    username 'user'
    password 'password'
    }
    }
    }

    View Slide

  36. Customising Project
    apply plugin: 'java'
    apply plugin: 'maven'
    apply plugin: 'eclipse'
    sourceCompatibility = 1.5
    version = '1.0'
    jar {
    manifest {
    attributes 'Implementation-Title': 'Gradle Quickstart',
    'Implementation-Version': version
    }
    }
    repositories {
    mavenCentral()
    }
    dependencies {
    compile 'commons-collections:commons-collections:3.2'
    testCompile 'junit:junit:4.+'
    }
    uploadArchives {
    repositories.mavenDeployer {
    repository(url: 'file://localhost/tmp/repo')
    }
    }

    View Slide

  37. > tree -C /tmp/repo/
    /tmp/repo/
    └── quickstart
    └── quickstart
    ├── 1.0
    │ ├── quickstart-1.0.jar
    │ ├── quickstart-1.0.jar.md5
    │ ├── quickstart-1.0.jar.sha1
    │ ├── quickstart-1.0.pom
    │ ├── quickstart-1.0.pom.md5
    │ └── quickstart-1.0.pom.sha1
    ├── maven-metadata.xml
    ├── maven-metadata.xml.md5
    └── maven-metadata.xml.sha1
    3 directories, 9 files

    View Slide

  38. Standard Gradle Plugins
    • java, groovy, scala, antlr, cpp*, cpp-exe*, cpp-lib*
    • announce, application, ear, jetty, maven, osgi,
    war, maven2Gradle
    • checkstyle, codearc, eclipse, ecilpse-wtp,
    findbugs, idea, jdepend, pmd, project-report,
    signing, sonar

    View Slide

  39. Multi-Project?
    http://gradle.org/docs/current/userguide/
    multi_project_builds.html

    View Slide

  40. Demo
    • gradle-android-plugin
    • Writing my own plugin
    • git://github.com/Gasol/intro-to-gradle.git

    View Slide

  41. Links
    • http://gradle.org/docs/current/userguide/
    userguide.html
    • https://speakerdeck.com/u/bmuschko/p/
    next-generation-builds-with-gradle-1

    View Slide

  42. http://gradleware.com/registered/books/building-and-testing/

    View Slide

  43. Thanks :)

    View Slide

  44. Question?

    View Slide