getVersion()定制插件摇篮总是不确定的 [英] getVersion() in custom Gradle plugin is always unspecified

查看:292
本文介绍了getVersion()定制插件摇篮总是不确定的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关
  <一href=\"http://stackoverflow.com/questions/13198358/how-to-get-project-version-in-custom-gradle-plugin\">How在自定义插件摇篮获取$ project.version?

摘要: getVersion()的gradle定制插件内返回未指定

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

我已经创建了一些适用于业务逻辑我们的Andr​​oid工作室/摇篮建立一个自定义插件的Gradle。这个插件运行,每次我们做CI生成时间。

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.

我想要的插件能够打印出它的版本,这是不同于Android应用程序的一个完全不同的版本号正在建造,在建造。 (CI中有用的调试信息编译)

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)

对于插件的build.gradle(也被部署到一个Maven回购作为一个jar):

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
        }
    }
}

...和插件应用到Android应用程序是这样的(敏感部位略):

... 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()) 

                    // ...
                }

                // ...
            }
        }
    }

..但每当我的Andr​​oid应用程序的执行gradle这个版本认为getVersion()是不确定的。

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

我应该怎么做,而不是使用它作为一个插件来追踪另一个构建的gradle的时候插件版本?

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

推荐答案

project.configure(项目){的println getVersion()} 相同的println project.version 。如果打印未指定,构建脚本没有设置(项目)版本

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

由于摇篮2.2,没有一个插件来声明或查询自己的版本内置的方式,但你可以自己实现(为自己的插件)。

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).

这篇关于getVersion()定制插件摇篮总是不确定的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆