1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.maven.plugin;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Date;
26 import java.util.HashSet;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.Set;
30
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugin.MojoFailureException;
34 import org.codehaus.plexus.util.FileUtils;
35 import org.eclipse.jetty.util.Scanner;
36 import org.eclipse.jetty.util.resource.Resource;
37 import org.eclipse.jetty.webapp.WebAppContext;
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 public class JettyRunMojo extends AbstractJettyMojo
69 {
70 public static final String DEFAULT_WEBAPP_SRC = "src"+File.separator+"main"+File.separator+"webapp";
71 public static final String FAKE_WEBAPP = "webapp-tmp";
72
73
74
75
76
77
78
79
80
81
82 protected boolean useTestScope;
83
84
85
86
87
88
89
90
91
92 protected String webXml;
93
94
95
96
97
98
99
100
101
102 protected File classesDirectory;
103
104
105
106
107
108
109
110
111 protected File testClassesDirectory;
112
113
114
115
116
117
118
119
120 protected File webAppSourceDirectory;
121
122
123
124
125
126
127 protected File[] scanTargets;
128
129
130
131
132
133
134
135
136 protected ScanTargetPattern[] scanTargetPatterns;
137
138
139
140
141
142 protected List<File> extraScanTargets;
143
144
145
146
147
148 protected WarPluginInfo warPluginInfo;
149
150
151
152
153
154 protected List<Artifact> warArtifacts;
155
156
157
158
159
160
161
162
163
164 @Override
165 public void execute() throws MojoExecutionException, MojoFailureException
166 {
167 warPluginInfo = new WarPluginInfo(project);
168 super.execute();
169 }
170
171
172
173
174
175
176
177
178
179 public void checkPomConfiguration () throws MojoExecutionException
180 {
181
182 try
183 {
184 if ((webAppSourceDirectory == null) || !webAppSourceDirectory.exists())
185 {
186 getLog().info("webAppSourceDirectory"+(webAppSourceDirectory == null ? " not set." : (webAppSourceDirectory.getAbsolutePath()+" does not exist."))+" Trying "+DEFAULT_WEBAPP_SRC);
187 webAppSourceDirectory = new File (project.getBasedir(), DEFAULT_WEBAPP_SRC);
188 if (!webAppSourceDirectory.exists())
189 {
190 getLog().info("webAppSourceDirectory "+webAppSourceDirectory.getAbsolutePath()+" does not exist. Trying "+project.getBuild().getDirectory()+File.separator+FAKE_WEBAPP);
191
192
193 File target = new File(project.getBuild().getDirectory());
194 webAppSourceDirectory = new File(target, FAKE_WEBAPP);
195 if (!webAppSourceDirectory.exists())
196 webAppSourceDirectory.mkdirs();
197 }
198 }
199 else
200 getLog().info( "Webapp source directory = " + webAppSourceDirectory.getCanonicalPath());
201 }
202 catch (IOException e)
203 {
204 throw new MojoExecutionException("Webapp source directory does not exist", e);
205 }
206
207
208 if ( !"automatic".equalsIgnoreCase( reload ) && !"manual".equalsIgnoreCase( reload ) )
209 {
210 throw new MojoExecutionException( "invalid reload mechanic specified, must be 'automatic' or 'manual'" );
211 }
212 else
213 {
214 getLog().info("Reload Mechanic: " + reload );
215 }
216
217
218
219 try
220 {
221
222 if (classesDirectory != null)
223 {
224 if (!classesDirectory.exists())
225 getLog().info( "Classes directory "+ classesDirectory.getCanonicalPath()+ " does not exist");
226 else
227 getLog().info("Classes = " + classesDirectory.getCanonicalPath());
228 }
229 else
230 getLog().info("Classes directory not set");
231 }
232 catch (IOException e)
233 {
234 throw new MojoExecutionException("Location of classesDirectory does not exist");
235 }
236
237 extraScanTargets = new ArrayList<File>();
238 if (scanTargets != null)
239 {
240 for (int i=0; i< scanTargets.length; i++)
241 {
242 getLog().info("Added extra scan target:"+ scanTargets[i]);
243 extraScanTargets.add(scanTargets[i]);
244 }
245 }
246
247 if (scanTargetPatterns!=null)
248 {
249 for (int i=0;i<scanTargetPatterns.length; i++)
250 {
251 Iterator itor = scanTargetPatterns[i].getIncludes().iterator();
252 StringBuffer strbuff = new StringBuffer();
253 while (itor.hasNext())
254 {
255 strbuff.append((String)itor.next());
256 if (itor.hasNext())
257 strbuff.append(",");
258 }
259 String includes = strbuff.toString();
260
261 itor = scanTargetPatterns[i].getExcludes().iterator();
262 strbuff= new StringBuffer();
263 while (itor.hasNext())
264 {
265 strbuff.append((String)itor.next());
266 if (itor.hasNext())
267 strbuff.append(",");
268 }
269 String excludes = strbuff.toString();
270
271 try
272 {
273 List<File> files = FileUtils.getFiles(scanTargetPatterns[i].getDirectory(), includes, excludes);
274 itor = files.iterator();
275 while (itor.hasNext())
276 getLog().info("Adding extra scan target from pattern: "+itor.next());
277 List<File> currentTargets = extraScanTargets;
278 if(currentTargets!=null && !currentTargets.isEmpty())
279 currentTargets.addAll(files);
280 else
281 extraScanTargets = files;
282 }
283 catch (IOException e)
284 {
285 throw new MojoExecutionException(e.getMessage());
286 }
287 }
288 }
289 }
290
291
292
293
294
295
296
297 public void configureWebApplication() throws Exception
298 {
299 super.configureWebApplication();
300
301
302
303
304
305
306 Resource webAppSourceDirectoryResource = Resource.newResource(webAppSourceDirectory.getCanonicalPath());
307 if (webApp.getWar() == null)
308 webApp.setWar(webAppSourceDirectoryResource.toString());
309
310 if (webApp.getBaseResource() == null)
311 webApp.setBaseResource(webAppSourceDirectoryResource);
312
313 if (classesDirectory != null)
314 webApp.setClasses (classesDirectory);
315 if (useTestScope && (testClassesDirectory != null))
316 webApp.setTestClasses (testClassesDirectory);
317
318 webApp.setWebInfLib (getDependencyFiles());
319
320
321 Set<Artifact> matchedWarArtifacts = new HashSet<Artifact>();
322
323
324 for (Artifact a:getWarArtifacts())
325 extraScanTargets.add(a.getFile());
326
327
328 List<Overlay> overlays = new ArrayList<Overlay>();
329 for (OverlayConfig config:warPluginInfo.getMavenWarOverlayConfigs())
330 {
331
332 if (config.isSkip())
333 continue;
334
335
336 if (config.isCurrentProject())
337 {
338 Overlay overlay = new Overlay(config, null);
339 overlays.add(overlay);
340 continue;
341 }
342
343
344 Artifact a = getArtifactForOverlay(config, getWarArtifacts());
345 if (a != null)
346 {
347 matchedWarArtifacts.add(a);
348 SelectiveJarResource r = new SelectiveJarResource(new URL("jar:"+Resource.toURL(a.getFile()).toString()+"!/"));
349 r.setIncludes(config.getIncludes());
350 r.setExcludes(config.getExcludes());
351 Overlay overlay = new Overlay(config, r);
352 overlays.add(overlay);
353 }
354 }
355
356
357 for (Artifact a: getWarArtifacts())
358 {
359 if (!matchedWarArtifacts.contains(a))
360 {
361 Overlay overlay = new Overlay(null, Resource.newResource(new URL("jar:"+Resource.toURL(a.getFile()).toString()+"!/")));
362 overlays.add(overlay);
363 }
364 }
365
366 webApp.setOverlays(overlays);
367
368
369 if (webApp.getDescriptor() == null)
370 {
371
372 if (webXml != null)
373 {
374 Resource r = Resource.newResource(webXml);
375 if (r.exists() && !r.isDirectory())
376 {
377 webApp.setDescriptor(r.toString());
378 }
379 }
380
381
382 if (webApp.getDescriptor() == null && webApp.getBaseResource() != null)
383 {
384 Resource r = webApp.getBaseResource().addPath("WEB-INF/web.xml");
385 if (r.exists() && !r.isDirectory())
386 {
387 webApp.setDescriptor(r.toString());
388 }
389 }
390
391
392 if (webApp.getDescriptor() == null && (webAppSourceDirectory != null))
393 {
394 File f = new File (new File (webAppSourceDirectory, "WEB-INF"), "web.xml");
395 if (f.exists() && f.isFile())
396 {
397 webApp.setDescriptor(f.getCanonicalPath());
398 }
399 }
400 }
401 getLog().info( "web.xml file = "+webApp.getDescriptor());
402 getLog().info("Webapp directory = " + webAppSourceDirectory.getCanonicalPath());
403 }
404
405
406
407
408
409
410
411 public void configureScanner ()
412 throws MojoExecutionException
413 {
414
415 scanList = new ArrayList<File>();
416 if (webApp.getDescriptor() != null)
417 {
418 try (Resource r = Resource.newResource(webApp.getDescriptor());)
419 {
420 scanList.add(r.getFile());
421 }
422 catch (IOException e)
423 {
424 throw new MojoExecutionException("Problem configuring scanner for web.xml", e);
425 }
426 }
427
428 if (webApp.getJettyEnvXml() != null)
429 {
430 try (Resource r = Resource.newResource(webApp.getJettyEnvXml());)
431 {
432 scanList.add(r.getFile());
433 }
434 catch (IOException e)
435 {
436 throw new MojoExecutionException("Problem configuring scanner for jetty-env.xml", e);
437 }
438 }
439
440 if (webApp.getDefaultsDescriptor() != null)
441 {
442 try (Resource r = Resource.newResource(webApp.getDefaultsDescriptor());)
443 {
444 if (!WebAppContext.WEB_DEFAULTS_XML.equals(webApp.getDefaultsDescriptor()))
445 scanList.add(r.getFile());
446 }
447 catch (IOException e)
448 {
449 throw new MojoExecutionException("Problem configuring scanner for webdefaults.xml", e);
450 }
451 }
452
453 if (webApp.getOverrideDescriptor() != null)
454 {
455 try (Resource r = Resource.newResource(webApp.getOverrideDescriptor());)
456 {
457 scanList.add(r.getFile());
458 }
459 catch (IOException e)
460 {
461 throw new MojoExecutionException("Problem configuring scanner for webdefaults.xml", e);
462 }
463 }
464
465
466 File jettyWebXmlFile = findJettyWebXmlFile(new File(webAppSourceDirectory,"WEB-INF"));
467 if (jettyWebXmlFile != null)
468 scanList.add(jettyWebXmlFile);
469 scanList.addAll(extraScanTargets);
470 scanList.add(project.getFile());
471 if (webApp.getTestClasses() != null)
472 scanList.add(webApp.getTestClasses());
473 if (webApp.getClasses() != null)
474 scanList.add(webApp.getClasses());
475 scanList.addAll(webApp.getWebInfLib());
476
477 scannerListeners = new ArrayList<Scanner.BulkListener>();
478 scannerListeners.add(new Scanner.BulkListener()
479 {
480 public void filesChanged (List changes)
481 {
482 try
483 {
484 boolean reconfigure = changes.contains(project.getFile().getCanonicalPath());
485 restartWebApp(reconfigure);
486 }
487 catch (Exception e)
488 {
489 getLog().error("Error reconfiguring/restarting webapp after change in watched files",e);
490 }
491 }
492 });
493 }
494
495
496
497
498
499
500
501 public void restartWebApp(boolean reconfigureScanner) throws Exception
502 {
503 getLog().info("restarting "+webApp);
504 getLog().debug("Stopping webapp ...");
505 webApp.stop();
506 getLog().debug("Reconfiguring webapp ...");
507
508 checkPomConfiguration();
509 configureWebApplication();
510
511
512
513 if (reconfigureScanner)
514 {
515 getLog().info("Reconfiguring scanner after change to pom.xml ...");
516 scanList.clear();
517 if (webApp.getDescriptor() != null)
518 scanList.add(new File(webApp.getDescriptor()));
519 if (webApp.getJettyEnvXml() != null)
520 scanList.add(new File(webApp.getJettyEnvXml()));
521 scanList.addAll(extraScanTargets);
522 scanList.add(project.getFile());
523 if (webApp.getTestClasses() != null)
524 scanList.add(webApp.getTestClasses());
525 if (webApp.getClasses() != null)
526 scanList.add(webApp.getClasses());
527 scanList.addAll(webApp.getWebInfLib());
528 scanner.setScanDirs(scanList);
529 }
530
531 getLog().debug("Restarting webapp ...");
532 webApp.start();
533 getLog().info("Restart completed at "+new Date().toString());
534 }
535
536
537
538
539
540
541
542 private List<File> getDependencyFiles ()
543 {
544 List<File> dependencyFiles = new ArrayList<File>();
545 for ( Iterator<Artifact> iter = projectArtifacts.iterator(); iter.hasNext(); )
546 {
547 Artifact artifact = (Artifact) iter.next();
548
549
550 if(artifact.getType().equals("war"))
551 {
552 continue;
553 }
554
555 if (Artifact.SCOPE_PROVIDED.equals(artifact.getScope()))
556 continue;
557
558 if (Artifact.SCOPE_TEST.equals(artifact.getScope()) && !useTestScope)
559 continue;
560
561 dependencyFiles.add(artifact.getFile());
562 getLog().debug( "Adding artifact " + artifact.getFile().getName() + " with scope "+artifact.getScope()+" for WEB-INF/lib " );
563 }
564
565 return dependencyFiles;
566 }
567
568
569
570
571
572
573
574 private List<Artifact> getWarArtifacts ()
575 {
576 if (warArtifacts != null)
577 return warArtifacts;
578
579 warArtifacts = new ArrayList<Artifact>();
580 for ( Iterator<Artifact> iter = projectArtifacts.iterator(); iter.hasNext(); )
581 {
582 Artifact artifact = (Artifact) iter.next();
583 if (artifact.getType().equals("war"))
584 {
585 try
586 {
587 warArtifacts.add(artifact);
588 getLog().info("Dependent war artifact "+artifact.getId());
589 }
590 catch(Exception e)
591 {
592 throw new RuntimeException(e);
593 }
594 }
595 }
596 return warArtifacts;
597 }
598
599
600
601
602
603
604
605
606 protected Artifact getArtifactForOverlay (OverlayConfig o, List<Artifact> warArtifacts)
607 {
608 if (o == null || warArtifacts == null || warArtifacts.isEmpty())
609 return null;
610
611 for (Artifact a:warArtifacts)
612 {
613 if (o.matchesArtifact (a.getGroupId(), a.getArtifactId(), a.getClassifier()))
614 {
615 return a;
616 }
617 }
618
619 return null;
620 }
621 }