Choosing the correct JRE version in Gradle with Eclipse

后端 未结 2 1628
灰色年华
灰色年华 2021-01-18 19:17

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

2条回答
  •  孤城傲影
    2021-01-18 19:59

    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.

提交回复
热议问题