-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
EclipseProject.java
382 lines (344 loc) · 13.8 KB
/
EclipseProject.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
/*
* 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.collect.ImmutableSet;
import com.google.common.collect.Sets;
import groovy.lang.Closure;
import groovy.lang.DelegatesTo;
import org.gradle.api.Action;
import org.gradle.api.InvalidUserDataException;
import org.gradle.plugins.ide.api.XmlFileContentMerger;
import org.gradle.plugins.ide.eclipse.model.internal.DefaultResourceFilter;
import org.gradle.util.internal.ClosureBackedAction;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
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 project details (.project file) of the Eclipse plugin
* <p>
* Example of use with a blend of all possible properties.
* Bear in mind that usually you don't have configure eclipse project directly because Gradle configures it for free!
*
* <pre class='autoTested'>
* plugins {
* id 'java'
* id 'eclipse'
* }
*
* eclipse {
* project {
* //if you don't like the name Gradle has chosen
* name = 'someBetterName'
*
* //if you want to specify the Eclipse project's comment
* comment = 'Very interesting top secret project'
*
* //if you want to append some extra referenced projects in a declarative fashion:
* referencedProjects 'someProject', 'someOtherProject'
* //if you want to assign referenced projects
* referencedProjects = ['someProject'] as Set
*
* //if you want to append some extra natures in a declarative fashion:
* natures 'some.extra.eclipse.nature', 'some.another.interesting.nature'
* //if you want to assign natures in a groovy fashion:
* natures = ['some.extra.eclipse.nature', 'some.another.interesting.nature']
*
* //if you want to append some extra build command:
* buildCommand 'buildThisLovelyProject'
* //if you want to append a build command with parameters:
* buildCommand 'buildItWithTheArguments', argumentOne: "I'm first", argumentTwo: "I'm second"
*
* //if you want to create an extra link in the eclipse project,
* //by location uri:
* linkedResource name: 'someLinkByLocationUri', type: 'someLinkType', locationUri: 'file://someUri'
* //by location:
* linkedResource name: 'someLinkByLocation', type: 'someLinkType', location: '/some/location'
*
* //if you don't want any node_modules folder to appear in Eclipse, you can filter it out:
* resourceFilter {
* appliesTo = 'FOLDERS'
* type = 'EXCLUDE_ALL'
* matcher {
* id = 'org.eclipse.ui.ide.multiFilter'
* arguments = '1.0-name-matches-false-false-node_modules'
* }
* }
* }
* }
* </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 Project} object
* <p>
* Examples of advanced configuration:
*
* <pre class='autoTested'>
* plugins {
* id 'java'
* id 'eclipse'
* }
*
* eclipse {
* project {
*
* 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 .project content is loaded from existing file
* //but before gradle build information is merged
* beforeMerged { project ->
* //if you want skip merging natures... (a very abstract example)
* project.natures.clear()
* }
*
* //closure executed after .project content is loaded from existing file
* //and after gradle build information is merged
* whenMerged { project ->
* //you can tinker with the {@link Project} here
* }
* }
* }
* }
* </pre>
*/
public abstract class EclipseProject {
public static final ImmutableSet<String> VALID_LINKED_RESOURCE_ARGS = ImmutableSet.of("name", "type", "location", "locationUri");
private String name;
private String comment;
private Set<String> referencedProjects = new LinkedHashSet<>();
private List<String> natures = new ArrayList<>();
private List<BuildCommand> buildCommands = new ArrayList<>();
private Set<Link> linkedResources = new LinkedHashSet<>();
private Set<ResourceFilter> resourceFilters = new LinkedHashSet<>();
private final XmlFileContentMerger file;
@Inject
public EclipseProject(XmlFileContentMerger file) {
this.file = file;
}
public String getName() {
return name;
}
/**
* Configures eclipse project name. It is <b>optional</b> because the task should configure it correctly for you.
* By default it will try to use the <b>project.name</b> or prefix it with a part of a <b>project.path</b>
* to make sure the moduleName is unique in the scope of a multi-module build.
* The 'uniqueness' of a module name is required for correct import
* into Eclipse and the task will make sure the name is unique.
* <p>
* The logic that makes sure project names are unique is available <b>since</b> 1.0-milestone-2
* <p>
* If your project has problems with unique names it is recommended to always run gradle eclipse from the root, e.g. for all subprojects, including generation of .classpath.
* If you run the generation of the eclipse project only for a single subproject then you may have different results
* because the unique names are calculated based on eclipse projects that are involved in the specific build run.
* <p>
* If you update the project names then make sure you run gradle eclipse from the root, e.g. for all subprojects.
* The reason is that there may be subprojects that depend on the subproject with amended eclipse project name.
* So you want them to be generated as well because the project dependencies in .classpath need to refer to the amended project name.
* Basically, for non-trivial projects it is recommended to always run gradle eclipse from the root.
* <p>
* For example see docs for {@link EclipseProject}
*/
public void setName(String name) {
this.name = name;
}
public String getComment() {
return comment;
}
/**
* A comment used for the eclipse project. By default it will be configured to <b>project.description</b>
* <p>
* For example see docs for {@link EclipseProject}
*/
public void setComment(String comment) {
this.comment = comment;
}
public Set<String> getReferencedProjects() {
return referencedProjects;
}
/**
* The referenced projects of this Eclipse project (*not*: java build path project references).
* <p>
* Referencing projects does not mean adding a build path dependencies between them!
* If you need to configure a build path dependency use Gradle's dependencies section or
* eclipse.classpath.whenMerged { classpath -> ... to manipulate the classpath entries
* <p>
* For example see docs for {@link EclipseProject}
*/
public void setReferencedProjects(Set<String> referencedProjects) {
this.referencedProjects = referencedProjects;
}
/**
* The referenced projects of this Eclipse project (*not*: java build path project references). <p> Referencing projects does not mean adding a build path dependencies between them! If you need to
* configure a build path dependency use Gradle's dependencies section or eclipse.classpath.whenMerged { classpath -> ... to manipulate the classpath entries
*
* @param referencedProjects The name of the project references.
*/
public void referencedProjects(String... referencedProjects) {
assert referencedProjects != null;
this.referencedProjects.addAll(Arrays.asList(referencedProjects));
}
public List<String> getNatures() {
return natures;
}
/**
* The natures to be added to this Eclipse project.
* <p>
* For example see docs for {@link EclipseProject}
*/
public void setNatures(List<String> natures) {
this.natures = natures;
}
/**
* Appends natures entries to the eclipse project. <p> For example see docs for {@link EclipseProject}
*
* @param natures the nature names
*/
public void natures(String... natures) {
assert natures != null;
this.natures.addAll(Arrays.asList(natures));
}
public List<BuildCommand> getBuildCommands() {
return buildCommands;
}
/**
* The build commands to be added to this Eclipse project.
* <p>
* For example see docs for {@link EclipseProject}
*/
public void setBuildCommands(List<BuildCommand> buildCommands) {
this.buildCommands = buildCommands;
}
/**
* Adds a build command with arguments to the eclipse project. <p> For example see docs for {@link EclipseProject}
*
* @param args A map with arguments, where the key is the name of the argument and the value the value.
* @param buildCommand The name of the build command.
* @see #buildCommand(String)
*/
public void buildCommand(Map<String, String> args, String buildCommand) {
assert buildCommand != null;
buildCommands.add(new BuildCommand(buildCommand, args));
}
/**
* Adds a build command to the eclipse project. <p> For example see docs for {@link EclipseProject}
*
* @param buildCommand The name of the build command
* @see #buildCommand(Map, String)
*/
public void buildCommand(String buildCommand) {
assert buildCommand != null;
buildCommands.add(new BuildCommand(buildCommand));
}
public Set<Link> getLinkedResources() {
return linkedResources;
}
/**
* The linked resources to be added to this Eclipse project.
* <p>
* For example see docs for {@link EclipseProject}
*/
public void setLinkedResources(Set<Link> linkedResources) {
this.linkedResources = linkedResources;
}
/**
* Adds a resource link (aka 'source link') to the eclipse project. <p> For example see docs for {@link EclipseProject}
*
* @param args A maps with the args for the link. Legal keys for the map are name, type, location and locationUri.
*/
public void linkedResource(Map<String, String> args) {
Set<String> illegalArgs = Sets.difference(args.keySet(), VALID_LINKED_RESOURCE_ARGS);
if (!illegalArgs.isEmpty()) {
throw new InvalidUserDataException("You provided illegal argument for a link: " + illegalArgs + ". Valid link args are: " + VALID_LINKED_RESOURCE_ARGS);
}
linkedResources.add(new Link(args.get("name"), args.get("type"), args.get("location"), args.get("locationUri")));
}
/**
* The resource filters of the eclipse project.
* @since 3.5
*/
public Set<ResourceFilter> getResourceFilters() {
return resourceFilters;
}
/**
* Adds a resource filter to the eclipse project.
* <p>
* For examples, see docs for {@link ResourceFilter}
*
* @param configureClosure The closure to use to configure the resource filter.
* @since 3.5
*/
public ResourceFilter resourceFilter(@DelegatesTo(value=ResourceFilter.class, strategy = Closure.DELEGATE_FIRST) Closure configureClosure) {
return resourceFilter(new ClosureBackedAction<ResourceFilter>(configureClosure));
}
/**
* Adds a resource filter to the eclipse project.
* <p>
* For examples, see docs for {@link ResourceFilter}
*
* @param configureAction The action to use to configure the resource filter.
* @since 3.5
*/
public ResourceFilter resourceFilter(Action<? super ResourceFilter> configureAction) {
ResourceFilter f = new DefaultResourceFilter();
configureAction.execute(f);
resourceFilters.add(f);
return f;
}
/**
* Enables advanced configuration like tinkering with the output XML or affecting the way existing .project content is merged with gradle build information <p> The object passed to whenMerged{}
* and beforeMerged{} closures is of type {@link Project} <p>
*
* For example see docs for {@link EclipseProject}
*/
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 .project content is merged with gradle build information.
*
* For example see docs for {@link EclipseProject}
*
* @since 3.5
*/
public void file(Action<? super XmlFileContentMerger> action) {
action.execute(file);
}
/**
* See {@link #file(Action)}
*/
public final XmlFileContentMerger getFile() {
return file;
}
@SuppressWarnings("unchecked")
public void mergeXmlProject(Project xmlProject) {
Project decoratedProject = new NonRenamableProject(xmlProject);
file.getBeforeMerged().execute(decoratedProject);
xmlProject.configure(this);
file.getWhenMerged().execute(decoratedProject);
}
}