Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore config ServiceLoader files from Gradle sources #42650

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import static java.util.Collections.unmodifiableMap;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -75,7 +78,6 @@ private EffectiveConfig(Builder builder) {
.withMapping(PackageConfig.class)
.withMapping(NativeConfig.class)
.withInterceptors(ConfigCompatibility.FrontEnd.instance(), ConfigCompatibility.BackEnd.instance())
.setAddDiscoveredSecretKeysHandlers(false)
.build();
this.values = generateFullConfigMap(config);
}
Expand Down Expand Up @@ -168,6 +170,17 @@ Builder withProfile(String profile) {
}
}

/**
* Builds a specific {@link ClassLoader} for {@link SmallRyeConfig} to include potential configuration files in
* the application source paths. The {@link ClassLoader} excludes the path <code>META-INF/services</code> because
* in most cases, the ServiceLoader files will reference service implementations that are not yet compiled. It is
* possible that the service files reference implementations from dependencies, which are valid and, in this case,
* wrongly excluded, but most likely only required for the application and not the Gradle build. We will rewrite
* the implementation to cover that case if this becomes an issue.
*
* @param sourceDirectories a Set of source directories specified by the Gradle build.
* @return a {@link ClassLoader} with the source paths
*/
private static ClassLoader toUrlClassloader(Set<File> sourceDirectories) {
List<URL> urls = new ArrayList<>();
for (File sourceDirectory : sourceDirectories) {
Expand All @@ -177,6 +190,23 @@ private static ClassLoader toUrlClassloader(Set<File> sourceDirectories) {
throw new RuntimeException(e);
}
}
return new URLClassLoader(urls.toArray(new URL[0]));

return new URLClassLoader(urls.toArray(new URL[0]), Thread.currentThread().getContextClassLoader()) {
@Override
public URL getResource(String name) {
if (name.startsWith("META-INF/services/")) {
return null;
}
return super.getResource(name);
}

@Override
public Enumeration<URL> getResources(String name) throws IOException {
if (name.startsWith("META-INF/services/")) {
return Collections.emptyEnumeration();
}
return super.getResources(name);
}
};
}
}
Loading