IDEA 配置 Gradle 使用指定 JDK 版本

最近尝试使用 Gradle 开始管理项目依赖,替代 Maven。但是在编译的时候,依然按照原来的方式添加。但是在编译的时候,出现下面的错误:

1
2
3
4
5
6
7
8
9
> Configure project :xxxxxxxx
Errors occurred while build effective model from /Users/MoMo/.gradle/caches/modules-2/files-2.1/com.alibaba/druid/1.1.18/83a6ea652284b08567d8553ae0b6f99ec80ea7c3/druid-1.1.18.pom:
'dependencies.dependency.systemPath' for com.alibaba:jconsole:jar must specify an absolute path but is ${env.JAVA_HOME}/lib/jconsole.jar in com.alibaba:druid:1.1.18
'dependencies.dependency.systemPath' for com.alibaba:tools:jar must specify an absolute path but is ${env.JAVA_HOME}/lib/tools.jar in com.alibaba:druid:1.1.18

> Configure project :xxxxxxxx
Errors occurred while build effective model from /Users/MoMo/.gradle/caches/modules-2/files-2.1/com.alibaba/druid/1.1.18/83a6ea652284b08567d8553ae0b6f99ec80ea7c3/druid-1.1.18.pom:
'dependencies.dependency.systemPath' for com.alibaba:jconsole:jar must specify an absolute path but is ${env.JAVA_HOME}/lib/jconsole.jar in com.alibaba:druid:1.1.18
'dependencies.dependency.systemPath' for com.alibaba:tools:jar must specify an absolute path but is ${env.JAVA_HOME}/lib/tools.jar in com.alibaba:druid:1.1.18

大概意思就是 druid 依赖了 jdk 的 jconsole.jar 和 tools.jar,但是在 JDK 11 中又移除了这两个 jar 包:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[I] [ 0 ]:[ 2019-10-26 10:10:03 ] MoMo in /L/J/JavaVirtualMachines
» ls -al
total 0
drwxr-xr-x 5 root wheel 160 Oct 17 18:43 ./
drwxr-xr-x 4 root wheel 128 Sep 30 04:20 ../
drwxr-xr-x 3 root wheel 96 Sep 3 00:15 jdk-11.0.4.jdk/
drwxr-xr-x 3 root wheel 96 Jun 17 2017 jdk1.7.0_80.jdk/
drwxr-xr-x 3 root wheel 96 Jan 30 2018 jdk1.8.0_161.jdk/

[I] [ 0 ]:[ 2019-10-26 10:10:23 ] MoMo in /L/J/JavaVirtualMachines
» find . -name "tools.jar"
./jdk1.7.0_80.jdk/Contents/Home/lib/tools.jar
./jdk1.8.0_161.jdk/Contents/Home/lib/tools.jar

[I] [ 0 ]:[ 2019-10-26 10:10:27 ] MoMo in /L/J/JavaVirtualMachines
» find . -name "jconsole.jar"
./jdk1.7.0_80.jdk/Contents/Home/lib/jconsole.jar
./jdk1.8.0_161.jdk/Contents/Home/lib/jconsole.jar

解决办法最好是等 druid 兼容 JDK11,不过 issue 还在,一直没有人处理。
https://github.com/alibaba/druid/issues/3060

当前我选择切换回 jdk8 进行开发,如果使用命令行,则添加参数 -Dorg.gradle.java.home 即可:

1
/gradlew -Dorg.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home   DownloadSources

如果在 IDEA,配置 Gradle 使用指定的 JDK 版本即可:
idea-gradle-jdk-version

注意主要在 Gradle projects 选择你的项目,右边选择版本即可。

执行 task DownloadSources 时,说不能在 offline 模式下下载 jar。

1
Could not download spring-boot-sources.jar (org.springframework.boot:spring-boot:2.1.6.RELEASE): No cached version available for offline mode

关掉这个开关即可。
idea-gradle-offline

Just for my love !!