To get the current Gradle version we can use the gradleVersion property of the Gradle object. This returns a string value we can use for displaying the values. If we want to compare Gradle versions we can use the GradleVersion object. With this class we can get the current version, but we can also compare Gradle versions. This can be useful in our build scripts if we have functionality based on a Gradle version.

In the following build file we first have a task that uses the gradleVersion of Gradle. Then inside the task we use the static method current of the GradleVersion class. We get an GradleVersion instance and we display different properties from this instance. In the task compareGradleVersion we create a GradleVersion instance with the static version method. We compare multiple GradleVersion objects and have different functionality based on the Gradle version.

00.task printGradleVersion << {
01.// Get current Gradle version as object.
02.final GradleVersion gradleVersion = GradleVersion.current()
03. 
04.// Print different properties.
05.println "Your Gradle version is ${gradleVersion.version}"
06.println "Base version: ${gradleVersion.baseVersion}"
07.println "Build time  : ${gradleVersion.buildTime}"
08.println "Build number: ${gradleVersion.buildNumber}"
09.println "Commit id   : ${gradleVersion.revision}"
10.println "Next major  : ${gradleVersion.nextMajor}"
11.println "Snapshot?   : ${gradleVersion.snapshot}"
12.}
13. 
14.task compareGradleVersion << {
15.// Current Gradle version.
16.final GradleVersion gradleVersion = GradleVersion.current()
17. 
18.// Gradle version 2.1 as GradleVersion object.
19.final GradleVersion gradle2_1 = GradleVersion.version('2.1')
20. 
21.// Compare versions.
22.if (gradleVersion > gradle2_1) {
23.println "Your Gradle version is newer than 2.1"
24.} else if (gradleVersion == gradle2_1) {
25.println "Your Gradle version is 2.1"
26.} else {
27.println "Your Gradle version is older than 2.1"
28.}
29.}

When we run the tasks we get the following output:

$ gradle -q printGradleVersion
Gradle version is 2.2
 
Your Gradle version is 2.2
Base version: Gradle 2.2
Build time  : 2014-11-10 13:31:44 UTC
Build number: none
Commit id   : aab8521f1fd9a3484cac18123a72bcfdeb7006ec
Next major  : Gradle 3.0
Snapshot?   : false
$ gradle -q compareGradleVersion
Your Gradle version is newer than 2.1
$

Thanks to John Engelman who showed me this class on a pull request for the Gradle Grails plugin.

Written with Gradle 2.2.

Gradle Goodness: Using and Working with Gradle Version的更多相关文章

  1. Gradle Goodness: Copy Files with Filtering

    Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filterin ...

  2. Gradle Goodness: Task Output Annotations Create Directory Automatically

    Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of ...

  3. Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects

    Gradle Goodness: Init Script for Adding Extra Plugins to Existing Projects Gradle is very flexible. ...

  4. Configuration on demand is not supported by the current version of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org

    androidStudio打开cocos3.17.2Lua项目时,出现了 Configuration on demand is not supported by the current version ...

  5. 彻底搞懂Gradle、Gradle Wrapper与Android Plugin for Gradle的区别和联系

    首先用一段通俗易懂但是不是非常专业的话描述一下三者的概念.区别和联系. Gradle是个构建系统,能够简化你的编译.打包.测试过程.熟悉Java的同学,可以把Gradle类比成Maven. Gradl ...

  6. Gradle快速上手——从Maven到Gradle

    [本文写作于2018年7月5日] 本文适合于有一定Maven应用基础,想快速上手Gradle的读者. 背景 Maven.Gradle都是著名的依赖管理及自动构建工具.提到依赖管理与自动构建,其重要性在 ...

  7. Gradle项目构建(1)——Gradle的由来

    一.项目自动构建介绍 作为Java的开发者对eclipse都非常熟悉,其实eclipse就是居于ant来构建项目的,我们先来看看为什么需要自动化构建项目. 1.为什么我们要自动化构建项目 可以假设我们 ...

  8. Gradle学习总结——抓重点学Gradle

    前言 网上关于Gradle的教程很多,但很多都是以"面"切入- 通过大量讲解其用法及其API分类来阐述.但Gradle API使用技巧众多,API更是成千上百,臣妾记不住呀.个人深 ...

  9. 解决gradle /Users/xxxx/Documents/workspace/fontmanager/.gradle/2.2.1/taskArtifacts/cache.properties (No such file or directory)报错办法

    git 上down下项目后,发现Android Studio报错: What went wrong: java.io.FileNotFoundException: /Users/raomengyang ...

随机推荐

  1. iOS下日期的处理

    NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates         NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是 ...

  2. Couchbase server---Enyim.Caching.dll

    本文不打算抄袭官方或者引用他人对Couchbase的各种描述,仅仅是自己对它的一点理解(错误之处,敬请指出),并附上一个入门示例. ASP.NET Web项目(其他web开发平台也一样)应用规模小的时 ...

  3. perl DBI 学习总结(转载)

    perl DBI 学习总结 源文地址:http://blog.csdn.net/like_zhz/article/details/5441946 DBI和DBD的不同关系模型: ########### ...

  4. Qt---- 点击按钮调用另一个窗口Ui

    -------------------------------------------------- #include "subdialog.h" SubDialog::SubDi ...

  5. vi/vim编辑器

    vi / vim是Unix / Linux上最常用的文本编辑器而且功能非常强大.

  6. Could not resolve placeholder

    使用spring的<context:property-placeholder location="/WEB-INF/redis.properties"/>读取prope ...

  7. 高效线程池(threadpool)的实现

    高效线程池(threadpool)的实现 Nodejs编程是全异步的,这就意味着我们不必每次都阻塞等待该次操作的结果,而事件完成(就绪)时会主动回调通知我们.在网络编程中,一般都是基于Reactor线 ...

  8. NET Core中使用Redis

    NET Core中使用Redis 注:本文提到的代码示例下载地址> https://code.msdn.microsoft.com/How-to-use-Redis-in-ASPNET-0d82 ...

  9. Java缓冲流细节

    FileOutPutStream继承OutputStream,并不提供flush()方法的重写所以无论内容多少write都会将二进制流直接传递给底层操作系统的I/O,flush无效果.而Buffere ...

  10. NEST.net Client For Elasticsearch简单应用

    NEST.net Client For Elasticsearch简单应用 由于最近的一个项目中的搜索部分要用到 Elasticsearch 来实现搜索功能,苦于英文差及该方面的系统性资料不好找,在实 ...