Skip to content

Commit

Permalink
Gradle Plugin: Include classes directory in ClassLoader
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Oct 13, 2023
1 parent 4c92823 commit 361eeb8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -15,7 +16,6 @@
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
Expand Down Expand Up @@ -130,7 +130,7 @@ private OpenApiDocument generateSchema(
IndexView index,
FileCollection resourcesSrcDirs) throws IOException {
OpenApiConfig openApiConfig = properties.asOpenApiConfig();
ClassLoader classLoader = getClassLoader(classpath);
ClassLoader classLoader = getClassLoader();

OpenAPI staticModel = generateStaticModel(openApiConfig, resourcesSrcDirs);
OpenAPI annotationModel = generateAnnotationModel(index, openApiConfig, SmallryeOpenApiTask.class.getClassLoader());
Expand Down Expand Up @@ -192,17 +192,22 @@ private void nullSafe(String what, String from, java.util.Optional<Integer> coll
getLogger().debug("Adding {} {} from {}", collection.map(Object::toString).orElse("<no>"), what, from);
}

private ClassLoader getClassLoader(FileCollection config) throws MalformedURLException {
Set<URL> urls = new HashSet<>();
private ClassLoader getClassLoader() {
URL[] locators = Stream.of(classesDirs, classpath)
.map(FileCollection::getFiles)
.flatMap(Collection::stream)
.map(pathEntry -> {
getLogger().debug("Adding {} to annotation scanner class loader", pathEntry);

for (File dependency : config.getFiles()) {
getLogger().debug("Adding {} to annotation scanner class loader", dependency);
urls.add(dependency.toURI().toURL());
}
try {
return pathEntry.toURI().toURL();
} catch (MalformedURLException mue) {
throw new UncheckedIOException(mue);
}
})
.toArray(URL[]::new);

return URLClassLoader.newInstance(
urls.toArray(new URL[0]),
Thread.currentThread().getContextClassLoader());
return URLClassLoader.newInstance(locators, Thread.currentThread().getContextClassLoader());
}

private OpenAPI generateAnnotationModel(IndexView indexView, OpenApiConfig openApiConfig,
Expand All @@ -226,13 +231,12 @@ private OpenAPI generateStaticModel(OpenApiConfig openApiConfig, FileCollection
}

private Path getStaticFile(FileCollection resourcesSrcDirs) {
Path staticFile = resourcesSrcDirs.getFiles()
return resourcesSrcDirs.getFiles()
.stream()
.map(this::getStaticFile)
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
return staticFile;
}

private Path getStaticFile(File dir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,13 @@ void smokeProject(Path buildDir, boolean withQuarkus, String taskName, String ou
" infoLicenseName.set(\"Apache 2.0\")",
" infoLicenseUrl.set(\"http://www.apache.org/licenses/LICENSE-2.0.html\")",
" operationIdStrategy.set(OperationIdStrategy.METHOD)",
" filter.set(\"testcases.CustomOASFilter\")",
" outputFileTypeFilter.set(\"" + outputFileTypeFilter + "\")",
"}"));

Path javaDir = Paths.get("src/main/java/testcases");
Files.createDirectories(buildDir.resolve(javaDir));

Files.write(buildDir.resolve(javaDir.resolve("DummyJaxRs.java")),
asList("package testcases;",
"",
Expand Down Expand Up @@ -237,6 +239,18 @@ void smokeProject(Path buildDir, boolean withQuarkus, String taskName, String ou
" }",
"}"));

Files.write(buildDir.resolve(javaDir.resolve("CustomOASFilter.java")),
asList("package testcases;",
"",
"import org.eclipse.microprofile.openapi.OASFilter;",
"import org.eclipse.microprofile.openapi.models.OpenAPI;",
"",
"public class CustomOASFilter implements OASFilter {",
" public void filterOpenAPI(OpenAPI openAPI) {",
" openAPI.addExtension(\"x-smallrye-gradle-generated\", Boolean.TRUE);",
" }",
"}"));

runGradleTask(buildDir, taskName, withQuarkus);

checkGeneratedFiles(buildDir, outputFileTypeFilter);
Expand Down Expand Up @@ -274,6 +288,7 @@ private static void checkGeneratedFiles(Path buildDir, String expectedOutputFile
targetOpenapiDir.resolve("my-openapi-schema-file.json").toUri().toURL(),
JsonNode.class);
assertThat(root.get("openapi").asText()).isEqualTo("3.0.2");
assertThat(root.get("x-smallrye-gradle-generated").booleanValue()).isTrue();

JsonNode info = root.get("info");
assertThat(info).isNotNull();
Expand Down

0 comments on commit 361eeb8

Please sign in to comment.