Multi-project build with NetKernel Gradle plugin Part2: Parameterize the version of the NetKernel Gradle plugin

This blog post has one goal: 

  1. to centralize the version of the NetKernel Gradle plugin and reference that single version definition in all the build.gradle scripts of a multi-module build

For a multi-module build one can use either a project property or a system property to define the version number in a single place and reference that property in all sub-projects which need it. It's standard Gradle behavior to define the properties in the gradle.properties file in the root directory of the root project or for the properties to be passed on the command line that runs the Gradle build. For the sake of completeness, we will illustrate defining the properties in the top level gradle.properties file and also as command line arguments to be passed to the build scripts of sub-projects.

Defining the properties in the top level gradle.properties

Before centralizing definition of the NetKernel Gradle plugin version - regardless whether version is defined as project or system property - the relevant section of the build.gradle script of any sub-project that is part of the multi-project build looks like:

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

    dependencies {
        classpath group: 'urn.org.netkernel', name: 'gradle.plugin', version: '[1.1.9,)'
    }
}

Defining the version as a project property

After centralizing definition of the NetKernel Gradle plugin version:

The relevant section of the top level gradle.properties looks like:

netkernelGradlePluginVersion=1.1.9

The relevant section of the build.gradle script looks like:

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

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

Defining the version as a system property

After centralizing definition of the NetKernel Gradle plugin version:

The relevant section of the top level gradle.properties looks like:

systemProp.netkernelGradlePluginVersion=1.1.9

The relevant section of the build.gradle script looks like:

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

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

Defining properties on the command line

Note: In a multi-project build, “systemProp.” properties set in any project except the root will be ignored. That is, only the root project's gradle.properties file will be checked for properties that begin with the “systemProp.” prefix.

We can define system properties in the JVM which runs Gradle using the command line option --system-prop or -D.
We can define project properties in the project object using the command line option --project-prop or -P.

Sample command line that defines a system property:

gradle --system-prop "netkernelGradlePluginVersion=1.1.9"

Sample command line that defines a project property:

gradle --project-prop "netkernelGradlePluginVersion=1.1.9"

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.

Next steps

In the next part of this tutorial series we'll centralize the boiler plate configuration of the NetKernel Gradle plugin. This way sub-projects will inherit the common configuration which will be defined in one place only.