Over a million developers have joined DZone.

Apply External Script With Plugin Configured Through Buildscript in Gradle

DZone 's Guide to

Apply External Script With Plugin Configured Through Buildscript in Gradle

How to import other Gradle build files into your build file that require an external plugin used by the other Gradle file.

· DevOps Zone ·
Free Resource

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 {
    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
import from: 'gradle/extra.gradle'

build script ,scripting ,Gradle

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}