控制Gradle println執行時機

控制Gradle println執行時機

問題現象

初次編寫Gradle task打印日誌,無論對應task是否執行,該task中println總是執行,跟正常的Java代碼認知不一致。

task hello {
    println "do sth before execute"
     // execute sth
    println "do sth after execute"
}

無論該任務是否執行,都會進行打印,爲什麼呢?

解決辦法

話不多說,直接上官網例子,有問題一定要去官網獲取第一手資料!!!
https://docs.gradle.org/current/userguide/build_lifecycle.html#header

settings.gradle

println 'This is executed during the initialization phase.'

build.gradle

println 'This is executed during the configuration phase.'

task configured {
    println 'This is also executed during the configuration phase.'
}

task test {
    doLast {
        println 'This is executed during the execution phase.'
    }
}

task testBoth {
	doFirst {
	  println 'This is executed first during the execution phase.'
	}
	doLast {
	  println 'This is executed last during the execution phase.'
	}
	println 'This is executed during the configuration phase as well.'
}

Output of gradle test testBoth

> gradle test testBoth
This is executed during the initialization phase.

> Configure project :
This is executed during the configuration phase.
This is also executed during the configuration phase.
This is executed during the configuration phase as well.

> Task :test
This is executed during the execution phase.

> Task :testBoth
This is executed first during the execution phase.
This is executed last during the execution phase.

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

參考

https://stackoverflow.com/questions/23288470/gradle-always-does-println-from-any-task
Gradle任務詳述-w3school

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章