Skip to content

Commit a1f3057

Browse files
committed
Gradle plugin: use full URI for configuration source locations
`io.quarkus.gradle.tasks.EffectiveConfig.CombinedConfigSourceProvider` passes only the "file extension" (e.g. `application.properties`) down to `io.smallrye.config.AbstractLocationConfigSourceLoader#loadConfigSources(java.lang.String[], int, java.lang.ClassLoader)`, which may let that function behave wrong and try to for example access an `application.properties` in the wrong location. This can be reproduced by placing an `application.properties` file in the project directory of a Gradle project that uses the Quarkus Gradle plugin. This change fixes this behavior by passing down the correct locations as the `String` representation of the resource URIs, instead of just the "file extensions". Fixes #36767
1 parent 3149ece commit a1f3057

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

devtools/gradle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/EffectiveConfig.java

+26-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.net.MalformedURLException;
88
import java.net.URL;
99
import java.net.URLClassLoader;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
1012
import java.util.ArrayList;
1113
import java.util.HashMap;
1214
import java.util.List;
@@ -139,21 +141,26 @@ List<URL> applicationPropsSources() {
139141

140142
static void configSourcesForApplicationProperties(Set<File> sourceDirectories, Consumer<URL> sourceUrls,
141143
Consumer<ConfigSource> configSourceConsumer, int ordinal, String[] fileExtensions) {
142-
URL[] resourceUrls = sourceDirectories.stream().map(File::toURI)
143-
.map(u -> {
144-
try {
145-
return u.toURL();
146-
} catch (MalformedURLException e) {
147-
throw new RuntimeException(e);
148-
}
149-
})
150-
.toArray(URL[]::new);
151-
152-
for (URL resourceUrl : resourceUrls) {
153-
URLClassLoader classLoader = new URLClassLoader(new URL[] { resourceUrl });
154-
CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider(sourceUrls, ordinal,
155-
fileExtensions);
156-
configSourceProvider.getConfigSources(classLoader).forEach(configSourceConsumer);
144+
for (var sourceDir : sourceDirectories) {
145+
var sourceDirPath = sourceDir.toPath();
146+
var locations = new ArrayList<String>();
147+
for (String file : fileExtensions) {
148+
Path resolved = sourceDirPath.resolve(file);
149+
if (Files.exists(resolved)) {
150+
locations.add(resolved.toUri().toString());
151+
}
152+
}
153+
if (!locations.isEmpty()) {
154+
URLClassLoader classLoader;
155+
try {
156+
classLoader = new URLClassLoader(new URL[] { sourceDir.toURI().toURL() });
157+
} catch (MalformedURLException e) {
158+
throw new RuntimeException(e);
159+
}
160+
CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider(sourceUrls, ordinal,
161+
fileExtensions, locations);
162+
configSourceProvider.getConfigSources(classLoader).forEach(configSourceConsumer);
163+
}
157164
}
158165
}
159166

@@ -204,11 +211,13 @@ static final class CombinedConfigSourceProvider extends AbstractLocationConfigSo
204211
private final Consumer<URL> sourceUrls;
205212
private final int ordinal;
206213
private final String[] fileExtensions;
214+
private final List<String> locations;
207215

208-
CombinedConfigSourceProvider(Consumer<URL> sourceUrls, int ordinal, String[] fileExtensions) {
216+
CombinedConfigSourceProvider(Consumer<URL> sourceUrls, int ordinal, String[] fileExtensions, List<String> locations) {
209217
this.sourceUrls = sourceUrls;
210218
this.ordinal = ordinal;
211219
this.fileExtensions = fileExtensions;
220+
this.locations = locations;
212221
}
213222

214223
@Override
@@ -225,8 +234,7 @@ protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws
225234

226235
@Override
227236
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
228-
// Note:
229-
return loadConfigSources(getFileExtensions(), ordinal, classLoader);
237+
return loadConfigSources(locations.toArray(new String[0]), ordinal, classLoader);
230238
}
231239
}
232240
}

0 commit comments

Comments
 (0)