目录

Lifecycle

Tasks

Dependencies

Multi-project 构建

Plugins

Properties

Extra Properties

Dynamic Methods

属性、方法、Script blocks列举

Properties

Methods

Script blocks


所有新的Gradle API都声明一个Action参数类型而不是Closure,这使得很容易选择委托类型。除了旧的Closure之外,即使是较旧的API也有Action变体。

Gradle has a convention whereby if a method has the same name as a collection-based property, then the method appends its values to that collection.Gradle有一个约定,即如果方法与基于集合的属性具有相同的名称,则该方法会将其值附加到该集合。

下表显示了每种Gradle脚本的委托:

Type of script Delegates to instance of
Build script Project
Init script Gradle
Settings script Settings

委托对象的属性和方法可供您在脚本中使用。其次,每个Gradle脚本都实现了Script接口。此接口定义了许多可在脚本中使用的属性和方法。

一个build script由零个或多个语句和script blocks组成。语句可以包括方法调用,属性赋值和局部变量定义。script blocks是一种方法调用,它将闭包作为参数。闭包被视为一个配置闭包,它在执行时配置一些委托对象。顶级script blocks如下所示:

下面列出了Gradle脚本中使用的一些核心类型:

Type Description
Project 此接口是您用于从构建文件与Gradle交互的主要API。从项目中,您可以以编程方式访问Gradle的所有功能。
Task ask表示构建的单个原子工作,例如编译classes或生成javadoc。
Gradle 表示Gradle的调用。
Settings 声明实例化和配置要参与构建的Project实例的层次结构所需的配置。
IncludedBuild 复合中包含的构建。
Script 此接口由所有Gradle脚本实现,以添加一些特定于Gradle的方法。由于编译后的脚本类将实现此接口,因此可以直接在脚本中使用此接口声明的方法和属性。
JavaToolChain 一组用于构建Java源的工具。
SourceSet SourceSet表示Java源代码和资源文件的逻辑组。用户手册中有更详细的介绍。
SourceSetOutput 所有输出目录(已编译的类,已处理的资源等)的集合 - 请注意SourceSetOutput扩展了FileCollection。
SourceDirectorySet SourceDirectorySet表示由一组源目录组成的一组源文件,以及相关的包含和排除模式。
IncrementalTaskInputs 提供对增量任务需要处理的任何输入文件的访问。
Configuration 配置表示一组工件及其依赖项。查找有关声明配置的依赖关系或管理ConfigurationContainer文档中的配置的更多信息
ResolutionStrategy 定义依赖解析的策略。例如,强制某些依赖项版本,替换,冲突解决方案或快照超时。
ArtifactResolutionQuery

A builder to construct a query that can resolve selected software artifacts of the specified components.

ComponentSelection

Represents a tuple of the component selector of a module and a candidate version to be evaluated in a component selection rule.

ComponentSelectionRules

Represents a container for component selection rules. Rules can be applied as part of the resolutionStrategy of a configuration and individual components can be explicitly accepted or rejected by rule. Components that are neither accepted or rejected will be subject to the default version matching strategies.

ExtensionAware 可以在运行时使用其他对象扩展的对象
ExtraPropertiesExtension Gradle域对象的附加,ad-hoc属性。
PluginDependenciesSpec 用于声明在脚本中使用的插件的DSL。
PluginDependencySpec 插件依赖的可变规范。
PluginManagementSpec 配置插件的解析方式。
ResourceHandler 提供对特定于资源的实用程序方法的访问,例如创建各种资源的工厂方法。
TextResourceFactory 创建由字符串,文件和存档条目等源支持的TextResources。

Lifecycle

Project和build.gradle文件之间存在一对一的关系。在构建初始化期间,Gradle为每个参与构建的项目组装一个Project对象,如下所示:

为构建创建一个Settings实例。

执行settings.gradle脚本,如果存在,则针对Settings对象进行配置。

使用配置好的Settings对象创建Project实例的层次结构。

最后,通过对项目执行build.gradle文件(如果存在)来丰满每个Project。项目按广度顺序进行丰满,以便在子项目之前父项目就丰满完了。可以通过调用Project.evaluationDependsOnChildren()或使用Project.evaluationDependsOn(java.lang.String)添加显式的依赖项来覆盖此顺序。

Tasks

项目本质上是Task对象的集合。每个任务执行一些基本工作,例如编译类,运行单元测试或压缩WAR文件。您可以使用TaskContainer上的某个create()方法向项目添加任务,例如TaskContainer.create(java.lang.String)。您可以使用TaskContainer上的查找方法之一找到现有任务,例如TaskCollection.getByName(java.lang.String)。

Dependencies

项目通常具有许多依赖性,以便完成其工作。此外,项目通常会生成许多artifacts,以供其他项目可以使用。这些依赖项按配置分组,可以从repositories中检索和上载。您使用Project.getConfigurations()方法返回的ConfigurationContainer来管理这些配置(依赖项要靠它分组)。Project.getDependencies()方法返回的DependencyHandler来管理依赖项。Project.getArtifacts()方法返回ArtifactHandler来管理artifacts。Project.getRepositories()方法返回的RepositoryHandler来管理repositories。

Multi-project 构建

项目按层次排列,项目具有名称和完全限定的路径,该路径在层次结构中唯一标识它。

Plugins

插件可用于模块化和重用项目的配置。可以使用PluginAware.apply(java.util.Map)方法或使用PluginDependenciesSpec插件脚本块应用插件。

Properties

Gradle针对Project实例执行项目的构建文件以配置项目。脚本使用的任何属性或方法都会委托给关联的Project对象。这意味着,您可以直接在脚本中使用Project接口上的任何方法和属性。

defaultTasks('some-task')  // Delegates to Project.defaultTasks()
reportsDir = file('reports') // Delegates to Project.file() and the Java Plugin

您还可以使用project属性访问Project实例。这可以使脚本在某些情况下更清晰。例如,您可以使用project.name而不是name来访问项目的名称。

一个项目有5个属性“范围”。您可以在构建文件中按名称访问这些属性,也可以通过调用项目的Project.property(java.lang.String)方法来访问这些属性。范围是:

Project对象本身。此范围包括Project实现类声明的任何属性getter和setter。例如,Project.getRootProject()可作为rootProject属性访问。此范围的属性是可读写的(可读写具体取决于是否存在相应的getter或setter方法)。

项目的extra 属性。每个项目都维护一个extra属性的map,它可以包含任意 名称->值对。定义后,此作用域的属性是可读写的。

插件添加到项目中的extensions。每个扩展都可以作为只读属性使用,其名称与扩展的属性名相同。

插件添加到项目中的convention 属性。插件可以通过项目的Convention对象向项目添加属性和方法。此范围的属性可以是可读的或可写的,具体取决于Convention的实现类。

项目的任务。可以使用任务名称作为属性名称来访问任务。此范围的属性是只读的。例如,一个名为compile的任务可以作为编译属性访问。

从项目的父级继承,递归到根项目的extra 属性和convention 属性。此范围的属性是只读的。

在读取属性时,项目按顺序搜索上述范围,并从找到该属性的第一个范围返回值。如果未找到,则抛出异常。在编写属性时,项目按顺序搜索上述范围,并在找到该属性的第一个范围内设置该属性。如果未找到,则抛出异常。

//project`s method of property
Object property(String propertyName)
返回给定属性的值。此方法按如下方式定位属性:If this project object has a property with the given name, return the value of the property.
If this project has an extension with the given name, return the extension.
If this project's convention object has a property with the given name, return the value of the property.
If this project has an extra property with the given name, return the value of the property.
If this project has a task with the given name, return the task.
Search up through this project's ancestor projects for a convention property or extra property with the given name.
If not found, a MissingPropertyException is thrown.//project`s method of setProperty
void setProperty(String name, Object value)
设置此项目的属性。此方法在以下位置搜索具有给定名称的属性,并在找到该属性的第一个位置设置该属性。The project object itself. For example, the rootDir project property.
The project's Convention object. For example, the srcRootName java plugin property.
The project's extra properties.
If the property is not found, a MissingPropertyException is thrown.

Extra Properties

必须通过“ext”命名空间定义所有额外属性。一旦定义了额外的属性,它就可以直接在拥有对象上获得(在下面的例子中分别是Project,Task和子项目),并且可以读取和更新。只需要通过命名空间完成的初始声明。

project.ext.prop1 = "foo"
task doStuff {ext.prop2 = "bar"
}
subprojects { ext.${prop3} = false }

读取额外属性是通过“ext”或通过拥有对象完成的。

ext.isSnapshot = version.endsWith("-SNAPSHOT")
if (isSnapshot) {// do snapshot stuff
}

Dynamic Methods

一个项目有5个方法'范围'可供搜索方法:

Project对象本身。

构建文件。该项目搜索构建文件中声明的匹配方法。

插件添加到项目中的extensions 。每个extensions都可用作方法,而且该方法将闭包或Action作为参数。

插件添加到项目中的convention 方法。插件可以通过项目的Convention对象向项目添加属性和方法。

项目的任务。为每个任务添加一个方法,使用任务名称作为方法名称并使用单个闭包或Action做参数。该方法(任务名称作为方法名的方法)使用提供的闭包为相关的任务调用Task.configure(groovy.lang.Closure)方法。例如,如果项目有一个名为compile的任务,则会添加一个带有以下签名的方法:void compile(Closure configureClosure)。

父项目的方法,递归到根项目。

项目的闭包类型的属性。闭包被视为一种方法,并使用提供的参数进行调用。

属性、方法、Script blocks列举

Properties

Property Description
allprojects

The set containing this project and its subprojects.

ant

The AntBuilder for this project. You can use this in your build file to execute ant tasks. See example below.

artifacts

Returns a handler for assigning artifacts produced by the project to configurations.

buildDir

The build directory of this project. The build directory is the directory which all artifacts are generated into. The default value for the build directory is projectDir/build

...

......

Properties added by the java plugin:

Property Description
archivesBaseName

The base name to use for archive files.

distsDir

deprecated

The directory to generate TAR and ZIP archives into.

distsDirName

The name for the distributions directory. This in interpreted relative to the project' build directory.

docsDir

Returns a file pointing to the root directory supposed to be used for all docs.

...

......

Methods

Method Description
absoluteProjectPath(path)

Converts a name to an absolute project path, resolving names relative to this project.

afterEvaluate(closure)

Adds a closure to be called immediately after this project has been evaluated. The project is passed to the closure as a parameter. Such a listener gets notified when the build file belonging to this project has been executed. A parent project may for example add such a listener to its child project. Such a listener can further configure those child projects based on the state of the child projects after their build files have been run.

...

......

Script blocks

其实是接收action类型或闭包类型单个参数的方法。

Block Description
allprojects

Configures this project and each of its sub-projects.

ant

Executes the given closure against the AntBuilder for this project. You can use this in your build file to execute ant tasks. The AntBuild is passed to the closure as the closure's delegate. See example in javadoc for Project.getAnt()

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.

subprojects

Configures the sub-projects of this project.

Script blocks added by the java plugin:

Block Description
reporting

Configures the ReportingExtension added by the java plugin.

sourceSets

Configures the source sets of this project.

gradle(Project)相关推荐

  1. 【转载】解决refreshing gradle project 和Building gradle project info 一直卡住\速度慢

    转载: http://blog.csdn.net/xx326664162/article/details/52002616 文章出自:薛瑄的博客 分析原因: 更改Gradle的版本后,或者更新AS后, ...

  2. AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案

    AndroidStudio导入项目一直卡在Building gradle project info最快速解决方案 参考文章: (1)AndroidStudio导入项目一直卡在Building grad ...

  3. AndroidStudio Refreshing Gradle Project编译更新Gradle卡住问题

    描述 重新安装AndroidStudio或从Eclipse换到AS的时候,在编译新的项目的时候,一般会遇到如下图那样. 一般是因为在AndroidStudio每次更新版本都会更新Gradle这个插件( ...

  4. Unlinked Gradle project

    导入新项目到AS时报错: Unlinked Gradle project... 导致项目不能编译.重复导了几次也一样. 解决方法: 点击Import Project,定位到项目所在的目录,找到其中的b ...

  5. Gradle project xxx refresh failed Error:Unable to tunnel through proxy. Proxy returns HTTP/...

    SVN地址更换重新导入项目到AS后,点击编译报错: Gradle project xxx refresh failed Error:Unable to tunnel through proxy. Pr ...

  6. android studio 导入项目卡死,AndroidStudio导入新项目一直卡在Building gradle project info...

    对于新手配置Gradle是一件很痛苦的事,记住二句话绝对搞定 1.在Gradle-->gradle-wrapper.properties中配置distributionUrl=https\://s ...

  7. building xxx gradle project info的解决办法

    AndroidStudio创建项目,最后一步finish后,一直长时间处于building"project name"gradle project info,界面就一直停留在如图所 ...

  8. Please review your Gradle project setup in the android/ folde

     重新配置flutter后死活运行了不 了  :代码以下 * Where: Build file 'C:\Users\Administrator\AndroidStudioProjects\flutt ...

  9. android studio创建项目一直,Android Studio在创建/导入项目的时候,一直处于building “XXX”gradle project info的解决办法...

    Android Studio在新建项目或者导入项目的时候,可能会一直处于building "XXX"gradle project info的状态,而且还取消不了,无奈之下只能干掉进 ...

  10. 解决AndroidStudio更新后在 Building gradle project info 一直卡住

    Android Studio导入项目的时候,一直卡在Building gradle project info这一步,主要原因还是因为被墙的结果.gradle官网虽然可以访问,但是速度连蜗牛都赶不上.. ...

最新文章

  1. Istio Pilot源码浅析
  2. Mysql-学习笔记(==》常用函数 八)
  3. 台大李宏毅教授的神经网络教程
  4. AWS EC2 Root密码重置
  5. MappedByteBuffer以及ByteBufer的底层原理
  6. 【转】VTK修炼之道1_初识VTK
  7. 除了吃月饼,中秋节还能干啥?
  8. 哔哩哔哩公司swot分析_可口可乐公司2019年SWOT分析
  9. hdu 6348 序列计数
  10. java 正则表达式提取字符串
  11. 停车还能360全方位影像_新司机轻松,老司机上瘾,中大型SUV都少不了360°全景影像...
  12. 英雄联盟(LOL)3d模型显示
  13. LSD直线检测和霍夫线变换的学习建议
  14. oracle增加分区时报ora14036,增加分区表2009年的分区报ORA-14074错误
  15. 实现简单的三D立方体自动旋转
  16. gedit 显示行号
  17. 介绍-FreeMarker
  18. JetBrains IDE全新UI预览版来了,要做简洁与强大兼顾的IDE
  19. guava ratelimiter限流(1)
  20. 【推荐】数据湖技术及实践与案例资料汇总合集47篇

热门文章

  1. 智邦国际销售管理系统对企业有哪些作用?
  2. 45-Jenkins-Sidebar Link插件实现添加侧边栏
  3. 猿创征文|Google Earth Engine(GEE)实现土地利用数据栅格转矢量
  4. 小技能:监控器查看视频录像回放
  5. 形式系统(Formale System)-SAT问题
  6. mysql 配置定时任务_Mysql定时任务
  7. Ajax+MySQL+PHP+jQuery实现异步请求页面并显示数据
  8. mysql jpa List_jpa查询数据库返回list
  9. 汝之蜜糖,吾之砒霜— 聊聊软件开发中的最佳实践
  10. DTU连接自建MQTT服务器