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.

8f2248c6bfcc6df39a2cd8edf4267cb5?s=128

Benjamin Muschko

January 26, 2018
Tweet

Transcript

  1. Building Go projects made easy with Gradle Benjamin Muschko

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

  3. my WAY tO GO

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

  5. Typical tasks Package management Cross-compila;on of binaries Test execu;on &

    repor;ng Go environment setup
  6. What’s out there?

  7. The community is using… #!/bin/sh Make Shell Scrip;ng Build Tool

    What do you use?
  8. The old world

  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™
  10. Why Gradle? Performant Flexible Polyglot Extensible Conven?ons Declara?ve Deep API

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

  12. distribution management

  13. executing gradle without installation Gradle Wrapper

  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
  15. gradle wrapper ›_

  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
  17. managing the Go version build.gradle golang {
 goVersion = '1.9.2'


    } downloaded and installed automatically
  18. package management

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

    may include vendored packages
  20. go package managers godep, govendor, glide…

  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
  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
 - ...
  23. gradle init ›_

  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
  25. gradle dependencies ›_

  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]
  27. gradle vendor ›_

  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
  29. gradle lock ›_

  30. Vendored packages in source tree ---
 apiVersion: "0.8.1"
 dependencies:
 build:


    - urls:
 - "https://github.com/mvdan/xurls.git"
 - "git@github.com:mvdan/xurls.git"
 vcs: "git"
 name: "github.com/mvdan/xurls"
 commit: "d315b61cf6727664f310fa87b3197e9faf2a8513"
 transitive: false
 test:
 - urls:
 - "https://github.com/davecgh/go-spew.git"
 - "git@github.com:davecgh/go-spew.git"
 ... gogradle.lock
  31. performing verifications so-called lifecycle or aggregation task gradle check fmt

    coverage vet test depends on
  32. test execution & reporting

  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
  34. test Html report .gogradle/reports/tests/index.html

  35. gradle test ›_

  36. code coverage

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

  38. coverage line details

  39. gradle coverage ›_

  40. gradle fmt & vet ›_

  41. Linting the source code

  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
  43. cross-compiling binaries

  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 <project-name>-<plaHorm>
  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
  46. gradle build ›_

  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
  48. We want your feedback!

  49. w Thank you