简单的常规程序 - Gradle

时间:2016-09-02 14:27:40

标签: gradle groovy

我创建了一个非常简单的gradle文件,其中包含一个名为groovy的任务。

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

有什么想法吗?

由于

西奥。

1 个答案:

答案 0 :(得分:1)

如果我正确理解您的文件未命名为build.gradle如果是这种情况,那么gradle不会知道任务groovy是什么,因为它没有读那个文件。

如果要通过gradle指定build.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'