How to use custom plugin to set version

I’ve written a custom plugin (my first) which manages a “build number” which I want to include in the build.gradle “version” as so:

allprojects {
  version = "1.2.3.${buildNumber}"

Unfortunately my plugin is never run and thus the buildNumber returned is “null”.

Below is my build.gradle.

If I run “gradle showbuild” I see the right buildNumber.

If I run “gradle showInfo” the build number reported is “null”.

Somehow I need to get gradle to call my plugin task ‘buildInfoLoad’ before the “version” value is set in allprojects. Since I’m knew to gradle I am struggling with this.

Any pointers would be most appreciated!

buildscript {
	repositories {
		maven {
			// Access to MangoGrove
			url uri('../repo')
		maven { // aka "jcenter()"
			url ""

	dependencies {
		// Provide the "provided" and "optional" methods for dependencies
		classpath ''

		classpath group: 'org.mangogrove.gradle',
				  name: 'MangoGrove',
				  version: '1.0.0-SNAPSHOT'

apply plugin: 'org.mangogrove.gradle'

task showbuild(dependsOn: 'buildInfoLoad') << {
	println("BI build number: $buildinfo.buildNumber")
	println("BI build time: $buildinfo.buildTime")

task createbuild(dependsOn: 'buildInfoCreate') << {
	println("BI build number: $buildinfo.buildNumber")
	println("BI build time: $buildinfo.buildTime")

allprojects {
	ext {
		buildNumber = "$buildinfo.buildNumber"
	version = "1.0.0.${buildNumber}-alpha2"
	//version = "1.0.0.${buildinfo.buildNumber}-alpha2"
	apply plugin: 'eclipse'

	eclipse {
		classpath {


task showInfo << {
	println("Product Version is $version")	
	println("Product BuildNumber is $buildNumber")

I take it the version is set by the ‘buildInfoLoad’ task? In that case this won’t work because the version will only ever be set if and when that task is run. The version number is need at configuration time so your plugin will need to set this value outside of a task action, basically, just inside the apply() method.

I tried modifying my plugin so in apply I call


and in the root build.gradle I do not set “version = …” at all

Unfortunately something like compileJava:jar seems to reference “project.version” before my plugin is loaded and thus before it sets project.setVersion().

How do I insure my plugin is loaded before this?

Here’s what I tried…

In my plugin:

public class MangoGrovePlugin implements Plugin<Project> {

	public void apply(Project project) {

        project.getExtensions().create("buildinfo", BuildInfoPluginExtension.class);
        project.getTasks().create("buildInfoLoad", BuildInfoLoadTask.class);
        project.getTasks().create("buildInfoCreate", BuildInfoCreateTask.class);


In my root bundle.gradle:

apply plugin: 'org.mangogrove.gradle'
apply plugin: 'java'

task showbuild(dependsOn: 'buildInfoLoad') << {
	println("BI build number: $buildinfo.buildNumber")
	println("BI build time: $buildinfo.buildTime")

task loadBuildInfo(dependsOn: 'buildInfoLoad') {
	doFirst {
		println("Loading build info??")
		//	println("build number: " + buildNumber)
		println("BI build number: $buildinfo.buildNumber")
		println("BI build time: $buildinfo.buildTime")
		project.buildNumber = "$buildinfo.buildNumber"

allprojects {
	ext {
	//	buildNumber = "10"
		  	buildNumber = "$buildinfo.buildNumber"
		//		buildNumber = buildinfo.buildNumber
		//		buildNumber = tasks.buildInfoLoad.buildinfo.buildNumber
		productVersionBase = "1.0.0"
		productVersion4dot = "${productVersionBase}.${buildNumber}"
		productVersionStatus = "alpha2"
		productVersion = "${productVersionBase}.${buildNumber}-${productVersionStatus}"
	//version = "${productVersion}"

With this setup if I run “gradle :Model:showbuild” the buildNumber is “null”:

% grw :Model:showbuild
init                           | Initializing BuildInfoLoadTask_Decorated
init                           | Initializing BuildInfoCreateTask_Decorated
Using Gradle Vaadin Plugin 0.10.5
load                           | Loading properties
getPropertiesFile              | Project dir: /home/mcooper/ws/cmb/CmbProduct
getPropertiesFile              | Properties file: /home/mcooper/ws/cmb/CmbProduct/buildinfo/
load                           | PROPS BuildNumber: 17
BuildNumber: null
Build Info Build Number: 17
Product Version: 1.0.0.null-alpha2
Project Version: unspecified


Here is showbuild from Model:

task showbuild(dependsOn: ':buildInfoLoad') << {
	println("BuildNumber: $buildNumber")
	println("Build Info Build Number: " + buildinfo.buildNumber)
	println("Product Version: " + productVersion)
	println("Project Version: " + version)

The output above including the “load …” lines are debug output from my custom plugin.

I figured it out. I was setting version in only the root buildscript. This is what I used as the basis to fix the problem:

String version = "....";
		project.getAllprojects().forEach(p -> {