Most plugins offer configuration options for build scripts and other plugins to customize how the plugin works.
Plugins do this using
extension objects
.
A
Project
has an associated
ExtensionContainer
object that contains all the settings and properties for the plugins that have been applied to the project.
You can provide configuration for your plugin by adding an extension object to this container.
An extension object is simply an object with Java Bean properties representing the configuration.
Let’s add a
greeting
extension object to the project, which allows you to configure the greeting:
build.gradle.kts
interface GreetingPluginExtension {
val message: Property<String>
}
class GreetingPlugin : Plugin<Project> {
override fun apply(project: Project) {
// Add the 'greeting' extension object
val extension = project.extensions.create<GreetingPluginExtension>("greeting")
// Add a task that uses configuration from the extension object
project.task("hello") {
doLast {
println(extension.message.get())
}
}
}
}
apply<GreetingPlugin>()
// Configure the extension
the<GreetingPluginExtension>().message = "Hi from Gradle"
build.gradle
interface GreetingPluginExtension {
Property<String> getMessage()
}
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
// Add the 'greeting' extension object
def extension = project.extensions.create('greeting', GreetingPluginExtension)
// Add a task that uses configuration from the extension object
project.task('hello') {
doLast {
println extension.message.get()
}
}
}
}
apply plugin: GreetingPlugin
// Configure the extension
greeting.message = 'Hi from Gradle'
$ gradle -q hello
Hi from Gradle
In this example,
GreetingPluginExtension
is an object with a property called
message
.
The extension object is added to the project with the name
greeting
.
This object becomes available as a project property with the same name as the extension object.
the<GreetingPluginExtension>()
is equivalent to
project.extensions.getByType(GreetingPluginExtension::class.java)
.
Often, you have several related properties you need to specify on a single plugin.
Gradle adds a configuration block for each extension object, so you can group settings:
build.gradle.kts
interface GreetingPluginExtension {
val message: Property<String>
val greeter: Property<String>
}
class GreetingPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create<GreetingPluginExtension>("greeting")
project.task("hello") {
doLast {
println("${extension.message.get()} from ${extension.greeter.get()}")
}
}
}
}
apply<GreetingPlugin>()
// Configure the extension using a DSL block
configure<GreetingPluginExtension> {
message = "Hi"
greeter = "Gradle"
}
build.gradle
interface GreetingPluginExtension {
Property<String> getMessage()
Property<String> getGreeter()
}
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
def extension = project.extensions.create('greeting', GreetingPluginExtension)
project.task('hello') {
doLast {
println "${extension.message.get()} from ${extension.greeter.get()}"
}
}
}
}
apply plugin: GreetingPlugin
// Configure the extension using a DSL block
greeting {
message = 'Hi'
greeter = 'Gradle'
}
$ gradle -q hello
Hi from Gradle
In this example, several settings can be grouped within the
configure<GreetingPluginExtension>
block.
The
configure
function is used to configure an extension object.
It provides a convenient way to set properties or apply configurations to these objects.
The type used in the build script’s
configure
function (
GreetingPluginExtension
) must match the extension type.
Then, when the block is executed, the receiver of the block is the extension.
In this example, several settings can be grouped within the
greeting
closure. The name of the closure block in the build script (
greeting
) must match the extension object name.
Then, when the closure is executed, the fields on the extension object will be mapped to the variables within the closure based on the standard Groovy closure delegate feature.