Gradle plugin project version number

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

Be that as it may, the version in the plugin is not updated when the version in the file changes build.gradle.

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
+4
source share
2 answers

Both script and plugin assemblies make the same error. They print the version as part of the task settings, and do not give the task behavior (task action). If the plugin is used before the version is installed in the script assembly (as it usually happens), it will display the previous value of the property version(perhaps one of them is set to 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
    }
}

.

+6

gradle build.gradle.

:

gradle properties -q | grep "version:" | awk '{print $2}'
+1

All Articles