1
1
package org .openapitools .openapidiff .maven ;
2
2
3
+ import static org .openapitools .openapidiff .core .utils .FileUtils .writeToFile ;
4
+
3
5
import java .io .ByteArrayOutputStream ;
6
+ import java .io .IOException ;
4
7
import java .io .OutputStreamWriter ;
8
+ import java .io .UncheckedIOException ;
5
9
import org .apache .maven .plugin .AbstractMojo ;
6
10
import org .apache .maven .plugin .MojoExecutionException ;
7
11
import org .apache .maven .plugin .MojoFailureException ;
11
15
import org .openapitools .openapidiff .core .OpenApiCompare ;
12
16
import org .openapitools .openapidiff .core .model .ChangedOpenApi ;
13
17
import org .openapitools .openapidiff .core .output .ConsoleRender ;
18
+ import org .openapitools .openapidiff .core .output .JsonRender ;
19
+ import org .openapitools .openapidiff .core .output .MarkdownRender ;
14
20
15
21
/** A Maven Mojo that diffs two OpenAPI specifications and reports on differences. */
16
22
@ Mojo (name = "diff" , defaultPhase = LifecyclePhase .TEST )
17
23
public class OpenApiDiffMojo extends AbstractMojo {
24
+
18
25
@ Parameter (property = "oldSpec" )
19
26
String oldSpec ;
20
27
@@ -30,6 +37,15 @@ public class OpenApiDiffMojo extends AbstractMojo {
30
37
@ Parameter (property = "skip" , defaultValue = "false" )
31
38
Boolean skip = false ;
32
39
40
+ @ Parameter (property = "consoleOutputFileName" )
41
+ String consoleOutputFileName ;
42
+
43
+ @ Parameter (property = "jsonOutputFileName" )
44
+ String jsonOutputFileName ;
45
+
46
+ @ Parameter (property = "markdownOutputFileName" )
47
+ String markdownOutputFileName ;
48
+
33
49
@ Override
34
50
public void execute () throws MojoExecutionException , MojoFailureException {
35
51
if (Boolean .TRUE .equals (skip )) {
@@ -39,10 +55,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
39
55
40
56
try {
41
57
final ChangedOpenApi diff = OpenApiCompare .fromLocations (oldSpec , newSpec );
42
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
43
- OutputStreamWriter outputStreamWriter = new OutputStreamWriter (outputStream );
44
- new ConsoleRender ().render (diff , outputStreamWriter );
45
- getLog ().info (outputStream .toString ());
58
+
59
+ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
60
+ OutputStreamWriter outputStreamWriter = new OutputStreamWriter (outputStream )) {
61
+ new ConsoleRender ().render (diff , outputStreamWriter );
62
+ getLog ().info (outputStream .toString ());
63
+ } catch (IOException e ) {
64
+ throw new UncheckedIOException (e );
65
+ }
66
+
67
+ writeDiffAsTextToFile (diff );
68
+ writeDiffAsJsonToFile (diff );
69
+ writeDiffAsMarkdownToFile (diff );
46
70
47
71
if (failOnIncompatible && diff .isIncompatible ()) {
48
72
throw new BackwardIncompatibilityException ("The API changes broke backward compatibility" );
@@ -55,4 +79,16 @@ public void execute() throws MojoExecutionException, MojoFailureException {
55
79
throw new MojoExecutionException ("Unexpected error" , e );
56
80
}
57
81
}
82
+
83
+ private void writeDiffAsTextToFile (final ChangedOpenApi diff ) {
84
+ writeToFile (new ConsoleRender (), diff , consoleOutputFileName );
85
+ }
86
+
87
+ private void writeDiffAsJsonToFile (final ChangedOpenApi diff ) {
88
+ writeToFile (new JsonRender (), diff , jsonOutputFileName );
89
+ }
90
+
91
+ private void writeDiffAsMarkdownToFile (final ChangedOpenApi diff ) {
92
+ writeToFile (new MarkdownRender (), diff , markdownOutputFileName );
93
+ }
58
94
}
0 commit comments