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.
Join the DZone community and get the full member experience.
Join For FreeSuppose 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
import from: 'gradle/extra.gradle'
...
Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments