我正在嘗試從如下函式訪問全域變數:
def var = "hello"
def func() {
println("func:" var)
}
tasks.register('callme') {
doLast {
print("callme:" var)
func()
}
}
gradle 報錯資訊:
> Task :callme FAILED
callme:hello
FAILURE: Build failed with an exception.
* Where:
Build file '/vagrant/work/gradle/build.gradle' line: 67
* What went wrong:
Execution failed for task ':callme'.
> Could not get unknown property 'var' for root project 'demo' of type org.gradle.api.Project.
從函式呼叫訪問全域變數的正確方法是什么?
uj5u.com熱心網友回復:
def 對函式不可見,您已將值傳遞給函式。使用分機。屬性,如果你需要它
ext.extVar = "ext-hello"
def strVar = "hello"
def func(String strVar) {
println("func - def:" strVar)
println("func - ext:" extVar)
}
println "from root "
func(strVar)
tasks.register('callme') {
doLast {
println("callme:" strVar)
func(strVar)
}
}
--
from root
func - def:hello
func - ext:ext-hello
> Task :callme
callme:hello
func - def:hello
func - ext:ext-hello
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/469223.html
標籤:毕业典礼
上一篇:Gradle下載舊的編譯依賴項
