From d532e814d84d474271e98395fdec06ce23e8f43a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Bu=CC=88the?= Date: Sat, 3 Jun 2023 12:44:46 +0200 Subject: [PATCH 1/2] add new parameter `failOnError`, which can be set to fail builds if errors occur (e.g. download fails) --- .../org/springdoc/maven/plugin/SpringDocMojo.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java b/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java index 46f5692..665faf2 100644 --- a/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java +++ b/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java @@ -12,6 +12,7 @@ import java.util.Map; import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; @@ -80,6 +81,12 @@ public class SpringDocMojo extends AbstractMojo { @Parameter(defaultValue = "false", property = "springdoc.skip") private boolean skip; + /** + * Fail build on error, if set to true. Default is false. + */ + @Parameter(defaultValue = "false", property = "springdoc.failOnError") + private boolean failOnError; + /** * Headers to send in request */ @@ -114,7 +121,12 @@ public void execute() { Files.write(Paths.get(outputDir.getAbsolutePath() + "/" + outputFileName), result.getBytes(StandardCharsets.UTF_8)); if (attachArtifact) addArtifactToMaven(); } else { - getLog().error("An error has occurred: Response code " + responseCode); + String message = "An error has occurred: Response code " + responseCode; + if(failOnError) { + throw new MojoFailureException(message); + } else { + getLog().error(message); + } } } catch (Exception e) { getLog().error("An error has occurred", e); From 9d0657fdd407333998bc205b2498b00733c2d249 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Bu=CC=88the?= Date: Mon, 15 Jul 2024 16:24:09 +0200 Subject: [PATCH 2/2] also fail on any exception --- .../java/org/springdoc/maven/plugin/SpringDocMojo.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java b/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java index 665faf2..fa499f0 100644 --- a/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java +++ b/src/main/java/org/springdoc/maven/plugin/SpringDocMojo.java @@ -104,7 +104,7 @@ public class SpringDocMojo extends AbstractMojo { */ private static final String GET = "GET"; - public void execute() { + public void execute() throws MojoFailureException { if (skip) { getLog().info("Skip execution as per configuration"); return; @@ -121,7 +121,7 @@ public void execute() { Files.write(Paths.get(outputDir.getAbsolutePath() + "/" + outputFileName), result.getBytes(StandardCharsets.UTF_8)); if (attachArtifact) addArtifactToMaven(); } else { - String message = "An error has occurred: Response code " + responseCode; + String message = "An error has occurred, response code: " + responseCode; if(failOnError) { throw new MojoFailureException(message); } else { @@ -130,6 +130,9 @@ public void execute() { } } catch (Exception e) { getLog().error("An error has occurred", e); + if(failOnError) { + throw new MojoFailureException("An error has occurred: " + e.getMessage()); + } } }