了解gradle任务定义中的groovy语法 [英] Understanding the groovy syntax in a gradle task definition

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

问题描述

我是Gradle和Groovy的新手,并试图了解Groovy在定义gradle任务时发生了什么。

 任务hello {
println配置任务hello
doLast {
printlnhello有
}
}

从阅读Gradle In Action我知道任务hello {} 实际上是对groovy 任务()方法的调用c $ c> Project 界面。在第77页上,它显示了项目界面上有4个称为任务的方法

 <$ c 
task(args:Map< String,?>,name:String,c:Closure)
任务(名称,地址< String,?> ;, :String)
task(name:String,c:Closure)

据我所知, {} 是封闭体。



我不明白的是,groovy如何在 task中解释 hello hello { } 根据 https://stackoverflow.com/a/25592665/438319 有一个groovy编译器插件将任务hello {} 转换为任务('hello',{})



我的问题:


  • 哪里可以找到关于Gradle Groovy编译器插件的信息转换?


  • 由于Gradle以某种方式扩展了Groovy编程语言,是否声称Gradle脚本在技术上是不正确的?


  • 有没有办法让 gradle 命令输出编译器插件运行后生成的基本groovy代码?



解决方案

Gradle使用 AST Transformations 来扩展Groovy语法。您提到的任务定义语法只是Gradle应用的转换之一。您可以找到该转换的实现这里。回答您的具体问题:


  • Gradle应用的单个转换没有在我知道的任何地方专门记录。然而,你可以在上面的链接的相同包中查看其他类。

  • Gradle脚本支持超级Groovy语法。任何有效的Groovy在Gradle脚本中也是有效的,但是,Gradle脚本不一定(并且通常不是)有效的默认Groovy。

  • 这是一种获取等效Groovy代码输出的方式,因为它是在内存中操作的实际抽象语法树。

I am new to Gradle and Groovy and trying to understand what is happening at the level of groovy when a gradle task is defined.

task hello  { 
   println "configuring task hello" 
   doLast {
     println "hello there" 
   }
}

From reading the "Gradle In Action" book I know that the task hello {} is a really a call to the task() method of the groovy Project interface. On page 77 it shows that there are 4 methods called task on the Project interface

task(args: Map<String,?>, name:String)
task(args: Map<String,?>, name:String, c:Closure)
task(name: String)
task(name: String, c:Closure)

I understand that the {} is the closure body.

What I don't understand is how does groovy interpret hello in task hello { } according to https://stackoverflow.com/a/25592665/438319 there is a groovy compiler plugin that converts task hello { } into task('hello', { })

My Questions:

  • Where can I find information about the Gradle Groovy Compiler plugin that does the conversion?

  • Is the claim that Gradle scripts are groovy programs technically incorrect since gradle somehow extends the Groovy programming language?

  • Is there a way to get the gradle command to print out the base groovy code that is generated after the compiler plugin has run?

解决方案

Gradle uses AST Transformations to extend the Groovy syntax. The task definition syntax you mention is just one of the transformations Gradle applies. You can find the implementation for that transform here. To answer your specific questions:

  • The individual transforms that Gradle applies are not specifically documented anywhere that I'm aware of. You could however look at the other classes in the same package of the link above.

  • Gradle scripts support a super-set of Groovy syntax. Any valid Groovy is also valid in a Gradle script, however, a Gradle script is not necessarily (and typically not) valid "default" Groovy.

  • There isn't a way to get an output of the equivalent Groovy code since it's the actual abstract syntax tree that is being manipulated in-memory.

这篇关于了解gradle任务定义中的groovy语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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