How to set JDK version in Gradle project WITHOUT explicit JDK path?

巧了我就是萌 提交于 2019-12-24 01:48:08

问题


When we set path to java libraries, we just write something like this:

compile('ch.qos.logback:logback-classic:1.0.13')

There is no any path information here, I don't bother if JAR will be located on drive C: on drive D: or in Linux root /

Let it remains so.

And now I want to set JDK version. I wish to be able to change this version easily and portable without setting any paths, relative to my machine.

Is this possible?

UPDATE

Apparently, even Ant can this: https://stackoverflow.com/a/11551770/258483


回答1:


Java has no concept of libraries or versions. In Java, there is no standard way to tell the JVM that you are using version 3.0.5 of Hibernate, and there is no standard way to say that foo-1.0.jar depends on bar-2.0.jar. This has led to external solutions often based on build tools.

This is taken from Gradle's dependency management Doc.

So this is where gradle dependency management is done, by dependency they mean Project's dependencies on external packages. Something called Separation of Concern. If you want to declare which Jdk your project should be using (jdk here is the compiler (the javac program) which is doing something thats logically different than what Project's dependencies do) the dependency block is not the right place to put it in.

As for how to declare the your custom jdk path to use:

Two ways

  1. In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory The gradle.properties can be defined at project level too, see gradle.org/docs/current/userguide/build_environment.html

  2. In your gradle.build

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


来源:https://stackoverflow.com/questions/35911416/how-to-set-jdk-version-in-gradle-project-without-explicit-jdk-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!