Gradle Goodness: Using and Working with Gradle Version

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.

view sourceprint?

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.

时间: 2024-10-13 06:28:48

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

Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method

Gradle Goodness: Task Output Annotations Create Directory Automatically

Gradle Goodness: Task Output Annotations Create Directory Automatically One of the great features of Gradle is incremental build support. With incremental build support a task is only executed if it is really necessary. For example if a task generate

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. One of the ways to alter the build configuration is with initialization or init scripts. These are like other Gradle scripts but are executed before t

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 of the Android Gradle plugin since you are using Gradle version 4.6 or above. Suggestion: disable configuration on demand by setting org.gradle.confi

Gradle Goodness: Changing Name of Default Build File

Gradle uses the name build.gradle as the default name for a build file. If we write our build code in a file build.gradle then we don't have to specify the build filename when we run tasks. We can create build files with a different name other than b

Gradle、Gradle Wrapper与Android Plugin for Gradle

欢迎和大家交流技术相关问题: 邮箱: [email protected] 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://github.com/jiangxincode 知乎地址: https://www.zhihu.com/people/jiangxinnju 首先用一段通俗易懂但是不是非常专业的话描述一下三者的概念.区别和联系. Gradle是个构建系统,能够简化你的编译.打包.测试过程.熟悉Java的同学,可以把Gra

Gradle快速上手——从Maven到Gradle

[本文写作于2018年7月5日] 本文适合于有一定Maven应用基础,想快速上手Gradle的读者. 背景 Maven.Gradle都是著名的依赖管理及自动构建工具.提到依赖管理与自动构建,其重要性在当今软件环境下不言而喻,Maven也是红极一时. Maven采用约定大于配置的思想,约定了工程结构,生命周期,采用严谨的XML格式进行构建脚本编写,显著地提升了软件构建的效率.但当软件越来越复杂后,大家突然发现Maven的脚本编制已然成了另外一个麻烦.绝大部分的使用者会掉入Maven的plugin陷

Android通过Gradle发布多渠道版本遇到的Gradle版本问题解决

今天在通过Gradle发布多渠道版本的时候,遇到了Gradle版本过低导致不支持manifestPlaceholders字段的问题,想尽各种办法升级Gradle.Gradle的插件,最后终于成功了,在这里把方法分享给大家. 本人使用的是Intellij IDEA 13,Gradle版本是1.8,Gradle插件版本是0.6.+,需要将Gradle升级到2.1,插件版本升级到0.14+.遇到的错误是: Deprecated dynamic property: "manifestPlaceholde

eclipse gradle插件 org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from &#39;https://services.gradle.org/distributions/gradle-3.4-bin.zip&#39;.

eclipse安装gradle后出现如下异常: org.gradle.tooling.GradleConnectionException: Could not install Gradle distribution from 'https://services.gradle.org/distributions/gradle-3.4-bin.zip'.    at org.gradle.tooling.internal.consumer.DistributionFactory$ZippedDist