Skip to content

Commit e4cd898

Browse files
committed
Added compilerComplianceLevel method, and classpathVariables method.
1 parent ef655df commit e4cd898

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/main/java/com/diffplug/gradle/oomph/ConventionJdt.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
*/
1616
package com.diffplug.gradle.oomph;
1717

18+
import java.util.Arrays;
1819
import java.util.HashSet;
20+
import java.util.LinkedHashMap;
21+
import java.util.List;
22+
import java.util.Map;
1923
import java.util.Set;
2024

2125
import org.gradle.api.Action;
26+
import org.gradle.api.JavaVersion;
2227

2328
/**
2429
* Adding the JDT convention to your project
@@ -33,24 +38,33 @@
3338
* ```gradle
3439
* oomphIde {
3540
* jdt {
41+
*
3642
* installedJre {
3743
* version = '1.6.0_45'
3844
* installedLocation = new File('C:/jdk1.6.0_45')
3945
* markDefault = true // or false
4046
* executionEnvironments = ['JavaSE-1.6'] // any execution environments can be specified here.
4147
* }
48+
* compilerComplianceLevel('1.6')
49+
* classpathVariable('myClasspath', '/var/lib/repo')
4250
* }
4351
* }
4452
* ```
4553
*/
4654
public class ConventionJdt extends OomphConvention {
55+
public final static String JDT_CORE_PREFS = ".metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.core.prefs";
56+
public final static List<String> JDT_COMPLIANCE_PROPS = Arrays.asList("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "org.eclipse.jdt.core.compiler.compliance", "org.eclipse.jdt.core.compiler.source");
57+
public final static String JDT_CLASSPATH_VAR_FMT = "org.eclipse.jdt.core.classpathVariable.%s";
58+
4759
ConventionJdt(OomphIdeExtension extension) {
4860
super(extension);
4961
requireIUs(IUs.IDE, IUs.JDT, IUs.ERROR_LOG);
5062
setPerspectiveOver(Perspectives.JAVA, Perspectives.RESOURCES);
5163
}
5264

5365
final Set<InstalledJre> installedJres = new HashSet<>();
66+
final Map<String, String> classpathVariables = new LinkedHashMap<>();
67+
private JavaVersion compilerComplianceLevel;
5468

5569
/** Adds an installed JRE with the given content. */
5670
public void installedJre(Action<InstalledJre> action) {
@@ -59,11 +73,59 @@ public void installedJre(Action<InstalledJre> action) {
5973
installedJres.add(instance);
6074
}
6175

76+
/** Sets default compliance level */
77+
public void compilerComplianceLevel(String compilerComplianceLevel) {
78+
this.compilerComplianceLevel = JavaVersion.toVersion(compilerComplianceLevel);
79+
}
80+
81+
/** Adds a compiler class path variable. */
82+
public void classpathVariable(String name, String value) {
83+
classpathVariables.put(name, value);
84+
}
85+
6286
@Override
6387
public void close() {
6488
// add installed jres
6589
if (!installedJres.isEmpty()) {
6690
extension.addSetupAction(new InstalledJreAdder(installedJres));
6791
}
92+
if (!classpathVariables.isEmpty()) {
93+
extension.workspaceProp(JDT_CORE_PREFS, new JavaClasspathVariableAction(classpathVariables));
94+
}
95+
if (compilerComplianceLevel != null) {
96+
extension.workspaceProp(JDT_CORE_PREFS, new JavaComplianceAction(compilerComplianceLevel));
97+
}
98+
}
99+
100+
static class JavaComplianceAction implements Action<Map<String, String>> {
101+
private final JavaVersion complianceLevel;
102+
103+
public JavaComplianceAction(JavaVersion complianceLevel) {
104+
super();
105+
this.complianceLevel = complianceLevel;
106+
}
107+
108+
@Override
109+
public void execute(Map<String, String> props) {
110+
JDT_COMPLIANCE_PROPS.forEach(p -> props.put(p, complianceLevel.toString()));
111+
//Use default compliance settings.
112+
props.put("org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode", "enabled");
113+
props.put("org.eclipse.jdt.core.compiler.problem.assertIdentifier", "error");
114+
props.put("org.eclipse.jdt.core.compiler.problem.enumIdentifier", "error");
115+
}
116+
}
117+
118+
static class JavaClasspathVariableAction implements Action<Map<String, String>> {
119+
private final Map<String, String> classpathVariables;
120+
121+
public JavaClasspathVariableAction(Map<String, String> classpathVariables) {
122+
super();
123+
this.classpathVariables = classpathVariables;
124+
}
125+
126+
@Override
127+
public void execute(Map<String, String> props) {
128+
classpathVariables.forEach((key, value) -> props.put(String.format(JDT_CLASSPATH_VAR_FMT, key), value));
129+
}
68130
}
69131
}

0 commit comments

Comments
 (0)