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