Skip to content

Commit

Permalink
Gradle plugin: use full URI for configuration source locations
Browse files Browse the repository at this point in the history
`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 quarkusio#36767
  • Loading branch information
snazy committed Oct 31, 2023
1 parent 3149ece commit a1f3057
Showing 1 changed file with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -139,21 +141,26 @@ List<URL> applicationPropsSources() {

static void configSourcesForApplicationProperties(Set<File> sourceDirectories, Consumer<URL> sourceUrls,
Consumer<ConfigSource> configSourceConsumer, int ordinal, String[] fileExtensions) {
URL[] resourceUrls = sourceDirectories.stream().map(File::toURI)
.map(u -> {
try {
return u.toURL();
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
})
.toArray(URL[]::new);

for (URL resourceUrl : resourceUrls) {
URLClassLoader classLoader = new URLClassLoader(new URL[] { resourceUrl });
CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider(sourceUrls, ordinal,
fileExtensions);
configSourceProvider.getConfigSources(classLoader).forEach(configSourceConsumer);
for (var sourceDir : sourceDirectories) {
var sourceDirPath = sourceDir.toPath();
var locations = new ArrayList<String>();
for (String file : fileExtensions) {
Path resolved = sourceDirPath.resolve(file);
if (Files.exists(resolved)) {
locations.add(resolved.toUri().toString());
}
}
if (!locations.isEmpty()) {
URLClassLoader classLoader;
try {
classLoader = new URLClassLoader(new URL[] { sourceDir.toURI().toURL() });
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider(sourceUrls, ordinal,
fileExtensions, locations);
configSourceProvider.getConfigSources(classLoader).forEach(configSourceConsumer);
}
}
}

Expand Down Expand Up @@ -204,11 +211,13 @@ static final class CombinedConfigSourceProvider extends AbstractLocationConfigSo
private final Consumer<URL> sourceUrls;
private final int ordinal;
private final String[] fileExtensions;
private final List<String> locations;

CombinedConfigSourceProvider(Consumer<URL> sourceUrls, int ordinal, String[] fileExtensions) {
CombinedConfigSourceProvider(Consumer<URL> sourceUrls, int ordinal, String[] fileExtensions, List<String> locations) {
this.sourceUrls = sourceUrls;
this.ordinal = ordinal;
this.fileExtensions = fileExtensions;
this.locations = locations;
}

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

@Override
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) {
// Note:
return loadConfigSources(getFileExtensions(), ordinal, classLoader);
return loadConfigSources(locations.toArray(new String[0]), ordinal, classLoader);
}
}
}

0 comments on commit a1f3057

Please sign in to comment.