How to configure a DSL style gradle plugin from plugin extension properties

I have implemented a Gradle plugin using the Gradle DSL style. The plugin is adding multiple aspects such as a adding a custom task, and configuring more other tasks. Overall, the plugin is generating some metadata property file in a source folder that must be configured by a plugin extension.

apply plugin: 'artifactMetadata'
    // configure the path for the metadata
    artifactMetadata {
        destinationDirectory = "src/main/generated/otherlocation/resources"

I have been able to figure out how to configure the task using the extension properties and afterEvaluate(). However it’s tricking me with the remaining stuff. What is a good approach to configure the source set, the clean task and the idea plugin (see the #n: TODO comments in the plugin code below)? The implementation below will always use the default value, not the one injected through the plugin extension.

class ArtifactMetadataPlugin implements Plugin<Project> {
        public static final String EXTENSION_NAME = 'artifactMetadata'
        public static final String TASK_NAME = 'generateArtifactMetadata'
        void apply(Project project) {
            project.configure (project) {
                      task (TASK_NAME, type: GenerateArtifactMetadata) {
                    group =
                    artifact =
                    version = project.version.toString()
                      sourceSets {
                    main {
                        // #1:TODO to get the plugin extension property current value here
                          output.dir(project.artifactMetadata.destinationDirectory, builtBy: TASK_NAME)
                        resources.srcDirs += file(project.artifactMetadata.destinationDirectory)
                      clean {
                    // #2:TODO get the plugin extension property here
                                           delete file(project.artifactMetadata.destinationDirectory)
                      if (project.plugins.hasPlugin(IdeaPlugin)) {
                    idea {
                        module {
                            // #3:TODO get the plugin extension property here
                                                   sourceDirs += file(project.artifactMetadata.destinationDirectory)
            project.afterEvaluate {
                def extension = project.extensions.findByName(EXTENSION_NAME)
                project.tasks.withType(GenerateArtifactMetadata).all { task ->
                    task.destinationDirectory = project.file(extension.destinationDirectory)
              private static void createExtension(Project project) {
            def extension = project.extensions.create(EXTENSION_NAME, ArtifactMetadataPluginExtension)
            extension.with {
                destinationDirectory = "src/main/generated/artifactinfo/resources"
1 Like