diff --git a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java index c9a5455f3..9cf914ddc 100644 --- a/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java +++ b/src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java @@ -16,9 +16,15 @@ package com.diffplug.gradle.oomph; import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; import java.util.Set; import org.gradle.api.Action; +import org.gradle.api.JavaVersion; + +import com.diffplug.common.collect.ImmutableList; /** * Adding the JDT convention to your project @@ -33,17 +39,22 @@ * ```gradle * oomphIde { * jdt { + * * installedJre { * version = '1.6.0_45' * installedLocation = new File('C:/jdk1.6.0_45') * markDefault = true // or false * executionEnvironments = ['JavaSE-1.6'] // any execution environments can be specified here. * } + * compilerComplianceLevel('1.6') + * classpathVariable('myClasspath', '/var/lib/repo') * } * } * ``` */ public class ConventionJdt extends OomphConvention { + final static String JDT_CORE_PREFS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs"; + ConventionJdt(OomphIdeExtension extension) { super(extension); requireIUs(IUs.IDE, IUs.JDT, IUs.ERROR_LOG); @@ -59,6 +70,29 @@ public void installedJre(Action action) { installedJres.add(instance); } + /** Sets default compliance level */ + public void compilerComplianceLevel(String compilerComplianceLevel) { + List JDT_COMPLIANCE_PROPS = ImmutableList.of( + "org.eclipse.jdt.core.compiler.codegen.targetPlatform", + "org.eclipse.jdt.core.compiler.compliance", + "org.eclipse.jdt.core.compiler.source"); + extension.workspaceProp(JDT_CORE_PREFS, props -> { + JDT_COMPLIANCE_PROPS.forEach(p -> props.put(p, compilerComplianceLevel.toString())); + //Use default compliance settings. + props.put("org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode", "enabled"); + props.put("org.eclipse.jdt.core.compiler.problem.assertIdentifier", "error"); + props.put("org.eclipse.jdt.core.compiler.problem.enumIdentifier", "error"); + }); + } + + /** Adds a compiler class path variable. */ + public void classpathVariable(String name, String value) { + String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s"; + extension.workspaceProp(JDT_CORE_PREFS, props -> { + props.put(String.format(JDT_CLASSPATH_VAR_FMT, name), value); + }); + } + @Override public void close() { // add installed jres diff --git a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java index 78952e70e..1349f2c3c 100644 --- a/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java +++ b/src/main/java/com/diffplug/gradle/oomph/OomphIdeExtension.java @@ -213,6 +213,16 @@ public void addSetupActionLazy(Action> lazyInternalSetupAction setupActions.addLazyAction(lazyInternalSetupAction); } + /** Links the given target into the workspace with the given name, see [eclipse manual](http://help.eclipse.org/neon/index.jsp?topic=%2Forg.eclipse.platform.doc.user%2Fconcepts%2Fconcepts-13.htm). */ + public void linkedResource(String linkName, Object linkTarget) { + final String CORE_RES_PREFS_FILE = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.core.resources.prefs"; + final String WS_PATHVAR_FMT = "pathvariable.%s"; + workspaceProp(CORE_RES_PREFS_FILE, props -> { + //Eclipse cannot handle backslashes in this value. It expects path separators to be '/' + props.put(String.format(WS_PATHVAR_FMT, linkName), project.file(linkTarget).getAbsolutePath().replace("\\", "/")); + }); + } + //////////////// // ideSetupP2 // ////////////////