Choosing the correct JRE version in Gradle with Eclipse

后端 未结 2 1626
灰色年华
灰色年华 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:55

    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)
          }
        }
      }
    }
    

提交回复
热议问题