-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
EclipseModel.java
315 lines (278 loc) · 8.8 KB
/
EclipseModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.plugins.ide.eclipse.model;
import com.google.common.base.Preconditions;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.internal.tasks.DefaultTaskDependency;
import org.gradle.api.internal.tasks.TaskDependencyFactory;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.TaskDependency;
import org.gradle.internal.xml.XmlTransformer;
import org.gradle.plugins.ide.api.XmlFileContentMerger;
import javax.inject.Inject;
import java.io.File;
import java.util.Map;
import static org.gradle.util.internal.ConfigureUtil.configure;
/**
* DSL-friendly model of the Eclipse project information.
* First point of entry for customizing Eclipse project generation.
*
* <pre class='autoTested'>
* plugins {
* id 'java'
* id 'eclipse'
* id 'eclipse-wtp' // for web projects only
* }
*
* eclipse {
* pathVariables 'GRADLE_HOME': file('/best/software/gradle'), 'TOMCAT_HOME': file('../tomcat')
*
* project {
* //see docs for {@link EclipseProject}
* }
*
* classpath {
* //see docs for {@link EclipseClasspath}
* }
*
* wtp {
* //see docs for {@link EclipseWtp}
* }
* }
* </pre>
*
* More examples in docs for {@link EclipseProject}, {@link EclipseClasspath}, {@link EclipseWtp}
*/
public abstract class EclipseModel {
private EclipseProject project;
private EclipseClasspath classpath;
private EclipseJdt jdt;
private EclipseWtp wtp;
private final DefaultTaskDependency synchronizationTasks;
private final DefaultTaskDependency autoBuildTasks;
public EclipseModel() {
synchronizationTasks = new DefaultTaskDependency();
autoBuildTasks = new DefaultTaskDependency();
}
/**
* Constructor.
*
* @since 5.4
*/
@Inject
public EclipseModel(Project project) {
TaskDependencyFactory taskDependencyFactory = ((ProjectInternal) project).getTaskDependencyFactory();
this.synchronizationTasks = taskDependencyFactory.configurableDependency();
this.autoBuildTasks = taskDependencyFactory.configurableDependency();
}
/**
* Injects and returns an instance of {@link ObjectFactory}.
*
* @since 4.9
*/
@Inject
protected ObjectFactory getObjectFactory() {
throw new UnsupportedOperationException();
}
/**
* Configures eclipse project information
* <p>
* For examples see docs for {@link EclipseProject}
*/
public EclipseProject getProject() {
if (project == null) {
XmlTransformer xmlTransformer = new XmlTransformer();
xmlTransformer.setIndentation("\t");
project = getObjectFactory().newInstance(EclipseProject.class, new XmlFileContentMerger(xmlTransformer));
}
return project;
}
public void setProject(EclipseProject project) {
this.project = project;
}
/**
* Configures eclipse classpath information
* <p>
* For examples see docs for {@link EclipseClasspath}
*/
public EclipseClasspath getClasspath() {
return classpath;
}
public void setClasspath(EclipseClasspath classpath) {
this.classpath = classpath;
}
/**
* Configures eclipse java compatibility information (jdt)
* <p>
* For examples see docs for {@link EclipseProject}
*/
public EclipseJdt getJdt() {
return jdt;
}
public void setJdt(EclipseJdt jdt) {
this.jdt = jdt;
}
/**
* Configures eclipse wtp information
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public EclipseWtp getWtp() {
if (wtp == null) {
wtp = getObjectFactory().newInstance(EclipseWtp.class);
}
return wtp;
}
public void setWtp(EclipseWtp wtp) {
this.wtp = wtp;
}
/**
* Configures eclipse project information
* <p>
* For examples see docs for {@link EclipseProject}
*/
public void project(@DelegatesTo(EclipseProject.class) Closure closure) {
configure(closure, getProject());
}
/**
* Configures eclipse project information
* <p>
* For examples see docs for {@link EclipseProject}
*
* @since 3.5
*/
public void project(Action<? super EclipseProject> action) {
action.execute(getProject());
}
/**
* Configures eclipse classpath information
* <p>
* For examples see docs for {@link EclipseClasspath}
*/
public void classpath(@DelegatesTo(EclipseClasspath.class) Closure closure) {
configure(closure, classpath);
}
/**
* Configures eclipse classpath information
* <p>
* For examples see docs for {@link EclipseClasspath}
*
* @since 3.5
*/
public void classpath(Action<? super EclipseClasspath> action) {
action.execute(classpath);
}
/**
* Configures eclipse wtp information
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public void wtp(@DelegatesTo(EclipseWtp.class) Closure closure) {
configure(closure, wtp);
}
/**
* Configures eclipse wtp information
* <p>
* For examples see docs for {@link EclipseWtp}
*
* @since 3.5
*/
public void wtp(Action<? super EclipseWtp> action) {
action.execute(wtp);
}
/**
* Configures eclipse java compatibility information (jdt)
* <p>
* For examples see docs for {@link EclipseProject}
*/
public void jdt(@DelegatesTo(EclipseJdt.class) Closure closure) {
configure(closure, getJdt());
}
/**
* Configures eclipse java compatibility information (jdt)
* <p>
* For examples see docs for {@link EclipseProject}
*
* @since 3.5
*/
public void jdt(Action<? super EclipseJdt> action) {
action.execute(getJdt());
}
/**
* Returns the tasks to be executed before the Eclipse synchronization starts.
* <p>
* This property doesn't have a direct effect to the Gradle Eclipse plugin's behaviour. It is used, however, by
* Buildship to execute the configured tasks each time before the user imports the project or before a project
* synchronization starts.
*
* @return the tasks names
* @since 5.4
*/
public TaskDependency getSynchronizationTasks() {
return synchronizationTasks;
}
/**
* Set tasks to be executed before the Eclipse synchronization.
*
* @see #getSynchronizationTasks()
* @since 5.4
*/
public void synchronizationTasks(Object... synchronizationTasks) {
this.synchronizationTasks.add(synchronizationTasks);
}
/**
* Returns the tasks to be executed during the Eclipse auto-build.
* <p>
* This property doesn't have a direct effect to the Gradle Eclipse plugin's behaviour. It is used, however, by
* Buildship to execute the configured tasks each time when the Eclipse automatic build is triggered for the project.
*
* @return the tasks names
* @since 5.4
*/
public TaskDependency getAutoBuildTasks() {
return autoBuildTasks;
}
/**
* Set tasks to be executed during the Eclipse auto-build.
*
* @see #getAutoBuildTasks()
* @since 5.4
*/
public void autoBuildTasks(Object... autoBuildTasks) {
this.autoBuildTasks.add(autoBuildTasks);
}
/**
* Adds path variables to be used for replacing absolute paths in classpath entries.
* <p>
* If the beginning of the absolute path of a library or other path-related element matches a value of a variable,
* a variable entry is used. The matching part of the library path is replaced with the variable name.
* <p>
* For example see docs for {@link EclipseModel}
*
* @param pathVariables A map with String->File pairs.
*/
public void pathVariables(Map<String, File> pathVariables) {
Preconditions.checkNotNull(pathVariables);
classpath.getPathVariables().putAll(pathVariables);
if (wtp != null && wtp.getComponent() != null) {
wtp.getComponent().getPathVariables().putAll(pathVariables);
}
}
}