9

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!

JoeG
  • 7,191
  • 10
  • 60
  • 105
  • So are you trying to have Gradle execute its build with the default JRE chosen in Eclipse? Or are you trying to configure Gradle to generate Eclipse projects that will use the default JRE selected in Eclipse? – ajoberstar Aug 10 '12 at 22:01
  • I think the latter of what you write. For example, many users here have the eclipse launched via java 1.6. Some of the gradle projects have a source compatibility of 1.7. The 1.7 java version IS installed in eclipse. However, when you right click a project and say "runAs-> Gradle Build" it gives an error "[ant:javac] javac: invalid target release: 1.7" – JoeG Aug 15 '12 at 12:40

3 Answers3

13

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.

ajoberstar
  • 2,575
  • 19
  • 21
  • Number 3 is exactly what I was trying to ask about and what I need/want to be able to do. A lot of folks here use Macs for dev and switching them from the installed 1.6 to 1.7 is apparently an issue. I haven't looked into that. However, installing 1.7 as the default JRE works fine - except for this Gradle snafu. Thanks for the help! – JoeG Aug 17 '12 at 11:14
4

if you have different versions of jdk it's easy

apply plugin: 'java'
apply plugin: 'eclipse'

eclipse {
  jdt {
    //if you want to alter the java versions (by default they are configured with gradle java plugin settings):
    sourceCompatibility = 1.6
    targetCompatibility = 1.5

now if you're using different JVM 1.6.XX and want to target to specific one I am not sure

  • Ok - I didn't know this, but not sure that helps my situation. I need to control the version of Java that Gradle will execute with when you have Gradle integrated into Eclipse. So rather then using the version of java that eclipse was started with (say 1.6), I need it to use one of the installed JREs (for instance, 1.7), otherwise I get errors. – JoeG Aug 15 '12 at 12:52
4

Try installing the "Gradle IDE Pack" from Eclipse Marketplace. I'm on Eclipse Luna and can see this option under the "Preferences > Gradle > Arguments" menu:

Eclipse Gradle Plugin Arguments dialog

You can verify that your chosen JRE has been used by using the 'file' command, as stated in this answer.

Community
  • 1
  • 1
Adil B
  • 14,635
  • 11
  • 60
  • 78