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

问题描述

我是 Gradle 和 Groovy 的新手,并试图了解在定义 gradle 任务时在 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" 
   }
}

通过阅读Gradle In Action"一书,我知道 task hello {} 实际上是对 groovy task() 方法的调用>项目界面.在第 77 页上,它显示了 Project 接口

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.

我不明白的是 groovy 如何根据 有一个 groovy 编译器插件可以将 task hello { } 转换为 task('hello', { })

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', { })

我的问题:

  • 在哪里可以找到有关执行转换的 Gradle Groovy Compiler 插件的信息?

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

因为 gradle 以某种方式扩展了 Groovy 编程语言,所以声称 Gradle 脚本是 groovy 程序在技术上是不正确的吗?

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

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

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 使用 AST Transformations 扩展 Groovy 语法.您提到的任务定义语法只是 Gradle 应用的转换之一.您可以找到该转换的实现 这里.回答您的具体问题:

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:

  • Gradle 应用的各个转换在我所知道的任何地方都没有特别记录.但是,您可以查看上述链接的同一包中的其他类.

  • 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 脚本支持 Groovy 语法的超集.任何有效的 Groovy 在 Gradle 脚本中也有效,但是,Gradle 脚本不一定(通常也不是)有效的默认"Groovy.

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.

没有办法获得等效的 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 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 14:39