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

Building Go projects made easy with Gradle

Building Go projects made easy with Gradle

For many Go projects, Make and shell scripts are the predominant tools of choice for automating the process of building source code as well as assembling and distributing binaries. While powerful tools, they do not provide any higher level abstractions like conventions and generalized concepts to minimize the burden on maintainability and readability. There must be a better way to avoid copy-pasting the same automation code from project to project!

In this demo-driven talk, we will discuss how the build tool Gradle can help with bootstrapping and streamlining the automation of Go projects. In the process, we’ll identify some typical challenges with the help of a sample project and how to overcome them with GoGradle, a community plugin for Gradle.

Benjamin Muschko

January 26, 2018
Tweet

More Decks by Benjamin Muschko

Other Decks in Programming

Transcript

  1. Building Go projects made easy with Gradle
    Benjamin Muschko

    View full-size slide

  2. About me
    @bmuschko
    h*ps:/
    /github.com/bmuschko
    h*p:/
    /bmuschko.com

    View full-size slide

  3. my WAY tO GO

    View full-size slide

  4. learn by coding
    https://github.com/bmuschko/link-verifier

    View full-size slide

  5. Typical tasks
    Package management
    Cross-compila;on of binaries
    Test execu;on & repor;ng
    Go environment setup

    View full-size slide

  6. What’s out there?

    View full-size slide

  7. The community is using…
    #!/bin/sh
    Make Shell Scrip;ng Build Tool
    What do you use?

    View full-size slide

  8. The old world

    View full-size slide

  9. Throwing the red flag
    Copy-pas;ng of code snippets
    Unstructured spagheE code
    Tes;ng through manual execu;on
    Only understood by build guru™

    View full-size slide

  10. Why Gradle?
    Performant
    Flexible
    Polyglot
    Extensible
    Conven?ons
    Declara?ve
    Deep API
    https://gradle.org/

    View full-size slide

  11. GoGradle FTW!
    https://github.com/gogradle/gogradle

    View full-size slide

  12. distribution management

    View full-size slide

  13. executing gradle without installation
    Gradle Wrapper

    View full-size slide

  14. generated wrapper files
    .
    !"" build.gradle
    !"" settings.gradle
    !"" gradle
    # $"" wrapper
    # !"" gradle-wrapper.jar
    # $"" gradle-wrapper.properties
    !"" gradlew
    $"" gradlew.bat
    supposed to be checked into SCM

    View full-size slide

  15. gradle wrapper
    ›_

    View full-size slide

  16. Applying the gogradle plugin
    build.gradle
    plugins {

    id 'com.github.blindpirate.gogradle' version '0.8.1'

    }


    golang {

    packagePath = 'github.com/bmuschko/link-verifier'

    }
    custom plugin DSL

    View full-size slide

  17. managing the Go version
    build.gradle
    golang {

    goVersion = '1.9.2'

    }
    downloaded and installed automatically

    View full-size slide

  18. package management

    View full-size slide

  19. Use revision for Reproducibility
    go get https://github.com/mvdan/xurls
    git clone https://github.com/mvdan/xurls
    may include vendored packages

    View full-size slide

  20. go package managers
    godep, govendor, glide…

    View full-size slide

  21. Use revision for Reproducibility
    glide.yaml
    package: .

    import:

    - package: github.com/mvdan/xurls

    version: 1.1.0

    testImport:

    - package: github.com/stretchr/testify

    version: 1.1.4

    concrete version or version ranges

    View full-size slide

  22. Use revision for Reproducibility
    glide.lock
    hash: 806deb3bb1bb02051f152c49856cac37224f623247...

    updated: 2018-01-08T20:54:38.326434-07:00

    imports:

    - name: github.com/mvdan/xurls

    version: d315b61cf6727664f310fa87b3197e9faf2a8513

    testImports:

    - name: github.com/davecgh/go-spew

    version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9

    subpackages:

    - spew

    - name: github.com/pmezard/go-difflib

    version: d8ed2627bdf02c080bf22230dbb337003b7aba2d

    subpackages:

    - difflib

    - ...

    View full-size slide

  23. gradle init
    ›_

    View full-size slide

  24. Migrate package declarations
    dependencies {

    golang {

    build(['name':'github.com/mvdan/xurls',
    'version':'d315b61cf6727664f310fa87...',
    'transitive':false])

    test(['name':'github.com/davecgh/go-spew',
    'version':'6d212800a42e8ab5c146b8ace...',
    'subpackages':['.', 'spew']
    'transitive':false])
    ...

    }

    }
    build.gradle
    configuration

    View full-size slide

  25. gradle dependencies
    ›_

    View full-size slide

  26. Dependency report in console
    $ gradle dependencies
    > Task :dependencies
    build:
    github.com/bmuschko/link-verifier
    \-- github.com/mvdan/xurls:d315b61
    test:
    github.com/bmuschko/link-verifier
    |-- github.com/davecgh/go-spew:6d21280 [spew]
    |-- github.com/pmezard/go-difflib:d8ed262 [difflib]
    \-- github.com/stretchr/testify:69483b4 [assert]

    View full-size slide

  27. gradle vendor
    ›_

    View full-size slide

  28. Vendored packages in source tree
    $ tree vendor
    vendor
    $"" github.com
    !"" davecgh
    # $"" go-spew
    # $"" spew
    !"" mvdan
    # $"" xurls
    !"" pmezard
    # $"" go-difflib
    # $"" difflib
    $"" stretchr
    $"" testify
    !"" assert
    $"" doc.go

    View full-size slide

  29. gradle lock
    ›_

    View full-size slide

  30. Vendored packages in source tree
    ---

    apiVersion: "0.8.1"

    dependencies:

    build:

    - urls:

    - "https://github.com/mvdan/xurls.git"

    - "[email protected]:mvdan/xurls.git"

    vcs: "git"

    name: "github.com/mvdan/xurls"

    commit: "d315b61cf6727664f310fa87b3197e9faf2a8513"

    transitive: false

    test:

    - urls:

    - "https://github.com/davecgh/go-spew.git"

    - "[email protected]:davecgh/go-spew.git"

    ...
    gogradle.lock

    View full-size slide

  31. performing verifications
    so-called lifecycle or aggregation task
    gradle check
    fmt
    coverage
    vet
    test
    depends on

    View full-size slide

  32. test execution & reporting

    View full-size slide

  33. executing tests
    $ gradle test
    > Task :test
    Test for github.com/bmuschko/link-verifier/http finished,
    3 completed, 0 failed
    Test for github.com/bmuschko/link-verifier/file finished,
    6 completed, 0 failed
    Test for github.com/bmuschko/link-verifier/stat finished,
    4 completed, 0 failed
    Test for github.com/bmuschko/link-verifier/text finished,
    8 completed, 0 failed
    filtering: gradle test --tests file*.go

    View full-size slide

  34. test Html report
    .gogradle/reports/tests/index.html

    View full-size slide

  35. gradle test
    ›_

    View full-size slide

  36. code coverage

    View full-size slide

  37. coverage Html report
    .gogradle/reports/coverage/index.html

    View full-size slide

  38. coverage line details

    View full-size slide

  39. gradle coverage
    ›_

    View full-size slide

  40. gradle fmt & vet
    ›_

    View full-size slide

  41. Linting the source code

    View full-size slide

  42. calling golint from task
    task lint(type: com.github.blindpirate.gogradle.Go) {

    run 'golint github.com/bmuschko/link-verifier/...'

    }


    check.dependsOn lint
    build.gradle
    hooks into check task

    View full-size slide

  43. cross-compiling binaries

    View full-size slide

  44. declaring target platforms
    build {

    targetPlatform = ['darwin-amd64', 'netbsd-amd64',
    'netbsd-386', 'openbsd-amd64',
    'openbsd-386', 'freebsd-amd64',
    'freebsd-386', 'linux-amd64',
    'linux-386', 'linux-arm',
    'windows-amd64', 'windows-386']

    }
    build.gradle
    -

    View full-size slide

  45. Generated binaries
    $ tree .gogradle
    .gogradle
    !"" link-verifier-darwin-amd64
    !"" link-verifier-freebsd-386
    !"" link-verifier-freebsd-amd64
    !"" link-verifier-linux-386
    !"" link-verifier-linux-amd64
    !"" link-verifier-linux-arm
    !"" link-verifier-netbsd-386
    !"" link-verifier-netbsd-amd64
    !"" link-verifier-openbsd-386
    !"" link-verifier-openbsd-amd64
    !"" link-verifier-windows-386
    $"" link-verifier-windows-amd64

    View full-size slide

  46. gradle build
    ›_

    View full-size slide

  47. Resources
    Gradle webpage
    hHps:/
    /gradle.org/
    GoGradle plugin
    hHps:/
    /github.com/gogradle/gogradle
    Link Verifier example source code
    hHps:/
    /github.com/bmuschko/link-verifier

    View full-size slide

  48. We want your feedback!

    View full-size slide