-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
EclipseWtpComponent.java
402 lines (363 loc) · 12.9 KB
/
EclipseWtpComponent.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Copyright 2011 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.collect.Iterables;
import com.google.common.collect.Lists;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.internal.project.ProjectInternal;
import org.gradle.api.internal.project.ProjectStateRegistry;
import org.gradle.plugins.ide.api.XmlFileContentMerger;
import org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory;
import org.gradle.plugins.ide.eclipse.model.internal.WtpComponentFactory;
import org.gradle.plugins.ide.internal.IdeArtifactRegistry;
import javax.inject.Inject;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.gradle.util.internal.ConfigureUtil.configure;
/**
* Enables fine-tuning wtp component details of the Eclipse plugin
* <p>
* Example of use with a blend of all possible properties.
* Bear in mind that usually you don't have to configure them directly because Gradle configures it for free!
*
* <pre class='autoTested'>
* plugins {
* id 'war' // or 'ear' or 'java'
* id 'eclipse-wtp'
* }
*
* configurations {
* someInterestingConfiguration
* anotherConfiguration
* }
*
* eclipse {
*
* //if you want parts of paths in resulting file(s) to be replaced by variables (files):
* pathVariables 'GRADLE_HOME': file('/best/software/gradle'), 'TOMCAT_HOME': file('../tomcat')
*
* wtp {
* component {
* //you can configure the context path:
* contextPath = 'someContextPath'
*
* //you can configure the deployName:
* deployName = 'killerApp'
*
* //you can alter the wb-resource elements.
* //non-existing source dirs won't be added to the component file.
* sourceDirs += file('someExtraFolder')
*
* // dependencies to mark as deployable with lib folder deploy path
* libConfigurations += [ configurations.someInterestingConfiguration ]
*
* // dependencies to mark as deployable with root folder deploy path
* rootConfigurations += [ configurations.someInterestingConfiguration ]
*
* // dependencies to exclude from wtp deployment
* minusConfigurations << configurations.anotherConfiguration
*
* //you can add a wb-resource elements; mandatory keys: 'sourcePath', 'deployPath':
* //if sourcePath points to non-existing folder it will *not* be added.
* resource sourcePath: 'extra/resource', deployPath: 'deployment/resource'
*
* //you can add a wb-property elements; mandatory keys: 'name', 'value':
* property name: 'moodOfTheDay', value: ':-D'
* }
* }
* }
* </pre>
*
* For tackling edge cases users can perform advanced configuration on resulting XML file.
* It is also possible to affect the way eclipse plugin merges the existing configuration
* via beforeMerged and whenMerged closures.
* <p>
* beforeMerged and whenMerged closures receive {@link WtpComponent} object
* <p>
* Examples of advanced configuration:
*
* <pre class='autoTested'>
* plugins {
* id 'war'
* id 'eclipse-wtp'
* }
*
* eclipse {
*
* wtp {
* component {
* file {
* //if you want to mess with the resulting XML in whatever way you fancy
* withXml {
* def node = it.asNode()
* node.appendNode('xml', 'is what I love')
* }
*
* //closure executed after wtp component file content is loaded from existing file
* //but before gradle build information is merged
* beforeMerged { wtpComponent ->
* //tinker with {@link WtpComponent} here
* }
*
* //closure executed after wtp component file content is loaded from existing file
* //and after gradle build information is merged
* whenMerged { wtpComponent ->
* //you can tinker with the {@link WtpComponent} here
* }
* }
* }
* }
* }
* </pre>
*/
public abstract class EclipseWtpComponent {
private final Project project;
private final XmlFileContentMerger file;
private Set<File> sourceDirs;
private Set<Configuration> rootConfigurations = new LinkedHashSet<>();
private Set<Configuration> libConfigurations = new LinkedHashSet<>();
private Set<Configuration> minusConfigurations = new LinkedHashSet<>();
private String deployName;
private List<WbResource> resources = new ArrayList<>();
private List<WbProperty> properties = new ArrayList<>();
private String contextPath;
private String classesDeployPath = "/WEB-INF/classes";
private String libDeployPath;
private Map<String, File> pathVariables = new HashMap<>();
@Inject
public EclipseWtpComponent(org.gradle.api.Project project, XmlFileContentMerger file) {
this.project = project;
this.file = file;
}
public Project getProject() {
return project;
}
/**
* See {@link #file(Action) }
*/
public XmlFileContentMerger getFile() {
return file;
}
/**
* Enables advanced configuration like tinkering with the output XML
* or affecting the way existing wtp component file content is merged with gradle build information
* <p>
* The object passed to whenMerged{} and beforeMerged{} closures is of type {@link WtpComponent}
* <p>
* For example see docs for {@link EclipseWtpComponent}
*/
public void file(@DelegatesTo(XmlFileContentMerger.class) Closure closure) {
configure(closure, file);
}
/**
* Enables advanced configuration like tinkering with the output XML
* or affecting the way existing wtp component file content is merged with gradle build information.
* <p>
* For example see docs for {@link EclipseWtpComponent}
*
* @since 3.5
*/
public void file(Action<? super XmlFileContentMerger> action) {
action.execute(file);
}
/**
* Source directories to be transformed into wb-resource elements.
* <p>
* For examples see docs for {@link EclipseWtp}
* <p>
* Only source dirs that exist will be added to the wtp component file.
* Non-existing resource directory declarations lead to errors when project is imported into Eclipse.
*/
public Set<File> getSourceDirs() {
return sourceDirs;
}
public void setSourceDirs(Set<File> sourceDirs) {
this.sourceDirs = sourceDirs;
}
/**
* The configurations whose files are to be marked to be deployed with a deploy path of '/'.
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public Set<Configuration> getRootConfigurations() {
return rootConfigurations;
}
public void setRootConfigurations(Set<Configuration> rootConfigurations) {
this.rootConfigurations = rootConfigurations;
}
/**
* The configurations whose files are to be marked to be deployed with a deploy path of {@link #getLibDeployPath()}.
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public Set<Configuration> getLibConfigurations() {
return libConfigurations;
}
public void setLibConfigurations(Set<Configuration> libConfigurations) {
this.libConfigurations = libConfigurations;
}
/**
* Synonym for {@link #getLibConfigurations()}.
*/
public Set<Configuration> getPlusConfigurations() {
return getLibConfigurations();
}
/**
* Synonym for {@link #setLibConfigurations(Set)}.
*/
public void setPlusConfigurations(Set<Configuration> plusConfigurations) {
setLibConfigurations(plusConfigurations);
}
/**
* The configurations whose files are to be excluded from wtp deployment.
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public Set<Configuration> getMinusConfigurations() {
return minusConfigurations;
}
public void setMinusConfigurations(Set<Configuration> minusConfigurations) {
this.minusConfigurations = minusConfigurations;
}
/**
* The deploy name to be used.
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public String getDeployName() {
return deployName;
}
public void setDeployName(String deployName) {
this.deployName = deployName;
}
/**
* Additional wb-resource elements.
* <p>
* For examples see docs for {@link EclipseWtp}
* <p>
* Only resources that link to an existing directory ({@link WbResource#getSourcePath()})
* will be added to the wtp component file.
* The reason is that non-existing resource directory declarations
* lead to errors when project is imported into Eclipse.
*/
public List<WbResource> getResources() {
return resources;
}
public void setResources(List<WbResource> resources) {
this.resources = resources;
}
/**
* Adds a wb-resource.
* <p>
* For examples see docs for {@link EclipseWtp}
*
* @param args A map that must contain a deployPath and sourcePath key with corresponding values.
*/
public void resource(Map<String, String> args) {
resources = Lists.newArrayList(Iterables.concat(getResources(), Collections.singleton(new WbResource(args.get("deployPath"), args.get("sourcePath")))));
}
/**
* Additional property elements.
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public List<WbProperty> getProperties() {
return properties;
}
public void setProperties(List<WbProperty> properties) {
this.properties = properties;
}
/**
* Adds a property.
* <p>
* For examples see docs for {@link EclipseWtp}
*
* @param args A map that must contain a 'name' and 'value' key with corresponding values.
*/
public void property(Map<String, String> args) {
properties = Lists.newArrayList(Iterables.concat(getProperties(), Collections.singleton(new WbProperty(args.get("name"), args.get("value")))));
}
/**
* The context path for the web application
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public String getContextPath() {
return contextPath;
}
public void setContextPath(String contextPath) {
this.contextPath = contextPath;
}
/**
* The deploy path for classes.
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public String getClassesDeployPath() {
return classesDeployPath;
}
public void setClassesDeployPath(String classesDeployPath) {
this.classesDeployPath = classesDeployPath;
}
/**
* The deploy path for libraries.
* <p>
* For examples see docs for {@link EclipseWtp}
*/
public String getLibDeployPath() {
return libDeployPath;
}
public void setLibDeployPath(String libDeployPath) {
this.libDeployPath = libDeployPath;
}
/**
* The variables to be used for replacing absolute path in dependent-module elements.
* <p>
* For examples see docs for {@link EclipseModel}
*/
public Map<String, File> getPathVariables() {
return pathVariables;
}
public void setPathVariables(Map<String, File> pathVariables) {
this.pathVariables = pathVariables;
}
public FileReferenceFactory getFileReferenceFactory() {
FileReferenceFactory referenceFactory = new FileReferenceFactory();
for (Map.Entry<String, File> pathVariable : pathVariables.entrySet()) {
referenceFactory.addPathVariable(pathVariable.getKey(), pathVariable.getValue());
}
return referenceFactory;
}
@SuppressWarnings("unchecked")
public void mergeXmlComponent(WtpComponent xmlComponent) {
file.getBeforeMerged().execute(xmlComponent);
ProjectInternal projectInternal = (ProjectInternal) this.project;
IdeArtifactRegistry ideArtifactRegistry = projectInternal.getServices().get(IdeArtifactRegistry.class);
ProjectStateRegistry projectRegistry = projectInternal.getServices().get(ProjectStateRegistry.class);
new WtpComponentFactory(projectInternal, ideArtifactRegistry, projectRegistry).configure(this, xmlComponent);
file.getWhenMerged().execute(xmlComponent);
}
}