15
15
*/
16
16
package com .diffplug .gradle .oomph ;
17
17
18
+ import java .util .Arrays ;
18
19
import java .util .HashSet ;
20
+ import java .util .LinkedHashMap ;
21
+ import java .util .List ;
22
+ import java .util .Map ;
19
23
import java .util .Set ;
20
24
21
25
import org .gradle .api .Action ;
26
+ import org .gradle .api .JavaVersion ;
22
27
23
28
/**
24
29
* Adding the JDT convention to your project
33
38
* ```gradle
34
39
* oomphIde {
35
40
* jdt {
41
+ *
36
42
* installedJre {
37
43
* version = '1.6.0_45'
38
44
* installedLocation = new File('C:/jdk1.6.0_45')
39
45
* markDefault = true // or false
40
46
* executionEnvironments = ['JavaSE-1.6'] // any execution environments can be specified here.
41
47
* }
48
+ * compilerComplianceLevel('1.6')
49
+ * classpathVariable('myClasspath', '/var/lib/repo')
42
50
* }
43
51
* }
44
52
* ```
45
53
*/
46
54
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
+
47
59
ConventionJdt (OomphIdeExtension extension ) {
48
60
super (extension );
49
61
requireIUs (IUs .IDE , IUs .JDT , IUs .ERROR_LOG );
50
62
setPerspectiveOver (Perspectives .JAVA , Perspectives .RESOURCES );
51
63
}
52
64
53
65
final Set <InstalledJre > installedJres = new HashSet <>();
66
+ final Map <String , String > classpathVariables = new LinkedHashMap <>();
67
+ private JavaVersion compilerComplianceLevel ;
54
68
55
69
/** Adds an installed JRE with the given content. */
56
70
public void installedJre (Action <InstalledJre > action ) {
@@ -59,11 +73,59 @@ public void installedJre(Action<InstalledJre> action) {
59
73
installedJres .add (instance );
60
74
}
61
75
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
+
62
86
@ Override
63
87
public void close () {
64
88
// add installed jres
65
89
if (!installedJres .isEmpty ()) {
66
90
extension .addSetupAction (new InstalledJreAdder (installedJres ));
67
91
}
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
+ }
68
130
}
69
131
}
0 commit comments