|
10 | 10 | import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE_PLUS_RUNTIME; |
11 | 11 |
|
12 | 12 | import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
13 | 15 | import java.nio.file.Path; |
14 | 16 | import java.nio.file.Paths; |
| 17 | +import java.nio.file.StandardCopyOption; |
| 18 | +import java.util.ArrayList; |
15 | 19 | import java.util.List; |
| 20 | +import java.util.Locale; |
16 | 21 | import java.util.Map; |
17 | 22 | import java.util.Optional; |
18 | 23 |
|
@@ -53,6 +58,9 @@ public class OpenAPIMojo extends BaseMojo { |
53 | 58 |
|
54 | 59 | @Parameter private List<File> adoc; |
55 | 60 |
|
| 61 | + @Parameter(property = "openAPI.copyOpenApiSpecTo") |
| 62 | + private File copyOpenApiSpecTo; |
| 63 | + |
56 | 64 | @Override |
57 | 65 | protected void doExecute(List<MavenProject> projects, String mainClass) throws Exception { |
58 | 66 | ClassLoader classLoader = createClassLoader(projects); |
@@ -84,10 +92,48 @@ protected void doExecute(List<MavenProject> projects, String mainClass) throws E |
84 | 92 | var result = tool.generate(mainClass); |
85 | 93 |
|
86 | 94 | var adocPath = ofNullable(adoc).orElse(List.of()).stream().map(File::toPath).toList(); |
| 95 | + var written = new ArrayList<Path>(); |
87 | 96 | for (var format : OpenAPIGenerator.Format.values()) { |
88 | | - tool.export(result, format, Map.of("adoc", adocPath)) |
89 | | - .forEach(output -> getLog().info(" writing: " + output)); |
| 97 | + written.addAll(tool.export(result, format, Map.of("adoc", adocPath))); |
| 98 | + } |
| 99 | + written.forEach(output -> getLog().info(" writing: " + output)); |
| 100 | + |
| 101 | + if (copyOpenApiSpecTo != null) { |
| 102 | + var destination = copyOpenApiSpecTo.toPath(); |
| 103 | + copySpec(written, destination); |
| 104 | + getLog().info(" copying: " + destination); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public static void copySpec(List<Path> written, Path destination) throws IOException { |
| 109 | + var format = specFormat(destination); |
| 110 | + var source = |
| 111 | + written.stream() |
| 112 | + .filter(path -> path.getFileName().toString().endsWith("." + format.extension())) |
| 113 | + .findFirst() |
| 114 | + .orElseThrow( |
| 115 | + () -> |
| 116 | + new IOException( |
| 117 | + String.format( |
| 118 | + "OpenAPI %s output not found for copyOpenApiSpecTo: %s", |
| 119 | + format.name(), destination))); |
| 120 | + var parent = destination.getParent(); |
| 121 | + if (parent != null) { |
| 122 | + Files.createDirectories(parent); |
90 | 123 | } |
| 124 | + Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING); |
| 125 | + } |
| 126 | + |
| 127 | + public static OpenAPIGenerator.Format specFormat(Path destination) { |
| 128 | + var name = destination.getFileName().toString().toLowerCase(Locale.ROOT); |
| 129 | + if (name.endsWith(".json")) { |
| 130 | + return OpenAPIGenerator.Format.JSON; |
| 131 | + } |
| 132 | + if (name.endsWith(".yaml") || name.endsWith(".yml")) { |
| 133 | + return OpenAPIGenerator.Format.YAML; |
| 134 | + } |
| 135 | + throw new IllegalArgumentException( |
| 136 | + "copyOpenApiSpecTo must end with .yaml, .yml or .json: " + destination); |
91 | 137 | } |
92 | 138 |
|
93 | 139 | private Optional<String> trim(String value) { |
@@ -186,4 +232,30 @@ public void setJavadoc(String javadoc) { |
186 | 232 | public String getJavadoc() { |
187 | 233 | return javadoc; |
188 | 234 | } |
| 235 | + |
| 236 | + /** |
| 237 | + * Copy the generated OpenAPI spec to the given file. The format is determined by the file |
| 238 | + * extension: <code>.yaml</code>, <code>.yml</code> or <code>.json</code>. |
| 239 | + * |
| 240 | + * @return Destination file. |
| 241 | + */ |
| 242 | + public @Nullable File getCopyOpenApiSpecTo() { |
| 243 | + return copyOpenApiSpecTo; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Copy the generated OpenAPI spec to the given file. The format is determined by the file |
| 248 | + * extension: <code>.yaml</code>, <code>.yml</code> or <code>.json</code>. |
| 249 | + * |
| 250 | + * <p>Example: |
| 251 | + * |
| 252 | + * <pre>{@code |
| 253 | + * <copyOpenApiSpecTo>${project.basedir}/docs/openapi.yaml</copyOpenApiSpecTo> |
| 254 | + * }</pre> |
| 255 | + * |
| 256 | + * @param copyOpenApiSpecTo Destination file. |
| 257 | + */ |
| 258 | + public void setCopyOpenApiSpecTo(@Nullable File copyOpenApiSpecTo) { |
| 259 | + this.copyOpenApiSpecTo = copyOpenApiSpecTo; |
| 260 | + } |
189 | 261 | } |
0 commit comments