1 package com.github.searls.jasmine.mojo;
2
3 import com.github.searls.jasmine.config.JasmineConfiguration;
4 import com.github.searls.jasmine.exception.StringifiesStackTraces;
5 import com.github.searls.jasmine.io.ScansDirectory;
6 import com.github.searls.jasmine.model.FileSystemReporter;
7 import com.github.searls.jasmine.model.Reporter;
8 import com.github.searls.jasmine.model.ScriptSearch;
9 import com.github.searls.jasmine.runner.SpecRunnerTemplate;
10 import com.github.searls.jasmine.thirdpartylibs.ProjectClassLoaderFactory;
11 import org.apache.maven.plugin.AbstractMojo;
12 import org.apache.maven.plugin.MojoExecutionException;
13 import org.apache.maven.plugin.MojoFailureException;
14 import org.apache.maven.plugins.annotations.Component;
15 import org.apache.maven.plugins.annotations.Parameter;
16 import org.apache.maven.project.MavenProject;
17 import org.codehaus.plexus.resource.ResourceManager;
18 import org.eclipse.jetty.server.Connector;
19
20 import java.io.File;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24
25 public abstract class AbstractJasmineMojo extends AbstractMojo implements JasmineConfiguration {
26
27
28
29
30
31
32
33
34 @Parameter(
35 property = "jsSrcDir",
36 defaultValue = "${project.basedir}${file.separator}src${file.separator}main${file.separator}javascript")
37 private File jsSrcDir;
38
39
40
41
42
43
44 @Parameter(
45 property = "jsTestSrcDir",
46 defaultValue = "${project.basedir}${file.separator}src${file.separator}test${file.separator}javascript")
47 private File jsTestSrcDir;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @Parameter
67 protected List<String> preloadSources;
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 @Parameter
87 protected String customRunnerTemplate;
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @Parameter
102 protected String customRunnerConfiguration;
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 @Parameter
119 protected List<Reporter> reporters = new ArrayList<Reporter>();
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137 @Parameter
138 protected List<FileSystemReporter> fileSystemReporters = new ArrayList<FileSystemReporter>();
139
140
141
142
143
144
145 @Parameter(defaultValue = "${project.build.directory}${file.separator}jasmine")
146 protected File jasmineTargetDir;
147
148
149
150
151
152
153
154 @Parameter(property = "skipTests")
155 protected boolean skipTests;
156
157
158
159
160
161
162
163 @Parameter(property = "maven.test.skip")
164 protected boolean mvnTestSkip;
165
166
167
168
169
170
171 @Parameter(property = "skipJasmineTests")
172 protected boolean skipJasmineTests;
173
174
175
176
177
178
179 @Parameter(property = "haltOnFailure", defaultValue = "true")
180 protected boolean haltOnFailure;
181
182
183
184
185
186
187 @Parameter(defaultValue = "300")
188 protected int timeout;
189
190
191
192
193
194
195 @Parameter(defaultValue = "false")
196 protected boolean debug;
197
198
199
200
201
202
203 @Parameter(defaultValue = "SpecRunner.html")
204 protected String specRunnerHtmlFileName;
205
206
207
208
209
210
211 @Parameter(defaultValue = "ManualSpecRunner.html")
212 protected String manualSpecRunnerHtmlFileName;
213
214
215
216
217
218
219 @Parameter(defaultValue = "spec")
220 protected String specDirectoryName;
221
222
223
224
225
226
227 @Parameter(defaultValue = "src")
228 protected String srcDirectoryName;
229
230
231
232
233
234
235 @Parameter(defaultValue = "${project.build.sourceEncoding}")
236 protected String sourceEncoding;
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 @Parameter
260 private final List<String> sourceIncludes = ScansDirectory.DEFAULT_INCLUDES;
261
262
263
264
265
266
267
268 @Parameter
269 private final List<String> sourceExcludes = Collections.emptyList();
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293 @Parameter
294 private final List<String> specIncludes = ScansDirectory.DEFAULT_INCLUDES;
295
296
297
298
299
300
301
302 @Parameter
303 private final List<String> specExcludes = Collections.emptyList();
304
305
306
307
308
309
310
311
312 @Parameter(property = "jasmine.serverPort", defaultValue = "8234")
313 protected int serverPort;
314
315
316
317
318
319
320 @Parameter(property = "jasmine.uriScheme", defaultValue = "http")
321 protected String uriScheme;
322
323
324
325
326
327
328
329
330
331 @Parameter(property = "jasmine.serverHostname", defaultValue = "localhost")
332 protected String serverHostname;
333
334
335
336
337
338
339
340
341
342 @Parameter(property = "jasmine.specRunnerTemplate", defaultValue = "DEFAULT")
343 protected SpecRunnerTemplate specRunnerTemplate;
344
345
346
347
348
349
350
351 @Parameter(property = "jasmine.autoRefreshInterval", defaultValue = "0")
352 protected int autoRefreshInterval;
353
354
355
356
357
358
359
360
361 @Parameter(property = "coffeeScriptCompilationEnabled", defaultValue = "true")
362 protected boolean coffeeScriptCompilationEnabled;
363
364
365
366
367
368
369
370
371
372 @Parameter(
373 property = "jasmine.connectorClass",
374 defaultValue = "org.eclipse.jetty.server.nio.SelectChannelConnector"
375 )
376 protected String connectorClass;
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 @Parameter
396 private List<Context> additionalContexts = Collections.emptyList();
397
398 @Parameter(defaultValue = "${project}", readonly = true)
399 protected MavenProject mavenProject;
400
401 @Component
402 ResourceManager resourceManager;
403
404 protected ResourceRetriever resourceRetriever;
405 protected ReporterRetriever reporterRetriever;
406
407 protected StringifiesStackTraces stringifiesStackTraces = new StringifiesStackTraces();
408
409 protected ScriptSearch sources;
410 protected ScriptSearch specs;
411
412 private File customRunnerTemplateFile;
413 private File customRunnerConfigurationFile;
414
415 @Override
416 public void execute() throws MojoExecutionException, MojoFailureException {
417 this.loadResources();
418
419 this.sources = new ScriptSearch(this.jsSrcDir, this.sourceIncludes, this.sourceExcludes);
420 this.specs = new ScriptSearch(this.jsTestSrcDir, this.specIncludes, this.specExcludes);
421
422 try {
423 this.run();
424 } catch (MojoFailureException e) {
425 throw e;
426 } catch (Exception e) {
427 throw new MojoExecutionException("The jasmine-maven-plugin encountered an exception: \n" + this.stringifiesStackTraces.stringify(e), e);
428 }
429 }
430
431 public abstract void run() throws Exception;
432
433 @Override
434 public String getSourceEncoding() {
435 return this.sourceEncoding;
436 }
437
438 @Override
439 public File getCustomRunnerTemplate() {
440 return this.customRunnerTemplateFile;
441 }
442
443 @Override
444 public SpecRunnerTemplate getSpecRunnerTemplate() {
445 return this.specRunnerTemplate;
446 }
447
448 @Override
449 public File getJasmineTargetDir() {
450 return this.jasmineTargetDir;
451 }
452
453 @Override
454 public String getSrcDirectoryName() {
455 return this.srcDirectoryName;
456 }
457
458 @Override
459 public ScriptSearch getSources() {
460 return this.sources;
461 }
462
463 @Override
464 public ScriptSearch getSpecs() {
465 return this.specs;
466 }
467
468 @Override
469 public String getSpecDirectoryName() {
470 return this.specDirectoryName;
471 }
472
473 @Override
474 public List<String> getPreloadSources() {
475 return this.preloadSources;
476 }
477
478 @Override
479 public int getAutoRefreshInterval() {
480 return this.autoRefreshInterval;
481 }
482
483 @Override
484 public boolean isCoffeeScriptCompilationEnabled() {
485 return this.coffeeScriptCompilationEnabled;
486 }
487
488 public MavenProject getMavenProject() {
489 return this.mavenProject;
490 }
491
492 @Override
493 public File getCustomRunnerConfiguration() {
494 return this.customRunnerConfigurationFile;
495 }
496
497 @Override
498 public List<Reporter> getReporters() {
499 return this.reporters;
500 }
501
502 @Override
503 public List<FileSystemReporter> getFileSystemReporters() {
504 return this.fileSystemReporters;
505 }
506
507 @Override
508 public File getBasedir() {
509 return this.mavenProject.getBasedir();
510 }
511
512 @Override
513 public ClassLoader getProjectClassLoader() {
514 return new ProjectClassLoaderFactory(mavenProject.getArtifacts()).create();
515 }
516
517 @Override
518 public List<Context> getContexts() {
519 List<Context> contexts = new ArrayList<Context>();
520 contexts.add(new Context(this.srcDirectoryName, this.jsSrcDir));
521 contexts.add(new Context(this.specDirectoryName, this.jsTestSrcDir));
522 contexts.addAll(additionalContexts);
523 return contexts;
524 }
525
526 protected Connector getConnector() throws MojoExecutionException {
527 try {
528 @SuppressWarnings("unchecked")
529 Class<? extends Connector> c = (Class<? extends Connector>) Class.forName(connectorClass);
530 return c.newInstance();
531 } catch (InstantiationException e) {
532 throw new MojoExecutionException("Unable to instantiate.", e);
533 } catch (IllegalAccessException e) {
534 throw new MojoExecutionException("Unable to instantiate.", e);
535 } catch (ClassNotFoundException e) {
536 throw new MojoExecutionException("Unable to instantiate.", e);
537 }
538 }
539
540 protected boolean isSkipTests() {
541 return this.skipTests || this.mvnTestSkip || this.skipJasmineTests;
542 }
543
544 private void loadResources() throws MojoExecutionException {
545 this.customRunnerTemplateFile = getResourceRetriever().getResourceAsFile("customRunnerTemplate", this.customRunnerTemplate, this.mavenProject);
546 this.customRunnerConfigurationFile = getResourceRetriever().getResourceAsFile("customRunnerConfiguration", this.customRunnerConfiguration, this.mavenProject);
547 this.reporters = getReporterRetriever().retrieveReporters(this.reporters, this.mavenProject);
548 this.fileSystemReporters = getReporterRetriever().retrieveFileSystemReporters(this.fileSystemReporters, this.getJasmineTargetDir(), this.mavenProject);
549 }
550
551 private ResourceRetriever getResourceRetriever() {
552 if (resourceRetriever == null) {
553 resourceRetriever = new ResourceRetriever(resourceManager);
554 }
555 return resourceRetriever;
556 }
557
558 private ReporterRetriever getReporterRetriever() {
559 if (reporterRetriever == null) {
560 reporterRetriever = new ReporterRetriever(getResourceRetriever());
561 }
562 return reporterRetriever;
563 }
564 }