-
Notifications
You must be signed in to change notification settings - Fork 316
/
AbstractXtextGeneratorMojo.java
358 lines (313 loc) · 12.3 KB
/
AbstractXtextGeneratorMojo.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
/*******************************************************************************
* Copyright (c) 2018, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.maven;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.xtext.builder.standalone.LanguageAccess;
import org.eclipse.xtext.builder.standalone.LanguageAccessFactory;
import org.eclipse.xtext.builder.standalone.StandaloneBuilder;
import org.eclipse.xtext.builder.standalone.compiler.CompilerConfiguration;
import org.eclipse.xtext.builder.standalone.compiler.IJavaCompiler;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.resource.persistence.IResourceStorageFacade;
import org.eclipse.xtext.resource.persistence.StorageAwareResource;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import com.google.common.base.Predicate;
import com.google.inject.Guice;
import com.google.inject.Injector;
/**
* @author Dennis Huebner - Initial contribution and API
*/
public abstract class AbstractXtextGeneratorMojo extends AbstractXtextMojo {
/**
* File encoding argument for the generator.
*/
@Parameter(property = "xtext.encoding", defaultValue = "${project.build.sourceEncoding}")
private String encoding;
public String getEncoding() {
return encoding;
}
/**
* Location of the generated source files.
*/
@Parameter(defaultValue = "${project.build.directory}/xtext-temp")
private String tmpClassDirectory;
/**
* Java source roots. List of folders, where the java source files are
* located.<br>
* The default value is a reference to the project's
* ${project.compileSourceRoots}.<br>
* When adding a new entry the default value will be overwritten not
* extended.<br>
* Used when your language needs java.
*/
@Parameter(defaultValue = "${project.compileSourceRoots}", required = true)
private List<String> javaSourceRoots;
/**
* you can specify a list of project mappings that is used to populate the EMF
* Platform Resource Map.
*
* <pre>
<projectMappings>
<projectMapping>
<projectName>sample.emf</projectName>
<path>${project.basedir}</path>
</projectMapping>
<projectMapping>
<projectName>sample.emf.edit</projectName>
<path>${project.basedir}/../sample.emf.edit</path>
</projectMapping>
</projectMappings>
* </pre>
*/
@Parameter
private List<ProjectMapping> projectMappings;
@Parameter(property = "xtext.generator.skip", defaultValue = "false")
private boolean skip;
@Parameter(defaultValue = "true")
private boolean failOnValidationError = true;
@Parameter(property = "maven.compiler.source", defaultValue = "11")
private String compilerSourceLevel;
@Parameter(property = "maven.compiler.target", defaultValue = "11")
private String compilerTargetLevel;
@Parameter(defaultValue = "false")
private boolean compilerSkipAnnotationProcessing;
@Parameter(defaultValue = "false")
private boolean compilerPreserveInformationAboutFormalParameters;
/**
* RegEx expression to filter class path during model files look up
*/
@Parameter
private String classPathLookupFilter;
/**
* Clustering configuration to avoid OOME
*/
@Parameter
private ClusteringConfig clusteringConfig;
/**
* if enabled the plugin will scan the project and its siblings and add them to
* the platform resource map automatically
*/
@Parameter(defaultValue = "false")
private boolean autoFillPlatformResourceMap;
/**
* Automatically add all output directories of all configured languages to the
* compile source roots needed by maven-compiler
*/
@Parameter(defaultValue = "true")
private boolean addOutputDirectoriesToCompileSourceRoots = true;
/**
* Track dependencies between model files and perform incremental builds when re-running
* the build after some files were touched.
*/
@Parameter(defaultValue = "false")
private boolean incrementalXtextBuild;
/**
* Whether to use the {@link StorageAwareResource} to write the semantic model, the
* {@link IResourceDescription resource description} and optionally the node model to a ResourceStorage.<br>
* For details see {@link StorageAwareResource} and {@link IResourceStorageFacade}.
*
* @since 2.35
*/
@Parameter(defaultValue = "false")
private boolean writeStorageResources;
@Parameter( readonly = true, defaultValue = "${plugin.artifacts}" )
private List<Artifact> pluginDependencies;
/**
* This way, a DSL file can refer to other DSL libraries only during compilation,
* by adding a dependency in the plugin section, not in the project.
*/
@Parameter(defaultValue = "false")
private boolean includePluginDependencies;
/**
* Location to which the class-path configuration shall be written. The file format is internal
* to the {@link StandaloneBuilder}.
* @see #writeClassPathConfigurationLocation
*/
@Parameter(defaultValue = "${project.build.directory}/xtext.classpath")
private String classpathConfigurationLocation;
/**
* Allows to write the class-path configuration to a file. The file format is internal
* to the {@link StandaloneBuilder}.
* @see #classpathConfigurationLocation
*/
@Parameter(defaultValue = "false")
private boolean writeClasspathConfiguration = false;
@Parameter( defaultValue = "${mojoExecution}", readonly = true )
private MojoExecution mojoExecution;
/*
* (non-Javadoc)
*
* @see org.apache.maven.plugin.Mojo#execute()
*/
public void execute() throws MojoExecutionException, MojoFailureException {
if (skip) {
getLog().info("skipped.");
} else {
synchronized (lock) {
new MavenLog4JConfigurator().configureLog4j(getLog());
autoAddToPlatformResourceMap(getProject());
manuallyAddToPlatformResourceMap();
internalExecute();
}
}
}
protected void internalExecute() throws MojoExecutionException {
if (addOutputDirectoriesToCompileSourceRoots) {
configureMavenOutputs();
}
Map<String, LanguageAccess> languages = new LanguageAccessFactory().createLanguageAccess(getLanguages(),
this.getClass().getClassLoader());
Injector injector = Guice.createInjector(new MavenStandaloneBuilderModule());
StandaloneBuilder builder = injector.getInstance(StandaloneBuilder.class);
builder.setBaseDir(getProject().getBasedir().getAbsolutePath());
builder.setLanguages(languages);
builder.setEncoding(getEncoding());
builder.setClassPathEntries(getClasspathEntries());
builder.setClassPathLookUpFilter(classPathLookupFilter);
builder.setSourceDirs(getSourceRoots());
builder.setJavaSourceDirs(javaSourceRoots);
builder.setFailOnValidationError(failOnValidationError);
builder.setTempDir(createTempDir().getAbsolutePath());
builder.setDebugLog(getLog().isDebugEnabled());
builder.setIncrementalBuild(incrementalXtextBuild);
builder.setWriteStorageResources(writeStorageResources);
if (writeClasspathConfiguration) {
builder.setClasspathConfigurationLocation(classpathConfigurationLocation, mojoExecution.getGoal(), getClassOutputDirectory());
}
if (clusteringConfig != null) {
builder.setClusteringConfig(clusteringConfig.convertToStandaloneConfig());
}
configureCompiler(builder.getCompiler());
logState();
boolean errorDetected = !builder.launch();
if (errorDetected && failOnValidationError) {
throw new MojoExecutionException("Execution failed due to a severe validation error.");
}
}
protected abstract String getClassOutputDirectory();
protected abstract List<String> getSourceRoots();
private void configureCompiler(IJavaCompiler compiler) {
CompilerConfiguration conf = compiler.getConfiguration();
conf.setSourceLevel(compilerSourceLevel);
conf.setTargetLevel(compilerTargetLevel);
conf.setVerbose(getLog().isDebugEnabled());
conf.setSkipAnnotationProcessing(compilerSkipAnnotationProcessing);
conf.setPreserveInformationAboutFormalParameters(compilerPreserveInformationAboutFormalParameters);
}
private void logState() {
getLog().info(
"Encoding: " + (getEncoding() == null ? "not set. Encoding provider will be used." : getEncoding()));
getLog().info("Compiler source level: " + compilerSourceLevel);
getLog().info("Compiler target level: " + compilerTargetLevel);
if (getLog().isDebugEnabled()) {
getLog().debug("Source dirs: " + IterableExtensions.join(getSourceRoots(), ", "));
getLog().debug("Java source dirs: " + IterableExtensions.join(javaSourceRoots, ", "));
getLog().debug("Classpath entries: " + IterableExtensions.join(getClasspathEntries(), ", "));
}
}
private File createTempDir() {
File tmpDir = new File(tmpClassDirectory + tmpDirSuffix());
if (!tmpDir.mkdirs() && !tmpDir.exists()) {
throw new IllegalArgumentException("Couldn't create directory '" + tmpClassDirectory + "'.");
}
return tmpDir;
}
protected String tmpDirSuffix() {
return "";
}
protected Predicate<String> emptyStringFilter() {
return new Predicate<String>() {
public boolean apply(String input) {
return !Strings.isEmpty(input.trim());
}
};
}
private Set<String> getClasspathEntries() {
Set<String> classpathElements = getClasspathElements();
if (isIncludePluginDependencies()) {
getLog().info("Including plugin dependencies");
List<String> pluginClasspathElements = pluginDependencies.stream()
.map(e -> e.getFile().toPath().toString())
.collect(Collectors.toList());
classpathElements.addAll(pluginClasspathElements);
}
return classpathElements;
}
/**
* @return resolved class path elements
*/
public abstract Set<String> getClasspathElements();
protected abstract void configureMavenOutputs();
public List<ProjectMapping> getProjectMappings() {
return projectMappings;
}
public void setProjectMappings(List<ProjectMapping> projectMappings) {
this.projectMappings = projectMappings;
}
/**
* Adds the given project and its child modules
* ({@link MavenProject#getModules()}) to
* {@link EcorePlugin#getPlatformResourceMap()}. Furthermore, this traverses
* {@link MavenProject#getParent()} recursively, if set.
*
* @param project the project
*/
private void autoAddToPlatformResourceMap(final MavenProject project) {
if (autoFillPlatformResourceMap) {
addToPlatformResourceMap(project.getBasedir());
project.getModules().stream().map(module -> new File(project.getBasedir(), module))
.forEach(this::addToPlatformResourceMap);
if (project.getParent() != null)
autoAddToPlatformResourceMap(project.getParent());
}
}
private void manuallyAddToPlatformResourceMap() {
if (projectMappings != null) {
for (ProjectMapping projectMapping : projectMappings) {
if (projectMapping.getPath() != null && projectMapping.getProjectName() != null) {
String path = projectMapping.getPath().toURI().toString();
String name = projectMapping.getProjectName();
getLog().info("Adding project '" + name + "' with path '" + path + "' to Platform Resource Map");
final URI uri = URI.createURI(path);
EcorePlugin.getPlatformResourceMap().put(name, uri);
}
}
}
}
/**
* Adds the given file to EcorePlugin's platform resource map. The file's name
* will be used as the map entry's key.
*
* @param file a file to register
* @return the registered URI pointing to the given file
* @see EcorePlugin#getPlatformResourceMap()
*/
private URI addToPlatformResourceMap(final File file) {
getLog().info("Adding project '" + file.getName() + "' with path '" + file.toURI().toString()
+ "' to Platform Resource Map");
final URI uri = URI.createURI(file.toURI().toString());
return EcorePlugin.getPlatformResourceMap().put(file.getName(), uri);
}
protected boolean isIncludePluginDependencies() {
return includePluginDependencies;
}
}