可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I can\'t figure out to get this working.
Scenario:
- I have an application built with gradle
- The application uses 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 / ...
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
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
Question:
- How to wire a variable (how ever defined, as variable in the
gradle.properties
, system environment variable, ...) to the build process?
I have more than one JDK7 available and need to point to a special version (minimum JDK_u version).
Any answer is appreciated and I\'m thankful for every hint to the right direction.
回答1:
Two ways
In gradle.properties
in the .gradle
directory in your HOME_DIRECTORY
set org.gradle.java.home=/path_to_jdk_directory
In your build.gradle
compileJava.options.fork = true
compileJava.options.forkOptions.executable = /path_to_javac
回答2:
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
gradle build -Dorg.gradle.java.home=/JDK_PATH
This way your build is not dependent on some concrete path.
回答3:
To people ending up here when searching for the Gradle equivalent of the Maven property maven.compiler.source
(or <source>1.8</source>
):
In build.gradle you can achieve this with
apply plugin: \'java\'
sourceCompatibility = 1.8
targetCompatibility = 1.8
See the Gradle documentation on this.
回答4:
If you are using linux and gradle wrapper you can use following solution.
Add path to local.properties file:
javaHome=<path to JDK>
Add to your gradlew script file:
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.
回答5:
I added this line in my GRADLE_HOME/bin/gradle file -
export JAVA_HOME=/path/to/java/version
回答6:
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.)
Right click on the deploy or any other task and select \"Open Gradle Run Configuration...\"
Then navigate to \"Java Home\" and paste your desired java path.
Please note that, bin will be added by the gradle task itself. So don\'t add the \"bin\" to the path.
回答7:
As seen in Gradle (Eclipse plugin)
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.
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
回答8:
If you are executing using gradle wrapper, you can run the command with JDK path like following
./gradlew -Dorg.gradle.java.home=/jdk_path_directory
回答9:
there is a Gradle plugin that download/bootstraps a JDK automatically:
https://plugins.gradle.org/plugin/com.github.rmee.jdk-bootstrap
No IDE integration yet and a decent shell required on Windows.
回答10:
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).
I set JDK manually like this, in file %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