2014-04-25 45 views
4

我有簡單的build.gradle(或任務有println任何build.gradle搖籃始終沒有從任何任務調用println

println GradleVersion.current().prettyPrint() 

task task1{ 
    println 'task1 starting' 
} 

現在,當我運行$ gradle build我總是看到任務執行或打印輸出

task1 starting 
:compileJava UP-TO-DATE 
:processResources UP-TO-DATE 
:classes UP-TO-DATE 
:jar 
:assemble 
:compileTestJava UP-TO-DATE 
:processTestResources UP-TO-DATE 
:testClasses UP-TO-DATE 
:test UP-TO-DATE 
:check UP-TO-DATE 
:build 

BUILD SUCCESSFUL 

Total time: 1.291 secs 

爲什麼總是會輸出println裏面的任務?

+0

重複:[搖籃的println打印時它不叫](HTTP:/ /stackoverflow.com/questions/17051140/gradle-println-prints-when-it-is-not-called) –

+1

在我看來,這兩個問題都在問「爲什麼println總是打印?「。這些問題的目標是不同的,但是基本問題是關於相同的Gradle行爲。我不認爲任何問題都應該作爲重複來封閉,但我認爲隨後的讀者會從評論中的交叉鏈接中受益。 –

回答

14

如果您有以下一段代碼:

task task1 { 
    println 'task1 starting' 
} 

你在配置階段的任務。此階段在腳本評估期間運行。如果您想在執行任務時打印某些內容您需要爲任務添加動作

它看起來像:

task task1 << { 
    println 'task1 action' 
} 

這段代碼將同時任務運行時進行評估。 <<與在任務對象上調用doLast方法完全相同。您可以添加許多操作。

5

從章55.構建生命週期http://www.gradle.org/docs/current/userguide/build_lifecycle.html

// in `settings.gradle` 
// println 'This is executed during the initialization phase.' 

println 'This is executed during the configuration phase.' 

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

task execute << { 
    println 'This is executed during the execution phase.' 
} 

運行與gradle help

輸出:

This is executed during the initialization phase. 
This is executed during the configuration phase. 
This is also executed during the configuration phase. 
:help 

Welcome to Gradle 1.10. 

To run a build, run gradle <task> ... 

To see a list of available tasks, run gradle tasks 

To see a list of command-line options, run gradle --help 

BUILD SUCCESSFUL 

Total time: 1.882 secs