【发布时间】:2016-07-28 15:44:45
【问题描述】:
我一直在努力解决user guide 的问题。这似乎是我想做的一件非常简单的事情,但我就是不知道如何让它发挥作用。
我想编写一个外部 groovy 文件来定义一个任务,然后在 build.gradle 中调用该任务。这是我的build.gradle 文件:
// Apply the groovy plugin to add support for Groovy
apply plugin: 'groovy'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// We use the latest groovy 2.x version for building this library
compile 'org.codehaus.groovy:groovy-all:2.4.7'
compile gradleApi()
compile localGroovy()
// We use the awesome Spock testing and specification framework
testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
testCompile 'junit:junit:4.12'
}
task myTask (type: my.package.MyTask)
然后,在src/main/groovy/my/package 下我有MyTask.groovy:
package my.package
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class MyTask extends DefaultTask {
String greeting = 'hello from MyTask'
@TaskAction
def greet(){
println greeting
}
}
我什至无法列出任务:
$ gradle tasks
FAILURE: Build failed with an exception.
* Where:
Build file '/projects/gradle/build.gradle' line: 21
* What went wrong:
A problem occurred evaluating root project 'gradle'.
> Could not get unknown property 'my' for root project 'gradle' of type org.gradle.api.Project.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 3.193 secs
在这一点上,我完全不知道发生了什么或如何做我想做的事。这似乎是最简单的示例,但遵循用户指南让我无处可去。无论我做什么,我都无法摆脱那个例外。
如何编写一个 groovy 文件并使用它在构建文件中定义一个新任务?
【问题讨论】: