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 Slide

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

    View Slide

  3. my WAY tO GO

    View Slide

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

    View Slide

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

    View Slide

  6. What’s out there?

    View Slide

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

    View Slide

  8. The old world

    View 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 Slide

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

    View Slide

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

    View Slide

  12. distribution management

    View Slide

  13. executing gradle without installation
    Gradle Wrapper

    View 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 Slide

  15. gradle wrapper
    ›_

    View 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 Slide

  17. managing the Go version
    build.gradle
    golang {

    goVersion = '1.9.2'

    }
    downloaded and installed automatically

    View Slide

  18. package management

    View 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 Slide

  20. go package managers
    godep, govendor, glide…

    View 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 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 Slide

  23. gradle init
    ›_

    View 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 Slide

  25. gradle dependencies
    ›_

    View 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 Slide

  27. gradle vendor
    ›_

    View 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 Slide

  29. gradle lock
    ›_

    View 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 Slide

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

    View Slide

  32. test execution & reporting

    View 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 Slide

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

    View Slide

  35. gradle test
    ›_

    View Slide

  36. code coverage

    View Slide

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

    View Slide

  38. coverage line details

    View Slide

  39. gradle coverage
    ›_

    View Slide

  40. gradle fmt & vet
    ›_

    View Slide

  41. Linting the source code

    View 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 Slide

  43. cross-compiling binaries

    View 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 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 Slide

  46. gradle build
    ›_

    View 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 Slide

  48. We want your feedback!

    View Slide

  49. w
    Thank you

    View Slide