Skip to content

Commit b5db4d3

Browse files
author
Dave Syer
committed
Enhance FileOptions to support multiple classpath resources
1 parent cd7b1b1 commit b5db4d3

File tree

1 file changed

+26
-12
lines changed
  • spring-boot-cli/src/main/java/org/springframework/boot/cli/command

1 file changed

+26
-12
lines changed

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
package org.springframework.boot.cli.command;
1818

1919
import java.io.File;
20+
import java.io.IOException;
2021
import java.net.URL;
2122
import java.util.ArrayList;
23+
import java.util.Arrays;
2224
import java.util.Collections;
25+
import java.util.Enumeration;
2326
import java.util.List;
2427

2528
import joptsimple.OptionSet;
@@ -69,11 +72,11 @@ public FileOptions(OptionSet optionSet, ClassLoader classLoader,
6972
break;
7073
}
7174
if (filename.endsWith(".groovy") || filename.endsWith(".java")) {
72-
File file = getFile(filename, classLoader);
73-
if (file == null) {
75+
List<File> file = getFiles(filename, classLoader);
76+
if (file.isEmpty()) {
7477
throw new IllegalArgumentException("Can't find " + filename);
7578
}
76-
files.add(file);
79+
files.addAll(file);
7780
}
7881
}
7982
}
@@ -84,27 +87,38 @@ public FileOptions(OptionSet optionSet, ClassLoader classLoader,
8487
throw new RuntimeException("Please specify at least one file to run");
8588
}
8689
for (String path : defaultPaths) {
87-
File file = getFile(path, classLoader);
88-
if (file != null && file.exists()) {
89-
files.add(file);
90+
for (File file : getFiles(path, classLoader)) {
91+
if (file != null && file.exists()) {
92+
files.add(file);
93+
}
9094
}
9195
}
9296
}
9397
this.files = Collections.unmodifiableList(files);
9498
}
9599

96-
private File getFile(String filename, ClassLoader classLoader) {
100+
private List<File> getFiles(String filename, ClassLoader classLoader) {
97101
File file = new File(filename);
98102
if (file.isFile() && file.canRead()) {
99-
return file;
103+
return Arrays.asList(file);
100104
}
105+
List<File> result = new ArrayList<File>();
101106
if (classLoader != null) {
102-
URL url = classLoader.getResource(filename);
103-
if (url != null && url.toString().startsWith("file:")) {
104-
return new File(url.toString().substring("file:".length()));
107+
Enumeration<URL> urls;
108+
try {
109+
urls = classLoader.getResources(filename);
110+
while (urls.hasMoreElements()) {
111+
URL url = urls.nextElement();
112+
if (url != null && url.toString().startsWith("file:")) {
113+
result.add(new File(url.toString().substring("file:".length())));
114+
}
115+
}
116+
}
117+
catch (IOException e) {
118+
// Ignore
105119
}
106120
}
107-
return null;
121+
return result;
108122
}
109123

110124
public List<?> getArgs() {

0 commit comments

Comments
 (0)