Gradle任务示例上的Groovy语法 [英] Groovy syntax on Gradle task example

查看:78
本文介绍了Gradle任务示例上的Groovy语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Groovy非常热情,但是最近才开始使用它,并且也希望从语言语法POV中了解Gradle.

I'm very enthusiastic about Groovy, but have just started using it very recently and would like to understand Gradle from language syntax POV as well.

我的示例是关于Gradle任务的:

My example is about Gradle task:

task hello(type: GreetingTask)

class GreetingTask extends DefaultTask {
    @TaskAction
    def greet() {
        println 'hello from GreetingTask'
    }
}

task方法接受一个字符串名称和一个闭包. hello(type: GreetingTask)怎么突然变成字符串?如果type是命名参数,那么GreetingTask是什么意思?是GreetingTask.class的快捷方式吗? hello函数/关闭如何返回字符串?

task method accepts a String name and a Closure. How can hello(type: GreetingTask) be a String out of a sudden? If type is a named parameter, what does GreetingTask mean? Is it a shortcut for GreetingTask.class? How come hello function/closure returns a String?

推荐答案

在创建任务时此处,您会看到用于创建任务的方法,并且在后台将调用它们来创建任务.当然,在后台将这些调用委托给 ,但由于

As Tim pointed out gradle scripts are written in a specific DSL which is not pure groovy. When it comes to creating tasks here you can see the methods that are used to create a task and under the hood they will be invoked to create the task. Of course under the hood this calls are delegated to TaskContainer, but there's not need to call project.tasks.task because of scopes and mentioned DSL. Now, how it happens that the code you provided creates an instance of task? With this transformer. I know that this answer if far from being sufficient, however hope it helps a bit ;)

type: GreetingTaskMap的一个实例(它等效于[type: reetingTask]),是的,在常规情况下,当引用一个类时,可以忽略.class.

type: GreetingTask is an instance of Map (it's equivalent of [type: reetingTask]) and, yes, in groovy .class can be omitted when a class is referred.

这篇关于Gradle任务示例上的Groovy语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆