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. Throwing the red flag Copy-pas;ng of code snippets Unstructured spagheE

    code Tes;ng through manual execu;on Only understood by build guru™
  2. generated wrapper files . !"" build.gradle !"" settings.gradle !"" gradle

    # $"" wrapper # !"" gradle-wrapper.jar # $"" gradle-wrapper.properties !"" gradlew $"" gradlew.bat supposed to be checked into SCM
  3. 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
  4. 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
  5. 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
 - ...
  6. 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
  7. 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]
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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>
  13. 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
  14. Resources Gradle webpage hHps:/ /gradle.org/ GoGradle plugin hHps:/ /github.com/gogradle/gogradle Link

    Verifier example source code hHps:/ /github.com/bmuschko/link-verifier