Gradle任务语法:如何从Groovy角度解释它? [英] Gradle task syntax: how is it explained from a Groovy perspective?

查看:91
本文介绍了Gradle任务语法:如何从Groovy角度解释它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解Gradle的Groovy DSL是如何工作的。

I'm having a hard time understanding how Gradle's Groovy DSL works.

不幸的是,Gradle是我今天遇到的Groovy的主要用例,日常工作,而我注意到,对于许多开发人员而言,他们对Groovy的了解完全是通过Gradle进行的。因此,大多数Gradle用户对Groovy的了解都非常有限。

Unfortunately Gradle is the main use-case for Groovy that I come across in my day to day work, and I've noticed that for many devs, their exposure to Groovy is strictly through Gradle. And that a majority of Gradle users have a very limited grasp of Groovy as a consequence.

在我对Groovy的有限理解中,以下sintax tokenA tokenB {tokenC} 其中所有令牌都不是语言关键字, tokenA 将是我们使用参数 tokenB调用的方法,最后一个参数是闭包。我想想我是正确的,但是我知道我错了,因为在令牌B之后可能需要用逗号才能使该分析正确。

In my limited understanding of Groovy, the following sintax tokenA tokenB { tokenC } where all tokens are not language keywords, tokenA would be a method that we are calling with arguments tokenB and the final argument is a closure. I would like to think I'm correct, but I know I'm wrong because there probably needs to be a comma after tokenB for that analysis to be correct.

I就像您已经知道的那样,绝不是Groovy开发人员,我认为在不学习Groovy基础知识的情况下使用Gradle是一件不好的事,因为它限制了我充分利用其功能。但是,我唯一可行的选择是通过示例学习而不不幸地学习理论。

I am by no means, as you can already tell, a Groovy dev, and I think using Gradle without learning the basics of Groovy is a bad thing to do, because it limits me from fully exploiting its capabilities. But my only viable option is to learn though examples without learning the theory unfortunately.

我确实检查了类似的问题,例如这一个,但对于我来说足够清晰或完整的答案都没有。

I did check out some similar questions like this one but no answers where clear or complete enough for me.

TL; DR


  1. 令牌如何执行 task myTask {doLast {}} 用Groovy解释吗?

  2. Gradle是否使用标准的Groovy解释器?

  3. myTask如何在存在 task 而不是 def 或它后面的类型时解释为标识符吗?

  4. 如果稍后在文件中添加了 myTask {dependOn myOtherTask} 怎么解释?

  1. How are the tokens task myTask { doLast {} } interpreted in Groovy?
  2. Does Gradle use a standard Groovy interpreter?
  3. How is myTask interpreted as an identifier when there is task and not def or a type behind it?
  4. If later in the file I added myTask { dependsOn myOtherTask } how does that get interpreted?


推荐答案

我相信它的所有功能都是通用的,没有什么特别的。这是您需要了解的常规概念。

I believe its all groovy and nothing special to gradle. Here's the groovy concepts you need to know.


  1. 如果方法的最后一个参数是闭包,则可以将闭包放在方法参数的右括号之后。



class MyClass {
   void doStuff(String name, Closure c) {
      c.call()
   } 
} 

def o = new MyClass() 
o.doStuff('x') {
   println "hello" 
} 




  1. 您可以实施您的对象上缺少方法 。如果有人尝试调用一个不存在的方法,您可以做

  1. You can implement method missing on your object. If someone tries to call a method that doesn't exist you can do stuff



class MyClass {
    def methodMissing(String name, args) {
        println "You invoked ${name}(${args})" 
    }
} 
def o = new MyClass() {
   o.thisMethodDoesNotExist('foo')
}




  1. 您可以将委托设置设置为关闭

  1. You can set the delegate on a closure



class MyBean {
   void include(String pattern) {...} 
   void exclude(String pattern) {...} 
} 
class MyClass {
   private MyBean myBean = new MyBean() 
   void doStuff(Closure c) {
      c.setDelegate(myBean)
      c.call()
   } 
} 

def o = new MyClass() 
o.doStuff {
   include 'foo' 
   exclude 'bar' 
} 

这3个时髦特性在很大程度上解释了gradle脚本中发生的魔术行为,使得Java开发人员抓狂了。

These 3 groovy features pretty much explain the "magic" behaviour going on in a gradle script that have java developers scratching their heads.

task myTask(type:Foo) { 
   doLast {...} 
}

让我们添加一些括号并添加隐式项目引用。让我们还将闭包提取到变量中

Let's add some brackets and also add the implicit project references. Let's also extract the closure into a variable

Closure c = { 
   doLast {...} 
}
project.task(project.myTask([type: Foo.class], c)) 

project.myTask(...)方法不存在,该行为最终通过 methodMissing 功能。 Gradle会将关闭对象上的委托设置为任务实例。因此,闭包中的任何方法都将委派给新创建的任务。

The project.myTask(...) method doesn't exist and the behavior is ultimately implemented via methodMissing functionality. Gradle will set the delegate on the closure to the task instance. So any methods in the closure will delegate to the newly created task.

最终,这在逻辑上是

Action<? extends Task> action = { task ->
   task.doLast {...} 
}
project.tasks.create('myTask', Foo.class, action)

请参见 TaskContainer.create(String,Class,Action)

这篇关于Gradle任务语法:如何从Groovy角度解释它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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