A simple Gradle wrapper for go commands, allowing to build and test
Golang programs and manage their dependencies either via Go modules, or via dep tool. The project
directory doesn't have to be in the standard $GOAPTH
repository, therefore
you can locate your project repository anywhere on filesystem.
For usage of Go modules you have to have installed at least go1.11.
The plugin supports 3 ways how to deal with external dependencies:
- Go modules
- go + dep + optional management of "proprietary vendors"
- go + manually/externally managed vendors
Plugin expects that go and dep (if used) commands are already installed on given system and that they are available on $PATH
.
In case of dep usage:
- Only Unix systems are supported. (Windows support can be added on demand.)
- The plugin can deal with "proprietary vendors" - package imports which are not in public repositories like GitHub, or Bitbucket, but are proprietary, e.g. repositories behind a company firewall etc. (see Limitations)
plugins {
id "cz.swsamuraj.godep" version "0.6.1"
}
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "gradle.plugin.cz.swsamuraj:gradle-godep-plugin:0.6.1"
}
}
apply plugin: "cz.swsamuraj.godep"
The plugin expects that you have a local installation of go1.11
and that
this command is available on $PATH
. Further, there should be:
- a
go.mod
file in your project directory, which contains at least themodule
statement.
No configuration is needed.
The plugin has following life-cycle.
test
build
The plugin requires that you have a local installation of go
and dep
tools and that
those commands are available on $PATH
. There shouldn't be any go.mod
file in the project.
A minimal necessary configuration:
godep {
importPath = 'github.com/sw-samuraj/hello'
}
The plugin uses following life-cycle. You can skip certain tasks via configuration switches.
prepareWorkspace
dep
can be optionally disabled (see Config options)proprietaryVendors
can be optionally disabled (see Config options)test
build
There must be a godep
part in the build.gradle
file which defines a mandatory parameter importPath
which emulates
directory structure inside standard $GOPATH
repository.
You can make certain tasks optional, or mandatory via boolean
switch (see example below).
godep {
// A mandatory value which defines the full import path for the package.
// Basically, it's the directory structure under $GOPATH/src.
importPath = 'github.com/sw-samuraj/hello'
// Skips the dep task in the build life-cycle.
depOptional = true
// Skips the proprietaryVendors task in the build life-cycle.
proprietaryVendorsOptional = true
// Map of import packages from non-public repositories.
// The item in the map has an import path as a key and a tag
// (or a branch) as a value.
proprietaryVendors = [
'my.private.repo/my-org/my-package': 'v0.1.0',
'my.private.repo/my-org/your-package': 'v0.3.2',
'my.private.repo/my-org/his-package': 'master'
]
}
The plugin expects that you have a local installation of go1.11
and that
this command is available on $PATH
. Further, there should be:
- a
go.mod
file in your project directory, which contains only themodule
statement - and a
vendor
directory containing all needed dependencies.
In this case, the dependencies are not managed neither by plugin, nor go itself.
A minimal necessary configuration:
godep {
disableGoModule = true
}
The plugin has following life-cycle.
test
build
Deletes the build
directory.
Deletes the vendor
directory. If you want to clean both, the build
and the vendor
directory every time, you can
define following task dependency:
clean.dependsOn cleanVendors
Creates a "fake" $GOPATH
directory structure inside the build/go
directory
with a symbolic link targeting the project directory. Every go
command is then
executed under this "real" $GOPATH
.
(This is just a technical detail explanation in case you are curious.) 😉
Calls dep init
command in case there is no Gopkg.toml
file presented.
Otherwise calls dep ensure
command.
Clones all the defined import packages from non-public repositories to the vendor/<importPath>
directory.
Currently, it supports only clonning via https.
Calls go test
command.
Calls go build
command. Compiled binary file is stored in the build/out
directory.
Applies for dep approach only.
Go tools currently don't support package imports which are not in public repositories,
such as GitHub, Bitbucket etc. Therefore if you have a proprietary
repository, e.g. behind a company firewall, you are on your own to solve all the dependency and build problems.
The gradle-godep-plugin
took a specific approach, how to handle this situation:
- Use
dep
tool for solving of public imports, which are stored in thevendor
directory. - Clone proprietary imports via Gradle
proprietaryVendors
task to thevendor
directory.
Unfortunately, configuration of the proprietary package imports has to be done on two places:
build.gradle
defines a map of package imports and their versions. These packages are clonned during execution of theproprietaryVendors
task.Gopkg.toml
defines the same package imports (without versions) asignored
, so they can be ignored during execution of thedep
task.- Eventually, if the proprietary package imports contain transitive dependencies on packages in public
repositories, those have to be defined in the
Gopkg.toml
file asrequired
.
Let's consider following example: we have a proprietary package my.company.repo/cool-project/shared-package
which has a transitive dependency on the github.com/coreos/etcd/clientv3
package.
build.gradle
:
godep {
// Map of import packages from non-public repositories.
// The item in the map has an import path as a key and a tag
// (or a branch) as a value.
proprietaryVendors = [
'my.company.repo/cool-project/shared-package': 'v0.1.0'
]
}
Gopkg.toml
:
# Needs to be filled only in case of transitive dependencies, otherwise can be omitted.
required = [
"github.com/coreos/etcd/clientv3"
]
# It's the same package as in godep.proprietaryVendors map, only without version.
ignored = [
"my.company.repo/cool-project/shared-package"
]
Currently, cloning of the proprietary vendors have these limitations:
- Cloning is done via https only (e.g.
git clone https://xxx
). - Cloned repository has to be anonymously accessible.
SSL, or https authentication could be added later, based on demand (please, fill an issue).
Usage of the plugin and example projects can be found in the examples
directory:
go-and-modules
shows configuration for plain Go modules support.go-and-dep
shows configuration for go + dep support (including "proprietary vendors").go-and-disabled-modules
shows configuration for Go build with manually managed vendors.
The gradle-godep-plugin is published under BSD 3-Clause license.