Java 如何告诉 Gradle 使用特定的 JDK 版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18487406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:05:30  来源:igfitidea点击:

How do I tell Gradle to use specific JDK version?

javagradlebuild.gradle

提问by bully

I can't figure out to get this working.

我不知道让这个工作。

Scenario:

设想:

  • I have an application built with gradle
  • The application uses JavaFX
  • 我有一个用 gradle 构建的应用程序
  • 应用程序使用 JavaFX

What I want

我想要的是

  • Use a variable (defined per developer machine) which points to an installation of a JDK which will be used for building the whole application / tests / ...
  • 使用一个变量(按开发者机器定义),它指向一个 JDK 的安装,该 JDK 将用于构建整个应用程序/测试/...

I thought about having the gradle.propertiesfile, defining the variable. Something like

我想过拥有gradle.properties文件,定义变量。就像是

JAVA_HOME_FOR_MY_PROJECT=<path to my desired JDK>

What I don't want

我不想要的

  • point JAVA_HOMEto the desired JDK
  • 指向JAVA_HOME所需的JDK

I could live with many suggestions:

我可以接受很多建议:

  • a solution that defines a system environment variable which I'm able to check in my build.gradle script
  • a variable defined in gradle.properties
  • overriding the JAVA_HOME variable only for the build context (something like use JAVA_HOME=<my special JDK path defined somewhere else defined>)
  • something else I didn't think about
  • 一个定义系统环境变量的解决方案,我可以在我的 build.gradle 脚本中检查它
  • gradle.properties 中定义的变量
  • 仅针对构建上下文覆盖 JAVA_HOME 变量(类似于use JAVA_HOME=<my special JDK path defined somewhere else defined>
  • 我没想到的其他事情

Question:

题:

  • How to wire a variable (how ever defined, as variable in the gradle.properties, system environment variable, ...) to the build process?
  • 如何将变量(如何定义,作为gradle.properties, 系统环境变量中的变量,...)连接到构建过程?

I have more than one JDK7 available and need to point to a special version (minimum JDK_u version).

我有多个 JDK7 可用,需要指向一个特殊版本(最低 JDK_u 版本)。

Any answer is appreciated and I'm thankful for every hint to the right direction.

任何答案都值得赞赏,我感谢每一个正确方向的提示。

采纳答案by First Zero

Two ways

两种方式

  1. In gradle.propertiesin the .gradledirectory in your HOME_DIRECTORYset org.gradle.java.home=/path_to_jdk_directory
  1. 在您设置gradle.properties.gradle目录中HOME_DIRECTORYorg.gradle.java.home=/path_to_jdk_directory

or:

或者:

  1. In your build.gradle

    compileJava.options.fork = true
    compileJava.options.forkOptions.executable = /path_to_javac
    
  1. 在你的 build.gradle

    compileJava.options.fork = true
    compileJava.options.forkOptions.executable = /path_to_javac
    

回答by Paul Verest

As seen in Gradle (Eclipse plugin)

Gradle(Eclipse 插件)中所见

http://www.gradle.org/get-started

http://www.gradle.org/get-started

Gradle uses whichever JDK it finds in your path (to check, use java -version). Alternatively, you can set the JAVA_HOME environment variable to point to the install directory of the desired JDK.

Gradle 使用它在您的路径中找到的任何 JDK(要检查,请使用 java -version)。或者,您可以将 JAVA_HOME 环境变量设置为指向所需 JDK 的安装目录。



If you are using this Eclipse plugin or Enide Studio 2014, alternative JAVA_HOME to use (set in Preferences) will be in version 0.15, see http://www.nodeclipse.org/history

如果您使用此 Eclipse 插件或Enide Studio 2014,要使用的替代 JAVA_HOME(在首选项中设置)将在 0.15 版中,请参阅http://www.nodeclipse.org/history

回答by mirmdasif

If you add JDK_PATH in gradle.propertiesyour build become dependent on on that particular path. Instead Run gradle taskwith following command line parametemer

如果您在gradle.properties 中添加 JDK_PATH,您的构建将依赖于该特定路径。而是使用以下命令行参数运行gradle 任务

gradle build -Dorg.gradle.java.home=/JDK_PATH

This way your build is not dependent on some concrete path.

通过这种方式,您的构建不依赖于某些具体路径。

回答by sylwano

If you are using linux and gradle wrapper you can use following solution.

如果您使用的是 linux 和 gradle 包装器,则可以使用以下解决方案。

Add path to local.properties file:

将路径添加到 local.properties 文件:

javaHome=<path to JDK>

Add to your gradlew script file:

添加到您的 gradlew 脚本文件:

DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source $DIR/local.properties 2>/dev/null

if ! [ -z "$javaHome" ]
then
  JAVA_HOME=$javaHome
fi

In this solution, each developer can set his own JDK path. File local.propertiesshouldn't be included in version control system.

在这个方案中,每个开发者都可以设置自己的JDK路径。文件local.properties不应包含在版本控制系统中。

回答by Manoj

I added this line in my GRADLE_HOME/bin/gradle file - export JAVA_HOME=/path/to/java/version

我在我的 GRADLE_HOME/bin/gradle 文件中添加了这一行 - export JAVA_HOME=/path/to/java/version

回答by Geert

To people ending up here when searching for the Gradle equivalent of the Maven property maven.compiler.source(or <source>1.8</source>):

对于在搜索与 Maven 属性maven.compiler.source(或<source>1.8</source>)等效的 Gradle 时到达此处的人们:

In build.gradle you can achieve this with

在 build.gradle 中,您可以使用

apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8

See the Gradle documentation on this.

请参阅关于此Gradle 文档

回答by Do Nhu Vy

I am using Gradle 4.2 . Default JDK is Java 9. In early day of Java 9, Gradle 4.2 run on JDK 8 correctly (not JDK 9).

我正在使用 Gradle 4.2 。默认 JDK 是 Java 9。在 Java 9 的早期,Gradle 4.2 在 JDK 8(不是 JDK 9)上正确运行。

I set JDK manually like this, in file %GRADLE_HOME%\bin\gradle.bat:

我像这样手动设置 JDK,在文件中%GRADLE_HOME%\bin\gradle.bat

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem  Gradle startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%..

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

@rem VyDN-start.
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_144\
@rem VyDN-end.

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%


@rem VyDN-start.
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_144\
@rem VyDN-end.


set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\lib\gradle-launcher-4.2.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.launcher.GradleMain %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if  not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega

回答by Kiran

There is one more option to follow. In your gradle tasks, you can set your desired jdk path. (I know this is a while since the question was posted. This answer can help someone.)

还有一个选项可以遵循。在你的 gradle 任务中,你可以设置你想要的 jdk 路径。(我知道问题发布已经有一段时间了。这个答案可以帮助某人。)

Right click on the deploy or any other task and select "Open Gradle Run Configuration..."

右键单击部署或任何其他任务,然后选择“打开 Gradle 运行配置...”

enter image description here

在此处输入图片说明

Then navigate to "Java Home" and paste your desired java path.

然后导航到“Java Home”并粘贴所需的Java路径。

enter image description here

在此处输入图片说明

Please note that, bin will be added by the gradle task itself. So don't add the "bin" to the path.

请注意,bin 将由 gradle 任务本身添加。所以不要将“bin”添加到路径中。

回答by Vittal Pai

If you are executing using gradle wrapper, you can run the command with JDK path like following

如果您使用 gradle 包装器执行,则可以使用 JDK 路径运行命令,如下所示

./gradlew -Dorg.gradle.java.home=/jdk_path_directory

./gradlew -Dorg.gradle.java.home=/jdk_path_directory

回答by Remo Meier

there is a Gradle plugin that download/bootstraps a JDK automatically:

有一个 Gradle 插件可以自动下载/引导 JDK:

https://plugins.gradle.org/plugin/com.github.rmee.jdk-bootstrap

https://plugins.gradle.org/plugin/com.github.rmee.jdk-bootstrap

No IDE integration yet and a decent shell required on Windows.

还没有 IDE 集成,并且在 Windows 上需要一个像样的外壳。