Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle plugin project version number

Tags:

plugins

gradle

I have a gradle plugin that uses the project.version variable.

How ever, the version in the plugin does not update when I change the version in the build.gradle file.

To illustrate:

plugin

// my-plugin
void apply(Project project) {
  project.tasks.create(name: 'printVersionFromPlugin') {
    println project.version
  }
}

build.gradle

version '1.0.1' // used to be 1.0.0

task printVersion {
  println project.version
}

apply plugin: 'my-plugin'

Result

> gradle printVersion
1.0.1
> gradle printVersionFromPlugin
1.0.0
like image 462
Theodor Avatar asked Jul 24 '14 14:07

Theodor


2 Answers

You can use gradle properties to extract project version without adding a dedicated task to the build.gradle file.

For example:

gradle properties -q | grep "version:" | awk '{print $2}'
like image 53
uris Avatar answered Sep 30 '22 04:09

uris


Both the build script and the plugin make the same mistake. They print the version as part of configuring the task, rather than giving the task a behavior (task action). If the plugin is applied before the version is set in the build script (which is normally the case), it will print the previous value of the version property (perhaps one is set in gradle.properties).

Correct task declaration:

task printVersion {
    // any code that goes here is part of configuring the task
    // this code will always get run, even if the task is not executed
    doLast { // add a task action
        // any code that goes here is part of executing the task
        // this code will only get run if and when the task gets executed
        println project.version
    }
}

Same for the plugin's task.

like image 42
Peter Niederwieser Avatar answered Sep 30 '22 02:09

Peter Niederwieser