18
18
19
19
import java .io .File ;
20
20
import java .lang .reflect .Method ;
21
+ import java .net .MalformedURLException ;
22
+ import java .net .URL ;
23
+ import java .util .ArrayList ;
24
+ import java .util .List ;
21
25
import java .util .concurrent .TimeUnit ;
22
26
import java .util .logging .Level ;
23
27
24
28
import org .springframework .boot .cli .compiler .GroovyCompiler ;
29
+ import org .springframework .boot .cli .util .ResourceUtils ;
25
30
26
31
/**
27
32
* Compiles Groovy code running the resulting classes using a {@code SpringApplication}.
@@ -83,18 +88,17 @@ public synchronized void compileAndRun() throws Exception {
83
88
throw new RuntimeException ("No classes found in '" + this .sources + "'" );
84
89
}
85
90
86
- // Run in new thread to ensure that the context classloader is setup
87
- this .runThread = new RunThread (compiledSources );
88
- this .runThread .start ();
89
- this .runThread .join ();
90
-
91
91
// Start monitoring for changes
92
92
if (this .fileWatchThread == null
93
93
&& this .configuration .isWatchForFileChanges ()) {
94
94
this .fileWatchThread = new FileWatchThread ();
95
95
this .fileWatchThread .start ();
96
96
}
97
97
98
+ // Run in new thread to ensure that the context classloader is setup
99
+ this .runThread = new RunThread (compiledSources );
100
+ this .runThread .start ();
101
+ this .runThread .join ();
98
102
}
99
103
catch (Exception ex ) {
100
104
if (this .fileWatchThread == null ) {
@@ -173,11 +177,13 @@ private class FileWatchThread extends Thread {
173
177
174
178
private long previous ;
175
179
180
+ private List <File > sources ;
181
+
176
182
public FileWatchThread () {
177
183
super ("filewatcher-" + (watcherCounter ++));
178
184
this .previous = 0 ;
179
- for ( String path : SpringApplicationRunner . this .sources ) {
180
- File file = new File ( path );
185
+ this .sources = getSourceFiles ();
186
+ for ( File file : this . sources ) {
181
187
if (file .exists ()) {
182
188
long current = file .lastModified ();
183
189
if (current > this .previous ) {
@@ -188,13 +194,32 @@ public FileWatchThread() {
188
194
setDaemon (false );
189
195
}
190
196
197
+ private List <File > getSourceFiles () {
198
+ List <File > sources = new ArrayList <File >();
199
+ for (String source : SpringApplicationRunner .this .sources ) {
200
+ List <String > paths = ResourceUtils .getUrls (source ,
201
+ SpringApplicationRunner .this .compiler .getLoader ());
202
+ for (String path : paths ) {
203
+ try {
204
+ URL url = new URL (path );
205
+ if ("file" .equals (url .getProtocol ())) {
206
+ sources .add (new File (url .getFile ()));
207
+ }
208
+ }
209
+ catch (MalformedURLException ex ) {
210
+ // Ignore
211
+ }
212
+ }
213
+ }
214
+ return sources ;
215
+ }
216
+
191
217
@ Override
192
218
public void run () {
193
219
while (true ) {
194
220
try {
195
221
Thread .sleep (TimeUnit .SECONDS .toMillis (1 ));
196
- for (String path : SpringApplicationRunner .this .sources ) {
197
- File file = new File (path );
222
+ for (File file : this .sources ) {
198
223
if (file .exists ()) {
199
224
long current = file .lastModified ();
200
225
if (this .previous < current ) {
0 commit comments