Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Gradle, is there a way to set JDK to the Eclipse Default?

Tags:

java

gradle

I am integrating my gradle build files into our eclipse development environment which supports multiple JDKs. While most developers have several versions installed, the correct behavior would be to use the "default JRE" as checked on the System Preferences->Java->Installed JREs page.

Is there a way to have gradle set JAVA_HOME (or "org.gradle.java.home"??) to this? If not, any suggestions on the best way to go about this for such a group of developers? This isn't really a problem for just a single person, it is trying to find a general approach that will scale across the group of us that has me searching!

thanks!

like image 973
JoeG Avatar asked Aug 10 '12 12:08

JoeG


1 Answers

I'm still not completely sure what you are asking for, but here's a few different takes on it.

  1. If what you want to do is have your code (in Gradle and Eclipse) compile so that the bytecode is compatible with a specific version of Java, use something like this. This does not change the version of Java that either Gradle or Eclipse uses during compilation, just makes the end result "bytecode compatible" with the version you specify. The settings that Luis mentions default to the values set at the more general Java plugin level.

    sourceCompatibility = '1.6' //or '1.5' or '1.7', etc.
    
  2. By default, the Gradle Eclipse plugin will generate the following entry in your .classpath file. I believe this always points to the default you specify in Eclipse, but I may be wrong.

    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
    

    If you want to change what that container is, pick the one you are looking for in Eclipse and then look in the .classpath file for the correct container value. Then you can specify it in the build file:

    eclipse.classpath.containers 'whatever the container value is'
    
  3. However if what you want is to be able to change the JAVA_HOME that Gradle runs with to match the default chosen in Eclipse, I think you'll have a tough time. I'm not sure if there's a easy place to find that value programmatically. You could probably set it up from the opposite direction though. Have the developers set JAVA_HOME to match what their Eclipse default is. Then they can reference the JAVA_HOME environment variable in the Eclipse config for their JRE.

like image 103
ajoberstar Avatar answered Sep 29 '22 11:09

ajoberstar