Gradle 实战

Gradle 是什么?

 Gradle™ 是一个基于 Apache Ant 和 Apache Maven 概念的项目自动化建构工具。它使用一种基于 Groovy 的特定领域语言来声明项目设置,而不是传统的 XML。当前其支持的语言限于 Java、Groovy 和 Scala,计划未来将支持更多的语言。 — wikipedia.org

特性

  • DSL 声明项目的配置,更加直观
  • 细粒度的传递依赖管理
  • 增量编译
  • 高效的内存执行

环境搭建

安装 Gradle

Windows

 在 Gradle 的下载页面,下载 gradle-8.1.1-bin.zip 文件,解压至 D:\apps\gradle,并添加环境变量 PATH=D:\apps\gradle\gradle-8.1.1\bin

Mac

1
2
$ brew install gradle
$ brew upgrade gradle

验证

1
2
# 检查是否安装成功
$ gradle -v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Picked up JAVA_TOOL_OPTIONS:  -Xmx12884m

------------------------------------------------------------
Gradle 8.1.1
------------------------------------------------------------

Build time: 2023-04-21 12:31:26 UTC
Revision: 1cf537a851c635c364a4214885f8b9798051175b

Kotlin: 1.8.10
Groovy: 3.0.15
Ant: Apache Ant(TM) version 1.10.11 compiled on July 10 2021
JVM: 11.0.19 (Azul Systems, Inc. 11.0.19+7-LTS)
OS: Linux 6.1.27-060127-generic amd64

Gradle 代理设置

1
2
3
4
5
6
7
8
9
10
$ gradle xxx -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=1080

# 或者修改 gradle.properties 配置文件
$ vim gradle.properties
systemProp.http.proxyHost=192.168.1.101
systemProp.http.proxyPort=8080
systemProp.http.nonProxyHosts=*.nonproxyrepos.com|localhost
systemProp.https.proxyHost=192.168.1.101
systemProp.https.proxyPort=8080
systemProp.https.nonProxyHosts=*.nonproxyrepos.com|localhost
如果在 Kafka 源码目录下修改的 gradle.properties 无法生效,可以直接拷贝到 ~/.gradle 目录下

编译源码

1
2
3
4
5
6
7
8
# 下载依赖
$ gradle

# 增加 `-x test` 参数,可跳过单元测试
$ gradle -x test

# 随后,生成可以用 Intellij Idea 打开的工程
$ gradle idea
如果 Gradle 的配置文件里面,没有设置 idea 插件,导致项目模块无法被识别。则需要删除 .idea 文件夹,并以 build.gradle 打开为项目,即可

实战技巧

不同的项目使用的 Gradle 版本不一致

 可以使用 ./gradlew 包装器,就可以避免使用本地安装的 Gradle。Gradlew 会根据项目各自指定的 Gradle 版本进行下载,确保和项目要求的编译环境一致。常用命令如下:

1
$ ./gradlew build

运行单元测试时,报错 No Class Found

 修改 Build and run usingRun tests using 两个选项为 Gradle(默认为 IntelliJ IDEA

Configurations of Gradle in Intellij Idea for Testcases

(对 IntelliJ IDEA™ 的截图)

并发执行任务

1
2
3
4
5
6
# 修改配置文件
$ vim gradle.properties
org.gradle.parallel=true

# 执行任务时指定参数
$ ./gradlew <task> --parallel

无法正常识别 module 下的代码

1
2
3
4
# 清理所有的 IDEA 配置文件
$ ./gradlew cleanIdea
# 生成 IDEA 配置文件
$ ./gradlew idea

加载本地 jar

1
2
3
4
5
6
// 加载单个 jar 包
dependencies { compile files('libs/yuzhouwan.jar') }

// 加载多个 jar 包
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) }
dependencies { compile fileTree(dir: 'libs', include: ['yuzhouwan.jar']) }

踩过的坑

编译 Spring 报错 No locally installed toolchains match

描述

1
2
3
4
5
6
# 克隆最新的 Spring 代码(当前版本是 v6.0.10)
$ git clone git@github.com:spring-projects/spring-framework.git

# 使用 Gradle 编译 Spring 项目
$ cd spring-framework
$ ./gradlew build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Picked up JAVA_TOOL_OPTIONS:  -Xmx12884m
Downloading https://services.gradle.org/distributions/gradle-8.1.1-bin.zip
...........10%............20%............30%............40%............50%............60%............70%............80%...........90%............100%

Welcome to Gradle 8.1.1!

Here are the highlights of this release:
- Stable configuration cache
- Experimental Kotlin DSL assignment syntax
- Building with Java 20

For more details see https://docs.gradle.org/8.1.1/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':framework-docs:compileJava'.
> Could not resolve all dependencies for configuration ':framework-docs:compileClasspath'.
> Failed to calculate the value of task ':spring-core:compileJava21Java' property 'javaCompiler'.
> No matching toolchains found for requested specification: {languageVersion=21, vendor=any, implementation=vendor-specific}.
> No locally installed toolchains match (see https://docs.gradle.org/8.1.1/userguide/toolchains.html#sec:auto_detection) and toolchain download repositories have not been configured (see https://docs.gradle.org/8.1.1/userguide/toolchains.html#sub:download_repositories).

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/8.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 3m 7s
4 actionable tasks: 4 executed

A build scan was not published as you have not authenticated with server 'ge.spring.io'.
For more information, please see https://gradle.com/help/gradle-authenticating-with-gradle-enterprise.

解决

 将 spring-core.gradle 配置文件中的目标 JDK 版本改成和本地版本一致,这里以 JDK 11 为例

1
$ java -version
1
2
3
4
Picked up JAVA_TOOL_OPTIONS:  -Xmx12884m
openjdk version "11.0.19" 2023-04-18 LTS
OpenJDK Runtime Environment Zulu11.64+19-CA (build 11.0.19+7-LTS)
OpenJDK 64-Bit Server VM Zulu11.64+19-CA (build 11.0.19+7-LTS, mixed mode)
1
$ vim spring-core/spring-core.gradle
1
2
3
4
multiRelease {
// targetVersions 17, 21
targetVersions 11
}
1
$ ./gradlew build -x test
1
2
3
4
5
6
7
8
9
10
11
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/8.1.1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 2m 28s
230 actionable tasks: 169 executed, 57 from cache, 4 up-to-date

A build scan was not published as you have not authenticated with server 'ge.spring.io'.
For more information, please see https://gradle.com/help/gradle-authenticating-with-gradle-enterprise.

资料

Doc

Blog

Github

欢迎加入我们的技术群,一起交流学习

群名称 群号
人工智能(高级)
人工智能(进阶)
大数据
算法
数据库