getVersion() in custom Gradle plugin is always uns

2019-09-02 03:10发布

问题:

Related to How to Get $project.version in Custom Gradle Plugin?

Executive summary: getVersion() inside of custom gradle plugin returns "unspecified"

I have created a custom gradle plugin that applies some business logic to our Android Studio / Gradle builds. This plugin is run every time we do a CI build.

I want the plugin to be able to print out its version, which is an entirely different version number from that of the Android application being built, during builds. (useful debugging information during CI builds)

The build.gradle for the plugin (that gets deployed to a maven repo as a jar):

buildscript {
    repositories {
        mavenCentral()
    }
}


apply plugin: 'groovy'
apply plugin: 'maven'

group = 'com.example.foo.gradle'
version = "0.0.12"

sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6

dependencies {
    repositories {
        mavenCentral()
    }

    compile gradleApi()
    compile localGroovy()
}



configurations {
    includeInJar
}

dependencies {
    //testCompile 'junit:junit:4.11'
}


jar {

}


uploadArchives {
    repositories.mavenDeployer {
        def deployPath = file(getProperty('aar.deployPath'))
        repository(url: "file://${deployPath.absolutePath}")
        pom.project {
            groupId project.group
            artifactId 'foo-gradle-jenkins'
            version project.version
        }
    }
}

... and the plugin is applied to an Android app like this (sensitive parts omitted):

buildscript {
    repositories {
        mavenCentral()
        maven {   url "../../../plugin_builds" }
    }
    dependencies {
        classpath 'com.example.foo.gradle:foo-gradle-jenkins:0.0.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'foo'


android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.example.android.examplegradleapplication3"
        minSdkVersion 14
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

In the plugin I attempt to print its version like this:

@Override
    void apply(Project project) { 

        project.configure(project) {
            if (it.hasProperty("android")) {

                if (isJenkinsBuild()) { 
                    log("Plugin version: " + getVersion()) 

                    // ...
                }

                // ...
            }
        }
    }

.. but whenever I perform gradle build of the Android app it thinks getVersion() is unspecified.

What should I be doing instead to track the plugin version at the time of another gradle build using it as a plugin?

回答1:

project.configure(project) { println getVersion() } is the same as println project.version. If this prints unspecified, the build script didn't set (project.)version.

As of Gradle 2.2, there isn't a built-in way for a plugin to declare or query its own version, but you could implement this yourself (for your own plugins).



回答2:

AppExtension android = (AppExtension) project.extensions.findByName("android")
String versionName = android.defaultConfig.versionName
String versionCode = android.defaultConfig.versionCode