Kumaran Senapathy
Kumaran Senapathy

Reputation: 1283

Global variable declaration on Gradle

I have a gradle task as follows. Setting the GOPATH before I start the build. When I run the second task, that is runUnitTest and GOPATH is not set inside that block and I see this error "$GOPATH not set".

task goBuild(type:Exec) {
  environment 'GOPATH', projectDir.toString().split("/src")[0]
  commandLine "go", "build", "main.go"
}

task runUnitTest(type:Exec) {
  dependsOn goBuild
  commandLine "go", "get", "github.com/AlekSi/gocov-xml"
  commandLine "go", "test", "-v"

}

I can of course, set the GOPATH again inside the second task. But, I am curious on how to have globally set in gradle.

Upvotes: 0

Views: 336

Answers (1)

MartinTeeVarga
MartinTeeVarga

Reputation: 10898

You can set the environmental property for all tasks of type Exec:

tasks.withType(Exec) {
    environment 'GOPATH', 'hello'
}

task first(type:Exec) {
    commandLine 'CMD', '/C', 'echo', "%GOPATH%"
}

task second(type:Exec) {
    commandLine 'CMD', '/C', 'echo', "%GOPATH%"
}

Upvotes: 1

Related Questions