Till now configuration for the plugins was done through setting the properties in gradle.properties
file.
If we follow the guidelines of Gradle, we should extend the Gradle DSL when our plugin will be applied with our own extension. We need to be able to configure 2 parameters in the plugin. For this we will create extension object that holds two properties. One property will hold files directory and second one the sorting type.
When applying the plugin, sortFiles
extension will be added to the container of project extensions.
SortFilesTaskExtension is a POJO class holding configuration properties.
SortFilesTaskExtension sortFilesTaskExtension = project.getExtensions().create("sortFiles", SortFilesTaskExtension.class);
Later you can reference this extension instance to fetch configuration values for the plugin:
sortFilesTaskExtension.getSortType().isPresent()?sortFilesTaskExtension.getSortType().get():DEFAULT_SORTING_TYPE
Simple extension block for sorting files:
sortFiles {
sortType = "date"
directoryLocation = "files"
}
You are still able to read properties in build.gradle
script and pass them through the sortFiles
extension, while the
plugin won't read any properties directly:
sortFiles {
sortType = getProject().getExtensions().getExtraProperties().get('tasks.files.sortType')
directoryLocation = getProject().getExtensions().getExtraProperties().get('tasks.files.folder')
}
I have added binary-plugin-extension-test-project project where this binary plugin with extension definition is being applied and parameters configured through the extension.