英文:
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%"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论