Skip to content

Commit

Permalink
refactor: publish openapi artifact with api group as classifier (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt authored Jul 8, 2024
1 parent 8c9baf4 commit 9e6f3b9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,34 @@ public void apply(Project target) {
.map(p -> (MavenPublication) p)
.peek(mavenPub -> mavenPub.pom(pom -> setPomInformation(pomExt, target, pom)))
.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");
});
var openapiFiles = target.getLayout().getBuildDirectory().getAsFile().get().toPath()
.resolve("docs").resolve("openapi").toFile()
.listFiles((dir, name) -> name.endsWith(".yaml"));

if (openapiFiles != null) {
for (var openapiFile : openapiFiles) {
addArtifactIfExist(target, openapiFile, mavenPub, artifact -> {
artifact.setClassifier(getFilenameWithoutExtension(openapiFile));
artifact.setType("yaml");
artifact.builtBy("openapi");
});
}
}

});
});
}

private String getFilenameWithoutExtension(File openapiFile) {
return openapiFile.getName().substring(0, openapiFile.getName().lastIndexOf("."));
}

private void addArtifactIfExist(Project project, File location, MavenPublication mavenPublication, Action<ConfigurablePublishArtifact> configureAction) {
if (location.exists()) {
mavenPublication.getArtifacts()
Expand All @@ -83,12 +95,6 @@ private void addArtifactIfExist(Project project, File location, MavenPublication
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) {
// these properties are mandatory!
var projectName = pomExt.getProjectName().getOrElse(project.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public void apply(Project target) {

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 apiGroup = swaggerExt.getApiGroup().getOrElse(DEFAULT_API_GROUP);

var outputDir = Path.of(swaggerExt.getOutputDirectory().getOrElse(fallbackOutputDir.toFile()).toURI())
.resolve(apiGroup)
Expand All @@ -81,7 +81,7 @@ public void apply(Project target) {
target.getTasks().findByName("jar").dependsOn(task);
task.setGroup("documentation");
task.setDescription("Generates openapi specification documentation.");
task.setOutputFileName("openapi");
task.setOutputFileName(swaggerExt.getApiGroup().getOrElse("openapi"));
task.setOutputDir(outputDir);
task.setOutputFormat(ResolveTask.Format.YAML);
task.setSortOutput(true);
Expand Down

0 comments on commit 9e6f3b9

Please sign in to comment.