Multi-project build with NetKernel Gradle plugin Part3: Externalize common buildscript configuration into reusable script plugin

This blog post has one goal:

  1. to externalize common buildscript configuration into reusable script plugin in order to prevent duplication in all sub-projects which would need to use a particular block of build configuration

Limitations of the Gradle configuration inheritance

Externalizing a build script block into a script plugin has limited functionality in terms of visibility of properties. Specifically, project properties are not visible in 'external scripts' (called 'script plugins' in Gradle parlance). All is not lost, though. System properties to the rescue. Once a system property is defined it becomes visible in all build scripts and script plugins that are executed in same JVM in which the system property was defined.

It's standard Gradle functionality that repositories and dependencies specified in a project's 'buildscript' block are visible to all subprojects. Repositories and dependencies externalized into a script plugin are also visible to all projects that apply that particular script plugin.

Using a system property to define the Netkernel Gradle plugin version

Define the NetKernel Gradle plugin version as a system property in the gradle.properties file in the root directory of the root project like this:

systemProp.netkernelGradlePluginVersion=1.1.9

Using a script plugin to externalize common build script configuration

Create a separate script plugin file to contain the common build configuration block that we want to be externalized. Place the script plugin file in the root directory of the root project. Script plugin file name can be arbitrarily chosen. In our example we named it common-buildscript.gradle

repositories {
    mavenLocal()
    mavenCentral()
    maven {
        url "http://maven.netkernel.org/netkernel-maven"
    }
}

dependencies {
    classpath group: 'urn.org.netkernel', name: 'gradle.plugin', version: "[${System.properties.netkernelGradlePluginVersion},)"
}

Include the script plugin into a project to reuse build configuration

Include the externalized build configuration into the build.gradle files of the projects that compose the multi-project build like this:

apply plugin: 'netkernel'

buildscript {
    //Provide this build script with repositories and dependencies to obtain the netkernel plugin
    apply from: "$rootProject.projectDir/common-buildscript.gradle", to: buildscript
}

Building all projects in a multi-project build

To build all projects that compose a multi-project build, run the following command line from the root directory of the root project, like this:

C:\data\sandbox\g-p\s\t\r\examples> gradle build

Building single project

To build a single sub-project, run one of the commands below: 

  • run the following command line from the root directory of the root project:
C:\data\sandbox\g-p\s\t\r\examples> gradle :01-single-module:build
  • run the following command line from sub-project's root directory:
C:\data\sandbox\g-p\s\t\r\e\01-single-module> gradle build

Example projects using the Netkernel Gradle plugin

For complete code that uses the NetKernel Gradle plugin, one can explore the multi-project build sample project on GitHub.