2016-03-17 92 views
0

我有下面的代碼,這是失敗的gradle文件。任務聲明不在gradle中工作

task wakeup 
task dressup 
task playMusic 
task goRunning 

//dependsOn(task) 

wakeup <<{ 
    println("I am awake,i need to go for a run") 
} 

dressup(dependsOn: wakeup)<<{ 
    println("I am ready with my track suit") 
} 

playMusic(dependsOn: dressup)<<{ 
    println("I have played track 7") 
} 

goRunning(dependsOn: playMusic)<<{ 
    println("I am running") 
} 

錯誤: -

C:\Users\akathaku\Desktop\gradlelearning>gradle -q -b taskmethods.gradle goRunning 

FAILURE: Build failed with an exception. 

* Where: 
Build file 'C:\Users\akathaku\Desktop\gradlelearning\taskmethods.gradle' line: 14 

* What went wrong: 
A problem occurred evaluating root project 'gradlelearning'. 
> Could not find method dressup() for arguments [{dependsOn=task ':wakeup'}] on root project 'gradlelearning'. 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

但是,如果我的代碼更改爲: -

//dependsOn(task) 

task wakeup <<{ 
    println("I am awake,i need to go for a run") 
} 

task dressup(dependsOn: wakeup)<<{ 
    println("I am ready with my track suit") 
} 

task playMusic(dependsOn: dressup)<<{ 
    println("I have played track 7") 
} 

task goRunning(dependsOn: playMusic)<<{ 
    println("I am running") 
} 

其完美運行。 Normaly宣佈任務並稍後使用它。但用dependsOn方法會失敗。爲什麼?

回答

1

你的問題不是,顯然它在你的第二個例子中有效。

問題是,當你嘗試定義任務主體時,你錯過了task聲明,所以gradle將groovy代碼解釋爲方法調用。意思是,當你寫:

dressup(dependsOn: wakeup)<<{ 
    println("I am ready with my track suit") 
} 

搖籃不承認它作爲一個任務,而是看到了dressup(dependsOn: wakeup)部分,你試圖調用與參數{dependsOn: wakeup}命名dressup方法。但是不存在這樣的方法,並且你得到錯誤。

這就是爲什麼你總是需要告訴gradle這個,這是一個任務,即:

task dressup(dependsOn: wakeup)<<{ 
    println("I am ready with my track suit") 
} 

根據你的第二個(成功)的例子。