Skip to content

Commit 35b1a59

Browse files
author
Phillip Webb
committed
Fix cli --watch option
Fix SpringApplicationRunner to correctly locate files to watch for reload. Fixes gh-610
1 parent ac4cdd3 commit 35b1a59

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

spring-boot-cli/src/main/java/org/springframework/boot/cli/command/run/SpringApplicationRunner.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818

1919
import java.io.File;
2020
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;
2125
import java.util.concurrent.TimeUnit;
2226
import java.util.logging.Level;
2327

2428
import org.springframework.boot.cli.compiler.GroovyCompiler;
29+
import org.springframework.boot.cli.util.ResourceUtils;
2530

2631
/**
2732
* Compiles Groovy code running the resulting classes using a {@code SpringApplication}.
@@ -83,18 +88,17 @@ public synchronized void compileAndRun() throws Exception {
8388
throw new RuntimeException("No classes found in '" + this.sources + "'");
8489
}
8590

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-
9191
// Start monitoring for changes
9292
if (this.fileWatchThread == null
9393
&& this.configuration.isWatchForFileChanges()) {
9494
this.fileWatchThread = new FileWatchThread();
9595
this.fileWatchThread.start();
9696
}
9797

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();
98102
}
99103
catch (Exception ex) {
100104
if (this.fileWatchThread == null) {
@@ -173,11 +177,13 @@ private class FileWatchThread extends Thread {
173177

174178
private long previous;
175179

180+
private List<File> sources;
181+
176182
public FileWatchThread() {
177183
super("filewatcher-" + (watcherCounter++));
178184
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) {
181187
if (file.exists()) {
182188
long current = file.lastModified();
183189
if (current > this.previous) {
@@ -188,13 +194,32 @@ public FileWatchThread() {
188194
setDaemon(false);
189195
}
190196

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+
191217
@Override
192218
public void run() {
193219
while (true) {
194220
try {
195221
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) {
198223
if (file.exists()) {
199224
long current = file.lastModified();
200225
if (this.previous < current) {

0 commit comments

Comments
 (0)