api / org.gradle.api.plugins / Convention

Convention

interface Convention : ExtensionContainer

A Convention manages a set of convention objects. When you add a convention object to a Convention, and the properties and methods of the convention object become available as properties and methods of the object which the convention is associated to. A convention object is simply a POJO or POGO. Usually, a Convention is used by plugins to extend a org.gradle.api.Project or a org.gradle.api.Task.

Functions

findPlugin

abstract fun <T : Any> findPlugin(type: Class<T>): T

Locates the plugin convention object with the given type.

getExtensionsAsDynamicObject

abstract fun getExtensionsAsDynamicObject(): DynamicObject

Returns a dynamic object which represents the properties and methods contributed by the extensions and convention objects contained in this convention.

getPlugin

abstract fun <T : Any> getPlugin(type: Class<T>): T

Locates the plugin convention object with the given type.

getPlugins

abstract fun getPlugins(): MutableMap<String, Any>

Returns the plugin convention objects contained in this convention.

Inherited Functions

add

abstract fun <T : Any> add(publicType: Class<T>, name: String, extension: T): Unit
abstract fun <T : Any> add(publicType: TypeOf<T>, name: String, extension: T): Unit

Adds a new extension to this container. Adding an extension of name 'foo' will:

  • add 'foo' dynamic property
  • add 'foo' dynamic method that accepts a closure that is a configuration script block
The extension will be exposed as publicType.

abstract fun add(name: String, extension: Any): Unit

Adds a new extension to this container. Adding an extension of name 'foo' will:

  • add 'foo' dynamic property
  • add 'foo' dynamic method that accepts a closure that is a configuration script block
The extension will be exposed as extension.getClass() unless the extension itself declares a preferred public type via the org.gradle.api.reflect.HasPublicType protocol.

configure

abstract fun <T : Any> configure(type: Class<T>, action: Action<in T>): Unit
abstract fun <T : Any> configure(type: TypeOf<T>, action: Action<in T>): Unit

Looks for the extension of the specified type and configures it with the supplied action.

abstract fun <T : Any> configure(name: String, action: Action<in T>): Unit

Looks for the extension with the specified name and configures it with the supplied action.

create

abstract fun <T : Any> create(publicType: Class<T>, name: String, instanceType: Class<out T>, vararg constructionArguments: Any): T
abstract fun <T : Any> create(publicType: TypeOf<T>, name: String, instanceType: Class<out T>, vararg constructionArguments: Any): T

Creates and adds a new extension to this container. A new instance of the given instanceType will be created using the given constructionArguments. The extension will be exposed as publicType. The new instance will have been dynamically made ExtensionAware, which means that you can cast it to ExtensionAware.

abstract fun <T : Any> create(name: String, type: Class<T>, vararg constructionArguments: Any): T

Creates and adds a new extension to this container. A new instance of the given type will be created using the given constructionArguments. The extension will be exposed as type unless the extension itself declares a preferred public type via the org.gradle.api.reflect.HasPublicType protocol. The new instance will have been dynamically made ExtensionAware, which means that you can cast it to ExtensionAware.

findByName

abstract fun findByName(name: String): Any

Looks for the extension of a given name. If none found null is returned.

findByType

abstract fun <T : Any> findByType(type: Class<T>): T
abstract fun <T : Any> findByType(type: TypeOf<T>): T

Looks for the extension of a given type (useful to avoid casting). If none found null is returned.

getByName

abstract fun getByName(name: String): Any

Looks for the extension of a given name. If none found it will throw an exception.

getByType

abstract fun <T : Any> getByType(type: Class<T>): T
abstract fun <T : Any> getByType(type: TypeOf<T>): T

Looks for the extension of a given type (useful to avoid casting). If none found it will throw an exception.

getExtensionsSchema

abstract fun getExtensionsSchema(): ExtensionsSchema

Provides access to the schema of all known extensions.

getExtraProperties

abstract fun getExtraProperties(): ExtraPropertiesExtension

The extra properties extension in this extension container. This extension is always present in the container, with the name “ext”.

getSchema

abstract fun getSchema(): MutableMap<String, TypeOf<*>>

Provides access to all known extensions types.

Extension Functions

findPlugin

fun <T : Any> Convention.findPlugin(): T?
fun <T : Any> Convention.findPlugin(conventionType: KClass<T>): T?

getPlugin

fun <T : Any> Convention.getPlugin(): T
fun <T : Any> Convention.getPlugin(conventionType: KClass<T>): T

getPluginByName

fun <T : Any> Convention.getPluginByName(name: String): T

Looks for the convention plugin of a given name and casts it to the expected type T.