Skip to content

Commit

Permalink
Ignore config ServiceLoader files from Gradle sources
Browse files Browse the repository at this point in the history
(cherry picked from commit 778834e)
  • Loading branch information
radcortez authored and gsmet committed Aug 20, 2024
1 parent 013fd6e commit 6f36035
Showing 1 changed file with 32 additions and 2 deletions.
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);
}
};
}
}

0 comments on commit 6f36035

Please sign in to comment.