gradle(Project)

目录

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 scriptDelegates to instance of
Build scriptProject
Init scriptGradle
Settings scriptSettings

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

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

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

TypeDescription
Project此接口是您用于从构建文件与Gradle交互的主要API。从项目中,您可以以编程方式访问Gradle的所有功能。
Taskask表示构建的单个原子工作,例如编译classes或生成javadoc。
Gradle表示Gradle的调用。
Settings声明实例化和配置要参与构建的Project实例的层次结构所需的配置。
IncludedBuild复合中包含的构建。
Script此接口由所有Gradle脚本实现,以添加一些特定于Gradle的方法。由于编译后的脚本类将实现此接口,因此可以直接在脚本中使用此接口声明的方法和属性。
JavaToolChain一组用于构建Java源的工具。
SourceSetSourceSet表示Java源代码和资源文件的逻辑组。用户手册中有更详细的介绍。
SourceSetOutput所有输出目录(已编译的类,已处理的资源等)的集合 - 请注意SourceSetOutput扩展了FileCollection。
SourceDirectorySetSourceDirectorySet表示由一组源目录组成的一组源文件,以及相关的包含和排除模式。
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可以在运行时使用其他对象扩展的对象
ExtraPropertiesExtensionGradle域对象的附加,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

PropertyDescription
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:

PropertyDescription
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

MethodDescription
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类型或闭包类型单个参数的方法。

BlockDescription
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:

BlockDescription
reporting

Configures the ReportingExtension added by the java plugin.

sourceSets

Configures the source sets of this project.

 

 

失败的原因以及如何解决这个问题? gradle project sync失败通常是由于以下几个原因之一引起的: 1. Gradle版本不兼容:可能是项目中使用的Gradle版本与Android Studio不兼容。解决方法是在项目的build.gradle文件中将Gradle版本更改为与Android Studio兼容的版本。 2. 网络连接问题:下载Gradle文件时可能由于网络连接问题导致同步失败。解决方法是检查网络连接,并确保可以正常访问Gradle服务器。 3. 依赖项缺失:Gradle同步失败可能是由于项目的依赖项无法被解析。解决方法是在build.gradle文件中添加正确的依赖项,或者更新依赖项的版本。 4. 缓存问题:Gradle缓存可能损坏或失效,导致同步失败。解决方法是删除Gradle缓存,并重新进行同步。 以下是解决gradle project sync失败的步骤: 1. 确认Gradle版本兼容性,并在项目的build.gradle文件中将Gradle版本更改为与Android Studio兼容的版本。 2. 检查网络连接,并确保可以正常访问Gradle服务器。 3. 如果依赖项无法解析,请检查项目的依赖项,并确保它们正确添加到build.gradle文件中。 4. 如果Gradle缓存存在问题,可以尝试删除Gradle缓存。可以在以下位置找到Gradle缓存: - 在Windows上:C:\Users\<YourUsername>\.gradle\caches - 在Mac上:/Users/<YourUsername>/.gradle/caches - 在Linux上:/home/<YourUsername>/.gradle/caches 删除Gradle缓存后,重新进行同步。 请根据具体的情况,逐步尝试上述解决方法,以解决gradle project sync失败的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值