ExtensionAware

API 文档ExtensionAware

可以在运行时通过其他对象进行扩展的对象。

// Extensions are just plain objects, there is no interface/type
class MyExtension {
  String foo

  MyExtension(String foo) {
    this.foo = foo
  }
}

// Add new extensions via the extension container
project.extensions.create('custom', MyExtension, "bar")
//                       («name»,   «type»,       «constructor args», …)

// extensions appear as properties on the target object by the given name
assert project.custom instanceof MyExtension
assert project.custom.foo == "bar"

// also via a namespace method
project.custom {
  assert foo == "bar"
  foo = "other"
}
assert project.custom.foo == "other"

// Extensions added with the extension container's create method are themselves extensible
assert project.custom instanceof ExtensionAware
project.custom.extensions.create("nested", MyExtension, "baz")
assert project.custom.nested.foo == "baz"

// All extension aware objects have a special "ext" extension of type ExtraPropertiesExtension
assert project.hasProperty("myProperty") == false
project.ext.myProperty = "myValue"

// Properties added to the "ext" extension are promoted to the owning object
assert project.myProperty == "myValue"

许多 Gradle 对象都支持扩展。这包括 projects、tasks、configurations、dependencies 等等。

有关添加和创建扩展的更多信息,请参阅 ExtensionContainer

有关额外属性的更多信息,请参阅 ExtraPropertiesExtension

一个 ExtensionAware 对象有几个 Gradle 搜索属性的“作用域”。这些作用域是

  • 对象本身。此作用域包括实现类声明的所有属性 getter 和 setter。此作用域的属性是否可读写取决于是否存在相应的 getter 或 setter 方法。
  • 对象类实现的 Groovy 元编程方法,例如 propertyMissing()。插件作者必须注意确保 propertyMissing() 的实现方式能够在其找不到属性时抛出 MissingPropertyException(String, Class) 异常。如果 propertyMissing() 总是为任何属性返回值,Gradle 将不会搜索下面的其他作用域。
  • 对象的额外属性。每个对象维护一个额外属性的 map,其中可以包含任意的名称 -> 值对。一旦定义,此作用域的属性即可读写。
  • 插件添加到对象的扩展。每个扩展都可作为一个与扩展同名的只读属性使用。

属性

属性描述
extensions

扩展的容器。

方法

无方法

脚本块

无脚本块

属性详情

ExtensionContainer extensions (只读)

扩展的容器。