2

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

Community
  • 1
  • 1
Dims
  • 47,675
  • 117
  • 331
  • 600

1 Answers1

-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
    
Bhargav
  • 8,118
  • 6
  • 40
  • 63
  • This way I can't share project with other developers, for example, share between Windows, Linux and Mac, since JDK paths are completely different on three platforms. So, this way is not applicable. – Dims Mar 12 '16 at 07:09
  • JDK is not just a compiler, it contains plenty of JARs, which are also have versions, sometimes incompatible. – Dims Mar 12 '16 at 07:10
  • Also I can propose very simple way to overcome your yellow cite: just configure JDK at some machine-level config file. This way developers could configure several JDKs at their machine level, while retail no path dependent information at project level. Is this possible in Gradle? – Dims Mar 12 '16 at 07:13
  • Also changing JDK globally at user level is not applicable since it will remove possibility to compile different projects with different JDKs – Dims Mar 12 '16 at 07:16
  • I think for gradle scripts to even start executing most of its functionalities (that are coded in java) it will need the jdk to compile and the jre to run, so no matter what gradle will need to know the path to the jdk folder/dir – Bhargav Mar 12 '16 at 12:18