Gradle上的全局变量声明

huangapple go评论77阅读模式
英文:

Global variable declaration on Gradle

问题

我有一个如下的Gradle任务。在开始构建之前设置GOPATH。当我运行第二个任务,即runUnitTest时,该块内部未设置GOPATH,并且我看到此错误"$GOPATH未设置"。

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"
}

当然,我可以在第二个任务中再次设置GOPATH。但是,我想知道如何在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.

答案1

得分: 1

你可以为所有类型为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%"
}
英文:

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%"
}

huangapple
  • 本文由 发表于 2017年2月8日 05:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/42100482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定