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.util.List;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.eclipse.jetty.util.PathWatcher;
27 import org.eclipse.jetty.util.PathWatcher.PathWatchEvent;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class JettyRunWarExplodedMojo extends AbstractJettyMojo
48 {
49
50
51
52
53
54
55
56
57
58 private File war;
59
60
61
62
63
64
65
66
67 public void execute () throws MojoExecutionException, MojoFailureException
68 {
69 super.execute();
70 }
71
72
73
74 @Override
75 public void finishConfigurationBeforeStart() throws Exception
76 {
77 server.setStopAtShutdown(true);
78 super.finishConfigurationBeforeStart();
79 }
80
81
82
83
84
85
86 public void checkPomConfiguration() throws MojoExecutionException
87 {
88 return;
89 }
90
91
92
93
94
95
96
97 public void configureScanner() throws MojoExecutionException
98 {
99 scanner.watch(project.getFile().toPath());
100 File webInfDir = new File(war,"WEB-INF");
101 scanner.watch(new File(webInfDir, "web.xml").toPath());
102 File jettyWebXmlFile = findJettyWebXmlFile(webInfDir);
103 if (jettyWebXmlFile != null)
104 scanner.watch(jettyWebXmlFile.toPath());
105 File jettyEnvXmlFile = new File(webInfDir, "jetty-env.xml");
106 if (jettyEnvXmlFile.exists())
107 scanner.watch(jettyEnvXmlFile.toPath());
108
109 PathWatcher.Config classesConfig = new PathWatcher.Config(new File(webInfDir, "classes").toPath());
110 classesConfig.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
111 scanner.watch(classesConfig);
112
113 PathWatcher.Config libConfig = new PathWatcher.Config(new File(webInfDir, "lib").toPath());
114 libConfig.setRecurseDepth(PathWatcher.Config.UNLIMITED_DEPTH);
115 scanner.watch(libConfig);
116
117 scanner.addListener(new PathWatcher.EventListListener()
118 {
119
120 @Override
121 public void onPathWatchEvents(List<PathWatchEvent> events)
122 {
123 try
124 {
125 boolean reconfigure = false;
126 for (PathWatchEvent e:events)
127 {
128 if (e.getPath().equals(project.getFile().toPath()))
129 {
130 reconfigure = true;
131 break;
132 }
133 }
134 restartWebApp(reconfigure);
135 }
136 catch (Exception e)
137 {
138 getLog().error("Error reconfiguring/restarting webapp after change in watched files",e);
139 }
140 }
141 });
142 }
143
144
145
146
147
148
149
150 public void restartWebApp(boolean reconfigureScanner) throws Exception
151 {
152 getLog().info("Restarting webapp");
153 getLog().debug("Stopping webapp ...");
154 stopScanner();
155 webApp.stop();
156 getLog().debug("Reconfiguring webapp ...");
157
158 checkPomConfiguration();
159
160
161
162 if (reconfigureScanner)
163 {
164 getLog().info("Reconfiguring scanner after change to pom.xml ...");
165 scanner.reset();
166 configureScanner();
167 }
168
169 getLog().debug("Restarting webapp ...");
170 webApp.start();
171 startScanner();
172 getLog().info("Restart completed.");
173 }
174
175
176
177
178
179
180
181 public void configureWebApplication () throws Exception
182 {
183 super.configureWebApplication();
184 webApp.setWar(war.getCanonicalPath());
185 }
186 }