Sometimes we want to include the classes from a plugin, like tasks, in our build class path without actually applying the plugin. Or we want to add the classes to the root project and actually apply the plugin in subprojects. We can achieve this with a buildScript block and add the plugin dependency to the classpath configuration. But we can also do this with the newer plugins configuration block. Inside the plugins block we define the id and the version of the plugin, and since Gradle 3.0 we can also use the apply method. We have to set the value false to include the plugin to the class path, but not apply it to the project.

In the following example we add the Asciidoctor plugin to our build file, but we only want to use the AsciidoctorTask task from this plugin.

plugins {
    // Add Asciidoctor plugin, but do not apply it.
    id 'org.asciidoctor.convert' version '1.5.3' apply false
}

configurations {
    convert
}

repositories {
    jcenter()
}

dependencies {
    convert 'org.asciidoctor:asciidoctorj:1.5.4'
}

// Use of Asciidoctor task from the Asciidoctor plugin.
task convert(type: org.asciidoctor.gradle.AsciidoctorTask) {
    classpath = configurations.convert
}

Another use case can be that we have a multi-project build. We want to configure all plugins in a plugins configuration block in the root build file. And based on certain conditions we want to actually apply the plugin to a subproject. In the following example we add the Asciidoctor plugin and apply it to all subprojects where the name ends -doc:

plugins {
    id 'org.asciidoctor.convert' version '1.5.3' apply false
}

subprojects {
    if (name.endsWith('-doc')) {
        apply plugin: 'org.asciidoctor.convert'
    }
}

Written with Gradle 3.1.

Original blog post

shadow-left