一、简介
本参考指南描述了组成Gradle构建语言的各种类型(types)。
Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建开源工具。它的构建脚本(script)使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置。Groovy的语法支持DSL的结构特性。
二、一些基础概念
在深入学习之前,你需要首先理解一些基本概念,这会对你以后编写Gradle的构建脚本有帮助。
首先,Gradle的脚本是配置脚本。当Gradle脚本执行的时候,它配置了一个特点类型的对象(it configures an object of a particular type)。例如:当一个构建脚本(a build script)执行的时候,它配置了Project这个类型的一个实例对象,这个对象叫做脚本的委托对象。
注:type就是java概念中的一个类,一个脚本执行的时候配置了特定类的一个实例对象Object。
下表列出了每种Gradle脚本类型的委托对象:
| 脚本类型 | 委托的实例对象 |
| Build script | Project |
| Init script | Gradle |
| Settings script | Settings |
委托对象的属性和方法可供您在脚本中使用。
其次,Gradle脚本实现了Script接口,这个接口定义了许多可以在脚本中使用的属性和方法。
三、构建脚本的结构
一个构建脚本由零个或多个语句和脚本块组成。语句可以包括方法调用、属性分配和局部变量定义。脚本块是以闭包为参数的方法调用。这个闭包被视为一个配置闭包,它在执行时配置一些委托对象。下面列出了顶级脚本块:
| 脚本块 | 描述 |
| allprojects { } | Configures this project and each of its sub-projects. 配置此项目及其每个子项目。 |
| artifacts { } | Configures the published artifacts for this project. 配置此项目的已发布项目。 |
| buildscript { } | Configures the build script classpath for this project. 配置此项目的构建脚本的类路径。 |
| configurations { } | Configures the dependency configurations for this project. 配置此项目的依赖项配置。 |
| dependencies { } | Configures the dependencies for this project. 配置此项目的依赖项。 |
| repositories { } |
Configures the repositories for this project. |
| sourceSets { } |
Configures the source sets of this project. |
| subprojects { } |
Configures the sub-projects of this project. |
| publishing { } |
Configures the PublishingExtension added by the publishing plugin. |
一个构建脚本(build script)也是一个groovy脚本,因此可以包含groovy脚本中允许的元素,例如方法定义和类定义。
四、核心类型(Core types)
下面列出了Gradle脚本中使用的一些中心类型:
| 类型(Type) | 描述 |
| Project | This interface is the main API you use to interact with Gradle from your build file. From a Project, you have programmatic access to all of Gradle's features. 这个接口是通过构建文件与Gradle交互的主要API。从一个Project类型的对象,您可以通过编程访问Gradle的所有功能。 |
| Task | A Task represents a single atomic piece of work for a build, such as compiling classes or generating javadoc. 任务表示一个构建的单个原子工作,例如编译类或生成javadoc。 |
| Gradle | Represents an invocation of Gradle. 表示对Gradle的调用。 |
| Settings | Declares the configuration required to instantiate and configure the hierarchy of Project instances which are to participate in a build. 声明实例化所需的配置,以及配置一次构建中Project实例的层次结构。 |
| IncludedBuild | A build that is included in the composite. 引用的其它build文件。 |
| Script | This interface is implemented by all Gradle scripts to add in some Gradle-specific methods. As your compiled script class will implement this interface, you can use the methods and properties declared by this interface directly in your script. |
| JavaToolChain | |
| SourceSet | |
| SourceSetOutput | |
| SourceDirectorySet | |
| IncrementalTaskInputs | |
| Configuration | |
| ResolutionStrategy | |
| ArtifactResolutionQuery | |
| ComponentSelection | |
| ComponentSelectionRules | |
| ExtensionAware | |
| ExtraPropertiesExtension | |
| PluginDependenciesSpec | |
| PluginDependencySpec | |
| PluginManagementSpec | |
| ResourceHandler | |
| TextResourceFactory | |
| InputChanges |
这篇指南介绍了Gradle构建语言的基础,它是一个基于Groovy DSL的项目自动化构建工具,替代了XML配置。内容涵盖Gradle脚本作为配置脚本的角色,脚本的基本概念如委托对象、Script接口,以及构建脚本的结构和核心类型。
992

被折叠的 条评论
为什么被折叠?



