Loading...

October 21, 2015

Gradle Goodness: Apply External Script With Plugin Configured Through Buildscript

Suppose we use the Gradle apply from: statement to import another Gradle build file into our build file. The external build file uses a Gradle plugin that needs a buildscript block to define the classpath configuration with the classes needed for the plugin. We cannot use the plugin id inside our external build script to use it, but we must use the type of the plugin. Otherwise Gradle cannot resolve the plugin from the main build file.

Let's create a simple Gradle build that we want to include in our main Gradle build file:

// File: gradle/extra.gradle
buildscript {
    repositories.jcenter()
    dependencies.classpath 'com.bmuschko:gradle-docker-plugin:2.6.1'
}

// We use the type of the plugin instead of the id.
// This is the class that defines the plugin. We can leave of
// .class, because Gradle uses Groovy.
apply plugin: com.bmuschko.gradle.docker.DockerRemoteApiPlugin

// The following statement doesn't work if this file
// is included via apply from: in another Gradle build file.
// apply plugin: 'com.bmuschko.docker-remote-api'

...

In the following Gradle build file we import this script:

// File: build.gradle
apply from: 'gradle/extra.gradle'
...

Written with Gradle 2.8.