Skip to content

Commit

Permalink
feat: publish openapi spec as artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Jul 3, 2024
1 parent 9a55b3c commit 6980c76
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jetbrainsAnnotation = "24.0.1"
jakarta-ws-rs = "4.0.0"
jupiter = "5.10.1"
mockito = "5.12.0"
swagger = "2.2.21"
swagger = "2.2.22"

[libraries]
assertj-core = { module = "org.assertj:assertj-core", version.ref = "assertj" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ public void beforeResolve(ResolvableDependencies dependencies) {
project.getLogger().debug("{}: use configured version from AutodocExtension (override) [{}]", project.getName(), version);
} else {
artifact += ":+";
project.getLogger().warn("No explicit configuration value for the annotationProcessor version was found. Please supply a configuration for the Autodoc Plugin's annotationProcessor.");
project.getLogger().info("No explicit configuration value for the annotationProcessor version was found. Current one will be used");
}

if (addDependency(project, artifact)) {
var task = project.getTasks().findByName("compileJava");
if ((task instanceof JavaCompile)) {
var compileJava = (JavaCompile) task;
if ((task instanceof JavaCompile compileJava)) {
var versionArg = format("-A%s=%s", VERSION, project.getVersion());
var idArg = format("-A%s=%s:%s", ID, project.getGroup(), project.getName());
var outputArg = format("-A%s=%s", OUTPUTDIR, extension.getOutputDirectory().getOrNull());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public abstract class AutodocExtension {
private boolean includeTransitive = true;

/**
* Overrides the default output directory relative to the current project dir
* Overrides the default output directory relative to the current project dir.
* By default, it is the "build" directory.
*/
public abstract Property<File> getOutputDirectory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import org.gradle.api.publish.PublishingExtension;
import org.gradle.api.publish.maven.MavenPom;
import org.gradle.api.publish.maven.MavenPublication;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.nio.file.Path;

import static org.eclipse.edc.plugins.edcbuild.conventions.ConventionFunctions.requireExtension;
Expand All @@ -50,27 +52,41 @@ public void apply(Project target) {
.filter(p -> p instanceof MavenPublication)
.map(p -> (MavenPublication) p)
.peek(mavenPub -> mavenPub.pom(pom -> setPomInformation(pomExt, target, pom)))
.forEach(mavenPub -> addManifestArtifact(target, mavenPub));
.forEach(mavenPub -> {

addArtifactIfExist(target, getManifestFile(target), mavenPub, artifact -> {
artifact.setClassifier("manifest");
artifact.setType("json");
artifact.builtBy("autodoc");
});

addArtifactIfExist(target, getOpenapiFile(target), mavenPub, artifact -> {
artifact.setClassifier("openapi");
artifact.setType("yaml");
artifact.builtBy("openapi");
});
});
});
}

private Action<ConfigurablePublishArtifact> configureManifestArtifact() {
return artifact -> {
artifact.setClassifier("manifest");
artifact.setType("json");
artifact.builtBy("autodoc");
};
private void addArtifactIfExist(Project project, File location, MavenPublication mavenPublication, Action<ConfigurablePublishArtifact> configureAction) {
if (location.exists()) {
mavenPublication.getArtifacts()
.artifact(project.getArtifacts().add("archives", location, configureAction));
}
}

private void addManifestArtifact(Project target, MavenPublication mavenPub) {
private static @NotNull File getManifestFile(Project target) {
var autodocExt = requireExtension(target, AutodocExtension.class);
var pathToManifest = autodocExt.getOutputDirectory().getOrElse(target.getBuildDir()).getAbsolutePath();
var pathToManifest = autodocExt.getOutputDirectory().getOrElse(target.getLayout().getBuildDirectory().getAsFile().get()).getAbsolutePath();
var manifestFileName = "edc.json";
var manifestFile = Path.of(pathToManifest, manifestFileName).toFile();
if (manifestFile.exists()) {
var jsonArtifact = target.getArtifacts().add("archives", manifestFile, configureManifestArtifact());
mavenPub.getArtifacts().artifact(jsonArtifact);
}
return Path.of(pathToManifest, manifestFileName).toFile();
}

private static @NotNull File getOpenapiFile(Project target) {
return target.getLayout().getBuildDirectory().getAsFile().get().toPath()
.resolve("docs").resolve("openapi").resolve("openapi.yaml")
.toFile();
}

private static void setPomInformation(MavenPomExtension pomExt, Project project, MavenPom pom) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,45 @@ public void apply(Project target) {
"jakarta.ws.rs:jakarta.ws.rs-api:%s".formatted(Versions.JAKARTA_WS_RS)
).forEach(dependency -> target.getDependencies().add(IMPLEMENTATION_CONFIGURATION_NAME, dependency));

var javaExt = requireExtension(target, JavaPluginExtension.class);
var classpath = requireExtension(target, JavaPluginExtension.class)
.getSourceSets().getAt("main").getRuntimeClasspath();
var swaggerExt = requireExtension(target, BuildExtension.class).getSwagger();
var fallbackOutputDir = defaultOutputDirectory(target);

var outputFileName = swaggerExt.getOutputFilename().getOrElse(target.getName());

var apiGroup = swaggerExt.getApiGroup().getOrElse(DEFAULT_API_GROUP);
var outputDir = Path.of(swaggerExt.getOutputDirectory().getOrElse(fallbackOutputDir.toFile()).toURI())
.resolve(apiGroup)
.toFile();

var resourcePkgs = swaggerExt.getResourcePackages(); // already provides the default

target.getTasks().withType(ResolveTask.class, task -> {
var outputFileName = swaggerExt.getOutputFilename().getOrElse(target.getName());
var apiGroup = swaggerExt.getApiGroup().getOrElse(DEFAULT_API_GROUP);
var fallbackOutputDir = defaultOutputDirectory(target);

var outputDir = Path.of(swaggerExt.getOutputDirectory().getOrElse(fallbackOutputDir.toFile()).toURI())
.resolve(apiGroup)
.toFile();

task.setOutputFileName(outputFileName);
task.setOutputDir(outputDir);
task.setOutputFormat(ResolveTask.Format.YAML);
task.setSortOutput(true);
task.setPrettyPrint(true);
task.setClasspath(javaExt.getSourceSets().getAt("main").getRuntimeClasspath());
task.setClasspath(classpath);
task.setBuildClasspath(task.getClasspath());
task.setResourcePackages(resourcePkgs);
});

target.getTasks().register("openapi", ResolveTask.class).configure(task -> {
var outputDir = target.getLayout().getBuildDirectory().getAsFile().get().toPath()
.resolve("docs").resolve("openapi")
.toFile();

target.getTasks().findByName("jar").dependsOn(task);
task.setGroup("documentation");
task.setDescription("Generates openapi specification documentation.");
task.setOutputFileName("openapi");
task.setOutputDir(outputDir);
task.setOutputFormat(ResolveTask.Format.YAML);
task.setSortOutput(true);
task.setPrettyPrint(true);
task.setClasspath(classpath);
task.setBuildClasspath(task.getClasspath());
task.setResourcePackages(resourcePkgs);
});
Expand Down

0 comments on commit 6980c76

Please sign in to comment.