如何检查在Gradle构建过程中使用了哪个已安装的JDK

如何检查在Gradle构建过程中使用了哪个已安装的JDK

本文介绍了如何检查在Gradle构建过程中使用了哪个已安装的JDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的gradle -v输出(在使用包装的项目中):

This was my output of gradle -v (in a project using the wrapper):

$ ./gradlew -v

------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------

Build time:   2018-11-26 11:48:43 UTC
Revision:     7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987

Kotlin DSL:   1.0.4
Kotlin:       1.3.10
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.1 (Oracle Corporation 11.0.1+13-LTS)
OS:           Linux 3.10.0-862.11.6.el7.x86_64 amd64

尤其要看这一行:

JVM: 11.0.1 (Oracle Corporation 11.0.1+13-LTS)

我希望切换到OpenJDK11.因此,请选择它,如下所示:

I was wishing to switch to OpenJDK 11. So select it as you can see below:

# alternatives --config java

There are 4 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
*  1           /usr/java/jdk-11.0.1/bin/java
 + 2           /usr/local/jdk-11.0.1/bin/java
   3           /usr/java/jre1.8.0_191-i586/bin/java
   4           /usr/java/jdk1.8.0_191-amd64/jre/bin/java

Enter to keep the current selection[+], or type selection number: 2

# java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

但是gradle -v输出没有区别.因此,我在网上搜索并找到了一些方法(请参见此处):

But there is no difference in gradle -v output. So I searched the web and find some ways (see here):

  1. 编辑gradle.properties文件

使用-Dorg.gradle.java.home命令行选项

编辑build.gradle文件

我使用了前两种方式.两者都起作用(以测试我切换到JDK 8然后运行build任务.由于Java 8不支持我的代码中的某些新功能,所以该任务失败了).但是 gradle -v的结果仍然保持不变!甚至使用第二种方式:

I used the first two ways. Both worked (to test I switched to JDK 8 and then run build task. The task failed due to some new features in my codes that aren't supported by Java 8). But the result of gradle -v remained unchanged still! Even using the second way:

# ./gradlew -Dorg.gradle.java.home=/usr/java/jdk1.8.0_191-amd64 -v

------------------------------------------------------------
Gradle 5.0
------------------------------------------------------------

Build time:   2018-11-26 11:48:43 UTC
Revision:     7fc6e5abf2fc5fe0824aec8a0f5462664dbcd987

Kotlin DSL:   1.0.4
Kotlin:       1.3.10
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          11.0.1 (Oracle Corporation 11.0.1+13-LTS)
OS:           Linux 3.10.0-862.11.6.el7.x86_64 amd64


所以问题是如何在build过程中检查Gradle使用了哪个JDK版本?


So the question is how to check which JDK version is used by Gradle during build process?

推荐答案

您可以添加一个在执行(Kotlin DSL)时打印所需内容的任务:

You can add a task that prints what you need when executed (Kotlin DSL):

tasks {
    val j by creating {
        doLast {
            println(System.getProperty("java.home"))
        }
    }
}

Groovy DSL:

Groovy DSL:

tasks.register("j") {
    doLast {
        println System.getProperty("java.home")
    }
}

然后执行./gradlew j:

/usr/lib/jvm/java-8-openjdk/jre

为什么gradlew使用另一个JVM?看一下此脚本,您将看到它使用JAVA_HOME变量搜索JVM.因此JAVA_HOME所指向的版本可能与您的PATH版本不同.

Why could gradlew use another JVM? Take a look at this script and you'll see that it uses JAVA_HOME variable to search for JVM. So probably the version from your PATH is not the same, that JAVA_HOME is pointing to.

这篇关于如何检查在Gradle构建过程中使用了哪个已安装的JDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 02:58