Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing the correct JRE version in Gradle with Eclipse

I'm using Gradle with the Eclipse plugin to generate project files for my project, but I can't get it to put the correct JRE version in .classpath. I can add a JRE container, but I can't figure out how to remove the default one - and since this project is shared between developers, who might have varying defaults set in Eclipse, I want to control this manually.

The way I think this should work, is like so:

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

sourceCompatibility = 1.6

Since targetCompatibility is the same as sourceCompatibility, I expect this setup to go to the Eclipse settings, find a JRE that matches the source version (and yes, there is one on my machine - both a JRE installation and a separate JDK installation) and go with it.

Instead, however, it picks the default, which on my machine happens to be Java 7.

I tried adding some stuff to the Eclipse configuration:

eclipse {
    jdt {
        sourceCompatibility = 1.6 // tried with and without this
    }
    classpath {
        // tried various ways to remove the old entry, among them:
        file.beforeMerged { p -> p.entries.clear() }

        // and then I add the "correct" one
        containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_45'
    }
}

Doing things like this I end up with two JRE containers in .classpath:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="output" path="bin"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" exported="true"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_45" exported="true"/>
</classpath>

How do I tell gradle that I only want a JRE 1.6 container, and not the default one too?


Some constraints on what I'm after:

  • The default setting in Eclipse should be irrelevant
  • Preferrably, I want the script to look up the container - in the above script, the string defining the container to add is user-dependent. I would like it much better to look among "installed JREs" for a version matching that of sourceConfiguration - I'm OK with throwing an error if no such JRE is installed in Eclipse.
like image 719
Tomas Aschan Avatar asked Jun 14 '13 14:06

Tomas Aschan


People also ask

What version of java does Gradle use?

Gradle can only run on Java version 8 or higher. Gradle still supports compiling, testing, generating Javadoc and executing applications for Java 6 and Java 7.

How do I set Gradle preferences in Eclipse?

It is important to add the GRADLE_USER_HOME variable in Eclipse: Window->Preferences->Java->Build Path->Classpath Variable. Set it to the path of the ~/. gradle folder in your home directory (e.g. /home/<user_name>/. gradle/ (Unix) or C:\Users\<user_name>\.

How do I mention java version in build Gradle?

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.

How do I find my Gradle version in Eclipse?

1 Answer. Show activity on this post. Select 'Help > About Eclipse' (on Macs this is 'Eclipse > About Eclipse'). Click the 'Installation Details' button to display the installation details dialog.

How do I select a JDK in Gradle?

Set the JDK versionOpen your project in Android Studio and select File > Settings... > Build, Execution, Deployment > Build Tools > Gradle (Android Studio > Preferences... > Build, Execution, Deployment > Build Tools > Gradle on a Mac). Under Gradle JDK, choose the Embedded JDK option.


2 Answers

I've ended up solving this in a slightly more manual way than I wanted - but at least it works.

In order to separate the settings from the implementation, each developer has a gradle.properties file which is not checked into version control. This file contains the following information (on my workstation):

javaVersion=1.6
javaPath=C:/Program/Java/jdk1.6.0_45
jdkName=jdk1.6.0_45

In the build script, i then do the following to get all the configuration correct:

// Set sourceCompatibility
if (project.hasProperty('javaVersion')) {
    project.sourceCompatibility = project.javaVersion
}

// Set bootClasspath - but wait until after evaluation, to have all tasks defined
project.afterEvaluate {
    if (project.hasProperty('javaPath')) {
        project.tasks.withType(AbstractCompile, {
            it.options.bootClasspath = "${project.javaPath}/jre/lib/rt.jar"
        })
    }
}

// Configure Eclipse .classpath
project.eclipse.classpath.file.whenMerged { Classpath cp ->
    if (project.hasProperty('jdkName') {
        cp.entries.findAll { it.path.contains('JRE_CONTAINER') }.each {
            it.path += "/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/$project.jdkName"
        }
    }
}

So far I've used it in a couple of projects and it's worked, so I guess it's at least quite portable - but it might be necessary to make slight modifications to make it work for others.

like image 165
Tomas Aschan Avatar answered Sep 23 '22 22:09

Tomas Aschan


I found that the proposed solution causes duplicate entries on subsequent 'gradle eclipse' executions. Borrowing some code from Specifiy JRE Container with gradle eclipse plugin, I came up with the following which seems to work:

project.afterEvaluate {
  // use jre lib matching version used by project, not the workspace default
  if (project.sourceCompatibility != null) {
    def target = project.sourceCompatibility.toString()
    def containerPrefix = "org.eclipse.jdt.launching.JRE_CONTAINER"
    def containerSuffix
    if (target =~ /1.[4-5]/) {
      containerSuffix = '/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-' + target
    } else if (target =~ /1.[6-8]/) {
      containerSuffix = '/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-' + target
    }
    if (containerSuffix != null) {
      project.eclipse.classpath {
        containers.removeAll { it.startsWith(containerPrefix) }
        containers.add(containerPrefix + containerSuffix)
      }
    }
  }
}
like image 24
John Vasileff Avatar answered Sep 23 '22 22:09

John Vasileff