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

Gradle Plugin: Include classes directory in ClassLoader #1613

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
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 @@ -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