Skip to content

Commit

Permalink
feat: publish openapi spec as artifact (#245)
Browse files Browse the repository at this point in the history
* feat: publish openapi spec as artifact

* dependencies
  • Loading branch information
ndr-brt authored Jul 4, 2024
1 parent 9a55b3c commit fdd7e68
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 32 deletions.
6 changes: 3 additions & 3 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.17.1, Apache-2.0, approve
maven/mavencentral/com.fasterxml/classmate/1.3.1, Apache-2.0, approved, CQ10239
maven/mavencentral/com.github.fge/btf/1.2, Apache-2.0 OR LGPL-2.0-or-later, approved, CQ14455
maven/mavencentral/com.github.fge/jackson-coreutils/1.6, Apache-2.0 or LGPL-3.0-only, approved, #2572
maven/mavencentral/com.github.fge/jackson-coreutils/1.8, LGPL-3.0 OR Apache-2.0, approved, clearlydefined
maven/mavencentral/com.github.fge/jackson-coreutils/1.8, (Apache-2.0 AND LGPL-3.0 AND LGPL-3.0-only) OR (Apache-2.0 AND LGPL-3.0-only), restricted, clearlydefined
maven/mavencentral/com.github.fge/json-patch/1.6, Apache-2.0 OR LGPL-3.0-or-later, approved, #10358
maven/mavencentral/com.github.fge/msg-simple/1.1, Apache-2.0 OR LGPL-3.0-or-later, approved, #2574
maven/mavencentral/com.github.fge/uri-template/0.9, Apache-2.0 OR LGPL-3.0-or-later, approved, #9668
maven/mavencentral/com.github.java-json-tools/json-schema-core/1.2.8, Apache-2.0 AND LGPL-2.1-or-later AND LGPL-3.0-only AND (Apache-2.0 AND GPL-1.0-or-later AND LGPL-3.0-only) AND Apache-2.0 AND LGPL-3.0-only, restricted, #15282
maven/mavencentral/com.github.java-json-tools/json-schema-core/1.2.8, Apache-2.0 OR LGPL-3.0-or-later, approved, #15282
maven/mavencentral/com.github.java-json-tools/json-schema-validator/2.2.8, Apache-2.0 OR LGPL-3.0-or-later, approved, CQ20779
maven/mavencentral/com.google.code.findbugs/jsr305/2.0.1, BSD-3-Clause AND CC-BY-2.5 AND LGPL-2.1+, approved, CQ13390
maven/mavencentral/com.google.code.findbugs/jsr305/3.0.1, CC-BY-2.5, approved, #15220
Expand Down Expand Up @@ -54,7 +54,7 @@ maven/mavencentral/io.opentelemetry/opentelemetry-api/1.32.0, Apache-2.0, approv
maven/mavencentral/io.opentelemetry/opentelemetry-context/1.32.0, Apache-2.0, approved, #11683
maven/mavencentral/io.swagger.core.v3/swagger-annotations/2.1.5, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.swagger.core.v3/swagger-core/2.1.5, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.swagger.core.v3/swagger-gradle-plugin/2.2.21, Apache-2.0 AND MIT, approved, #10356
maven/mavencentral/io.swagger.core.v3/swagger-gradle-plugin/2.2.22, Apache-2.0 AND MIT, approved, #10356
maven/mavencentral/io.swagger.core.v3/swagger-models/2.1.5, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.swagger.parser.v3/swagger-parser-core/2.0.23, Apache-2.0, approved, clearlydefined
maven/mavencentral/io.swagger.parser.v3/swagger-parser-v2-converter/2.0.23, Apache-2.0, approved, clearlydefined
Expand Down
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 fdd7e68

Please sign in to comment.