gpt4 book ai didi

gradle - Gradle 任务内联配置如何在幕后工作?

转载 作者:行者123 更新时间:2023-12-03 05:55:02 25 4
gpt4 key购买 nike

我试图了解如何在后台解释 gradle 构建脚本。

我知道整个构建脚本都委托(delegate)给项目对象,所以当我们输入

task taskName

我们正在执行项目对象中定义的函数,任务为名称并采用字符串参数。

对我来说奇怪的是任务的内联配置:
taskName.description = "Description of the task"
taskName.group = "Group of the task"

这在项目对象级别如何真正起作用?

最佳答案

看看https://docs.gradle.org/current/dsl/org.gradle.api.Project.html ,第一个“属性”部分。
以下是删除了一些细节的有用位:

A project has 5 property 'scopes', which it searches for properties. The scopes are:

  • The Project object itself.
  • The extra properties of the project.
  • The extensions added to the project by the plugins.
  • The tasks of the project.

The extra properties and convention properties inherited from the project's parent, recursively up to the root project. The properties of this scope are read-only.

When reading a property, the project searches the above scopes in order, and returns the value from the first scope it finds the property in. If not found, an exception is thrown.


在您的示例中, taskName.description = "..."表示 Gradle 查找 taskName作为 Project 上的一种方法,作为一个额外的属性,作为一个扩展,然后它会找到一个具有该名称的任务。那么您只需调用 setDescription定期 Task目的。
奇迹发生在 Project.property() 使用 ExtensibleDynamicObject像这样配置:
https://github.com/gradle/gradle/blob/0b9cb4429513297e03965b0578607d10a2a1fcdf/subprojects/core/src/main/java/org/gradle/api/internal/project/DefaultProject.java#L226-L230

关于gradle - Gradle 任务内联配置如何在幕后工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45401007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com