简体   繁体   English

Groovy 中 Gradle 构建脚本的依赖语法

[英]Dependency syntax of Gradle build script in Groovy

For example the Groovy code like this:例如像这样的 Groovy 代码:

dependencies {
    classpath 'com.android.tools.build:gradle:0.13.2'
}

I know classpath is a function and you invoke it by passing a string我知道classpath是一个函数,你通过传递一个字符串来调用它
But my questions are:但我的问题是:

  1. Is dependencies is a method of object project ? dependencies项是对象project的一种方法吗?
  2. Is classpath a method of project or dependencies ? classpathproject方法还是dependencies项?
  3. What does it mean when you pass a closure to a function?将闭包传递给函数是什么意思?

Try looking at the API docs, or navigating through the source.尝试查看 API 文档,或浏览源代码。 It helps to get your head around it.它有助于你了解它。 That said, there are all sorts of other things going on with delegates/owners and such, so it's not always clear on inspection where things are being called if you're not used to this aspect of Groovy.也就是说,委托/所有者等还有各种各样的其他事情,因此如果您不习惯 Groovy 的这一方面,检查时并不总是很清楚在哪里调用了这些事情。 The descriptions and examples in the docs are likely going to be more useful for general usage.文档中的描述和示例可能对一般用途更有用。

dependencies is a method on Project, and is of type DependencyHandler. dependencies是 Project 上的一个方法,属于 DependencyHandler 类型。 (Many if not most things you use frequently will live on Project) https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#dependencies-groovy.lang.Closure- https://docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html (如果不是大多数你经常使用的东西都将存在于 Project 中) https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#dependencies-groovy.lang.Closure- https:// docs.gradle.org/current/javadoc/org/gradle/api/artifacts/dsl/DependencyHandler.html

In the dependencies block the add method of DependencyHandler gets called, with a 'configuration' and the dependency string.在依赖项块中,调用 DependencyHandler 的add方法,带有“配置”和依赖项字符串。 classpath here is a configuration used for the buildscript itself这里的类路径是用于构建脚本本身的配置

What does it mean when you pass a closure to a function?将闭包传递给函数是什么意思?

It means just that.就是这个意思。 Closure is a class in Groovy, and when you write this in Gradle you are creating a new closure and passing it as a parameter to a function (see the docs above, they reference Closure as a param in many methods). Closure 是 Groovy 中的一个类,当您在 Gradle 中编写它时,您正在创建一个新的闭包并将其作为参数传递给函数(参见上面的文档,它们在许多方法中将Closure作为参数引用)。 The Groovy docs can explain how they work better than I can. Groovy 文档可以解释它们如何比我更好地工作。 You can sort of think of it as a lambda, but it's not the same.你可以把它想象成一个 lambda,但它不一样。 It can execute code on its owner (usually the enclosing block/closure it's defined in) and can sometimes execute against its assigned delegate.它可以在其所有者(通常是定义它的封闭块/闭包)上执行代码,并且有时可以针对其分配的委托执行代码。 If you're trying to work out how the Gradle configs work internally, usually you can find most of what you need on the class as above (eg mostly we're calling methods on DependencyHandler in this case).如果你想弄清楚 Gradle 配置是如何在内部工作的,通常你可以在上面的类中找到你需要的大部分内容(例如,在这种情况下,大多数情况下我们正在调用 DependencyHandler 上的方法)。 However the written documentation covers most common use cases.但是,书面文档涵盖了最常见的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM