Over a million developers have joined DZone.

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

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

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


The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:
build script ,scripting ,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.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}