Gradle 5.0 正式版本发布,一大波新特性来袭

前言

在历经了一年多时间, 20 个 4.x 系列版本的迭代后,Gradle 官方终于在 11月26日 发布了 5.0 的正式版本,先放上一张官方各版本 Gradle 性能对比图html

官方表示这是至今为止最快、最安全,最强大的版本。java

  • 改进的增量编译
  • 更快的构建速度
  • 细粒度的传递依赖管理
  • 更高效的内存执行等等。
  • ......

让咱们赶忙来看一下有哪些使人激动的新特性。git

官方新特性一览

  • Kotlin DSL 1.0
  • Dependency version alignment
  • Gradle build initialization features
  • Searchable documentation
  • Task timeouts
  • HTTP retries during dependency resolution
  • Java 11 runtime support
  • Plugin authoring features
  • Gradle Native features
  • Promoted features

我擦,感受一脸懵逼,不要紧,下面我会针对这些特性作些简要的总结和说明,内容可能比较多,可是干货满满,建议耐心阅读下去。github

Kotlin DSL 1.0

早在 Gradle 4.x 版本中就已经支持了经过使用 Koltin DSL 的方式去编写构建脚本,可是当时刚出来,不少地方支持的还不太好(须要踩坑),因此并不推荐你们迁移过去,而是尝鲜为主。如今 Kotlin DSL 1.0 release 版本出来后,意味着接下来你能够放心的迁移或者使用 Kotlin DSL 啦。新版本作了不少优化和改进的地方,好比:spring

  • Code Completion:代码自动完成缓存

  • Error Highlighting:错误高亮安全

  • Quick Documentation:文档快速提示网络

  • Refactoring:代码重构app

    图片来自官方

如何迁移你的构建语言到 Kotlin DSL 上,能够参考个人另外一篇文章Gradle指南之从Groovy迁移到Kotlin dom

Dependency version alignment

Dependency version alignment allows different modules belonging to the same logical group (a platform) to have identical versions in a dependency graph.

根据官网的介绍,直译过来的意思是:依赖版本一致性容许属于相同的逻辑组(平台)的不一样module 拥有相同的版本依赖图。

这个概念确实很差理解,我本身也是花了一些时间去理解和消化。仍是经过一个示例来讲明吧。若是有英文水平高或者理解能力比较强的同窗,欢迎指教。

好比,咱们 build.gradle 有如下依赖:

dependencies {
	    // a dependency on Jackson Databind
	    implementation 'com.fasterxml.jackson.core:jackson-databind:2.8.9'

	    // and a dependency on vert.x
	    implementation 'io.vertx:vertx-core:3.5.3'
	}
复制代码

由于 vertx-core 也间接依赖了 jackson-core,经过依赖传递,当解析依赖信息的时候,咱们实际依赖的版本号是:

  • jackson-core 版本 2.9.5(依赖传递)
  • jackson-databind 版本 2.8.9 -> 2.9.5(低版本经过依赖仲裁变为高版本)
  • jackson-annotation 版本 2.9.0 (jackson-databind 2.8.9 版本间接依赖 jackson-annotation 2.9.0 版本,注意这里,由于上面的依赖仲裁,变成了被高版本 的 jackson-databind 2.9.5 所依赖)

这里建议有兴趣的同窗实际在 intellij IDEA CE 里建个简单的 Gradle 工程,根据上面说的步骤实际操做一遍就很容易理解了。

再举个小例子,看下下面的依赖关系:

+--- com.google:gson:1.0.0 -> 2.0.0
	|    \--- com.google:gson-test:1.0.0
	+--- commons-io:commons-io:2.0.0
	  \--- com.google:gson:2.0.0
复制代码

gson 被仲裁到 2.0.0 高版本,而 gson-test 仍是 1.0.0 版本,至关于原来是 gson:1.0.0 依赖的是的 gson-test:1.0.0 ,如今变成了高版本的 gson:2.0.0 依赖了低版本的 gson-test:1.0.0,这样就颇有可能会出现一些未知问题。理想的依赖应该是 gson:2.0.0 依赖的 gson-test:2.0.0。

经过以上的分析,咱们发现实际上解析出来的依赖库版本出现了和预期不一致的状况,而相似这种状况很容易会致使一些未知问题,尤为是在一些不兼容老版本接口的依赖库里就会直接致使 Crash。

一般,针对这种问题,以前的作法可能就是简单粗暴的强指定版本号,不过,如今有了更加优雅的方式去帮助咱们去管理版本,即 Dependency version alignment,咱们能够经过指定一个 platform 去管理某一组的 module

回到刚才的场景示例中,咱们在 build.gradle 里能够定义 jackson 为一组的 module 版本的规则,以下所示:

class JacksonAlignmentRule implements ComponentMetadataRule {
	    void execute(ComponentMetadataContext ctx) {
	        ctx.details.with {
	            if (id.group.startsWith("com.fasterxml.jackson")) {
	                // declare that Jackson modules all belong to the Jackson virtual platform
	                belongsTo("com.fasterxml.jackson:jackson-platform:${id.version}")
	            }
	        }
	    }
	}
复制代码

而后应用该规则:

dependencies {
	    components.all(JacksonAlignmentRule)
	}
复制代码

最后,在执行完 ./gradlew app:dependencies 命令后,你会发现当匹配上 com.fasterxml.jackson 组的 module 的版本都保持了一致,也就是

  • jackson-core 版本 2.9.5
  • jackson-databind 版本 2.8.9 -> 2.9.5
  • jackson-annotation 版本 2.9.0 -> 2.9.5

另外,platform 一样也提供了强制为一组 module 指定某个版本号,好比:

dependencies {
	    // Forcefully downgrade the Jackson platform to 2.8.9
	    implementation enforcedPlatform('com.fasterxml.jackson:jackson-platform:2.8.9')
	}
复制代码

还有一个比较方便的地方,就是经过 platform,咱们能够直接省略该 module 的版本号(今后告别经过定义版本变量去维护众多 module 的版本信息),以下所示:

dependencies {
	    // import a BOM. The versions used in this file will override any other version found in the graph
	    implementation(enforcedPlatform("org.springframework.boot:spring-boot-dependencies:1.5.8.RELEASE"))

	     // define dependencies without versions
	    implementation("com.google.code.gson:gson")
	    implementation("dom4j:dom4j")

	     // this version will be overriden by the one found in the BOM
	    implementation("org.codehaus.groovy:groovy:1.8.6")
	}
复制代码

Gradle build initialization features

新版本对 gradle init 方法进行了升级,经过更多的特性和交互,来帮助咱们去快速初始化一个 Gradle 项目。简单来讲的话,就是增强版的脚手架。

Interactive mode

当你在控制台执行 init task 后,Gradle 将会提供更多的构建信息提示,来帮助你生成 Gradle 模版项目。

Kotlin library and applications

init task 能够经过 kotlin-library 或者 kotlin-application 类型来设置生成一个 Kotlin 的类库或者应用。你只须要根据它的提示来操做便可。

Generated builds use recommended configurations

init task 生成的构建脚本将推荐使用新的 implementationtestImplementationtestRuntimeOnly 去代替 compiletestCompiletestRuntime

Configure project and source package names

  • --project-name 选项能够帮助你调整生成的项目名称
  • --package 选项能够帮助你调整生成的项目包名

Create resource directories

init task 会建立一个空的 resource 目录

Create a .gitignore file

init task 会生成一个简单的 .gitignore 文件来帮助你设置你的 Git repository 。

Searchable documentation

Gradle 的文档搜索功能回来了(虽然我一直不知道这个功能在哪里),总之,能够更加方便的搜索用户手册和 DSL 的描述。

固然,还有更方便查找类和方法的 Gradle API Javadocs

Task timeouts

这个超时设置简直太有用了有木有。特别是当你由于一些网络缘由,致使你的 build task 一直卡在那里的时候,你的内心必定是万马奔腾的感受,赶忙用起来吧,不要再浪费这些无用的时间了。示例:

task hangingTask() {
	    doLast {
	        Thread.sleep(100000)
	    }
	    timeout = Duration.ofMillis(500)
	}
复制代码

HTTP retries during dependency resolution

HTTP 访问重试功能,嗯。挺好

Performance features

Gradle can be started as a low-priority process

经过 --priority low 命令参数或者 org.gradle.priority=low 属性设置可让你的 Gradle 以一个低优先级的进程启动,至于好处嘛,固然是你一边在 building 的时候,一边听音乐的时候,不再会感受一卡一卡的了 :)

JaCoCo plugin now works with the build cache and parallel test execution

简单看了下介绍,能够帮助你查看代码覆盖率的 JaCoCo 插件,感兴趣的同窗能够点这里了解更多

Java 11 runtime support

Gradle 5.0 版本构建支持 Java 11 版本

Plugin authoring features

这个版本为插件和自定义 task 提供了建立 SourceDirectorySet 的 API、提升 Provider 的 API、和构建缓存的兼容性提升。

Gradle Native features

Gradle Native project 持续的改善和提高 native 生态系统, 更多细节:Changes included in Gradle 5.0

Promoted features

一些现有功能的提高,详细点击这里

其余

5.0 版本共计修复了 166 个 issues(惊人),固然,伴随而来的还有一些已知的 issues ,详细能够点击这里 5.0 release-notes

总结

Gradle 5.0 版本真的是干货满满,尤为是对于使用 Gradle 构建的 Android 或者 Java 开发者来讲,像 Kotlin DSL(构建语言)、Dependency version alignment(依赖版本一致性)、Performance features(性能提高) 等等这些特性仍是很让人期待的。有兴趣的小伙伴能够赶忙更新一波了。

如何升级到 Gradle 5.0 版本呢,很简单,如下两种方式任选其一

  • 执行 ./gradlew wrapper --gradle-version=5.0 命令

  • 或者直接修改你的 rootProject/gradle/wrapper/gradle-wrapper.properties

    distributionBase=GRADLE_USER_HOME
      distributionPath=wrapper/dists
      distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-all.zip
      zipStoreBase=GRADLE_USER_HOME
      zipStorePath=wrapper/dists
    复制代码
  • 附上官方升级说明: Upgrade Gradle 4.x to 5.0
  • 须要注意的是:目前 Android 的项目若是要升级到 5.0 版本的 Gradle ,须要配套的升级 Android Gradle Plugin 和 Android Studio 到 3.4 版本,因为 5.0 Gradle 版本刚出来,Android Studio 3.4 版本如今仍是预览版,因此建议想升级的小伙伴,再耐心等待一段时间。
相关文章
相关标签/搜索