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

时间:2021-04-22 07:07:55

I can't figure out to get this working.

我搞不懂怎么让它工作。

Scenario:

场景:

  • I have an application built with gradle
  • 我有一个用gradle构建的应用
  • The application uses JavaFX
  • 应用程序使用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的安装,该安装将用于构建整个应用程序/测试/…

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

我想过要考等级。属性文件,定义变量。类似的

JAVA_HOME_FOR_MY_PROJECT=<path to my desired JDK>

What I don't want

我不想要什么

  • point JAVA_HOME to 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
  • 一个定义系统环境变量的解决方案,我可以在我的构建中检查它。gradle脚本
  • a variable defined in gradle.properties
  • 在level .properties中定义的变量
  • overriding the JAVA_HOME variable only for the build context (something like use JAVA_HOME=<my special JDK path defined somewhere else defined>)
  • 仅为构建上下文重写JAVA_HOME变量(类似于使用JAVA_HOME= <我在其他地方定义的特定jdk路径> )
  • something else I didn't think about
  • 还有一些我没想过的事。

Question:

问题:

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

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.

任何答案都是感激的,我感谢每一个指向正确方向的提示。

8 个解决方案

#1


147  

Two ways

两种方式

  1. In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory

    在它。home=/path_to_jdk_directory中的.gradle目录中的属性

  2. In your build.gradle

    在你build.gradle

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

#2


56  

If you add JDK_PATH in gradle.properties your build become dependent on on that particular path. Instead Run gradle task with following command line parametemer

如果在第一级中添加JDK_PATH。您的构建依赖于该特定路径的属性。相反,使用以下命令行参数设置来运行渐变任务

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

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

这样,您的构建就不依赖于某些具体的路径。

#3


19  

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(或1.8)的level等价物的人(或1.8):

In build.gradle you can achieve this with

在构建。葛莱蒂:你可以做到这一点

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

See the Gradle documentation on this.

请参阅这方面的渐变文档。

#4


2  

As seen in Gradle (Eclipse plugin)

正如在Gradle中看到的(Eclipse插件)

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

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版本)。或者,您可以将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,可以在版本0.15中使用替代的JAVA_HOME(设置参数),请参见http://www.nodeclipse.org/history

#5


2  

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

如果您正在使用linux和gradle包装,您可以使用以下解决方案。

Add path to local.properties file:

路径添加到本地。属性文件:

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.properties shouldn't be included in version control system.

在这个解决方案中,每个开发人员都可以设置自己的JDK路径。文件的地方。属性不应该包含在版本控制系统中。

#6


2  

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

我在GRADLE_HOME/bin/gradle文件中添加了这一行——导出JAVA_HOME=/path/to/java/版本。

#7


2  

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.)

还有一个选择。在您的渐变任务中,您可以设置所需的jdk路径。(我知道这个问题发布已经有一段时间了。这个答案可以帮助某人。

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

右键单击部署或任何其他任务,选择“打开渐变运行配置…”

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

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

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

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

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

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

#8


1  

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).

我正在使用第4.2级。默认JDK是Java 9。在Java 9的早期,第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

#1


147  

Two ways

两种方式

  1. In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory

    在它。home=/path_to_jdk_directory中的.gradle目录中的属性

  2. In your build.gradle

    在你build.gradle

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

#2


56  

If you add JDK_PATH in gradle.properties your build become dependent on on that particular path. Instead Run gradle task with following command line parametemer

如果在第一级中添加JDK_PATH。您的构建依赖于该特定路径的属性。相反,使用以下命令行参数设置来运行渐变任务

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

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

这样,您的构建就不依赖于某些具体的路径。

#3


19  

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(或1.8)的level等价物的人(或1.8):

In build.gradle you can achieve this with

在构建。葛莱蒂:你可以做到这一点

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

See the Gradle documentation on this.

请参阅这方面的渐变文档。

#4


2  

As seen in Gradle (Eclipse plugin)

正如在Gradle中看到的(Eclipse插件)

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

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版本)。或者,您可以将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,可以在版本0.15中使用替代的JAVA_HOME(设置参数),请参见http://www.nodeclipse.org/history

#5


2  

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

如果您正在使用linux和gradle包装,您可以使用以下解决方案。

Add path to local.properties file:

路径添加到本地。属性文件:

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.properties shouldn't be included in version control system.

在这个解决方案中,每个开发人员都可以设置自己的JDK路径。文件的地方。属性不应该包含在版本控制系统中。

#6


2  

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

我在GRADLE_HOME/bin/gradle文件中添加了这一行——导出JAVA_HOME=/path/to/java/版本。

#7


2  

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.)

还有一个选择。在您的渐变任务中,您可以设置所需的jdk路径。(我知道这个问题发布已经有一段时间了。这个答案可以帮助某人。

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

右键单击部署或任何其他任务,选择“打开渐变运行配置…”

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

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

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

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

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

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

#8


1  

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).

我正在使用第4.2级。默认JDK是Java 9。在Java 9的早期,第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