2016-09-02 46 views
-2

我創建了一個非常簡單的gradle文件,其中包含一個名爲groovy的任務。簡單的groovy程序 - Gradle

task groovy << {} 

println "Hello Groovy!" 

我嘗試在cmd中我的項目的文件夾下運行它(我使用的是Windows 8.1)作爲

gradle groovy 

,但我得到這個錯誤:

FAILURE: Build failed with an exception. 

What went wrong: 
Task 'groovy' not found in root project 'MyExercise'. 

Try: 
Run gradle tasks to get a list of available tasks. Run with --stacktrace  
option 
to get the stack trace. Run with --info or --debug option to get more log  
output 

它爲什麼不能找到時髦的任務?它在我的build.gradle文件中聲明。 Gradle已安裝。這是我的版本。

Gradle 3.0 
------------------------------------------------------------ 

Build time: 2016-08-15 13:15:01 UTC 
Revision:  ad76ba00f59ecb287bd3c037bd25fc3df13ca558 

Groovy:  2.4.7 
Ant:   Apache Ant(TM) version 1.9.6 compiled on June 29 2015 
JVM:   1.8.0_45 (Oracle Corporation 25.45-b02) 
OS:   Windows 8.1 6.3 amd64 

和命令gradle這個任務說明:

All tasks runnable from root project 
------------------------------------------------------------ 

Build Setup tasks 
----------------- 
init - Initializes a new Gradle build. [incubating] 
wrapper - Generates Gradle wrapper files. [incubating] 

Help tasks 
---------- 
buildEnvironment - Displays all buildscript dependencies declared 
t 'MyExercise'. 
components - Displays the components produced by root project 'MyE 
ubating] 
dependencies - Displays all dependencies declared in root project 
dependencyInsight - Displays the insight into a specific dependenc 
ect 'MyExercise'. 
help - Displays a help message. 
model - Displays the configuration model of root project 'MyExerci 
ng] 
projects - Displays the sub-projects of root project 'MyExercise'. 
properties - Displays the properties of root project 'MyExercise'. 
tasks - Displays the tasks runnable from root project 'MyExercise' 

To see all tasks and more detail, run gradle tasks --all 

To see more detail about a task, run gradle help --task <task> 

BUILD SUCCESSFUL 

任何想法?

謝謝

Theo。

+0

又是什麼'gradle這個tasks'告訴你? –

+0

我把gradle任務的結果放在我的問題 – Theo

+0

我想我應該看到像其他任務groovy類似的東西。但我不:( – Theo

回答

1

如果我正確理解你的文件沒有命名爲build.gradle如果是這種情況,那麼gradle將不知道什麼groovy是因爲它沒有讀取該文件。

如果你想指定一個非build.gradle的文件用於gradle評估,那麼你需要給gradle文件名。爲了清晰起見,我使用了一個名爲test.groovy的文件,並演示瞭如何在groovy文件中執行任務。

~/temp/39294812 $ cat test.groovy 
task groovy << { 
    println "Hello to you!" 
} 

println "Hello groovy" 

~/temp/39294812 $ gradle -q -b test.groovy groovy 
Hello groovy 
Hello to you! 

另外請注意,如果你只需要你的任務添加到另一個build.gradle你可以使用apply from: <path>

// to call groovy from inside a `build.gradle` file 
apply from: 'test.groovy' 
+1

是的,我做到了,實際上我忘了把代碼放到gradle文件中。 – Theo