2016-09-02 28 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