如何向脚本实例添加属性?

How to add a property to the instance of Script?

根据 Gradle documentation 13.3:

When Gradle executes a script, it compiles the script into a class which implements Script.

我们可以通过将它们的声明放入 ext 块来向 Project 对象添加额外的属性。例如:

ext {
    springVersion = "3.1.0.RELEASE" //added to the Project object
    emailNotification = "build@master.org" //added to the Project object
}

是否可以通过 ext

Script 对象添加属性

要将属性添加到脚本,您需要使用 def 指令。

def mySrciptProp = "hello world"

请注意,所有实施 ExtensionAware can use the extra properties extension (ExtraPropertiesExtension) 的 类。

Script 对象未实现此功能,因此无法向其添加额外的属性。