diff --git a/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java b/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java index 51de7d93..e86b6d9f 100644 --- a/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java +++ b/src/main/java/org/apache/maven/plugins/checkstyle/AbstractCheckstyleReport.java @@ -31,6 +31,7 @@ import java.util.Map; import com.puppycrawl.tools.checkstyle.DefaultLogger; +import com.puppycrawl.tools.checkstyle.SarifLogger; import com.puppycrawl.tools.checkstyle.XMLLogger; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions; @@ -314,7 +315,7 @@ public abstract class AbstractCheckstyleReport extends AbstractMavenReport { /** * Specifies the format of the output to be used when writing to the output - * file. Valid values are "plain" and "xml". + * file. Valid values are "plain", "sarif" and "xml". */ @Parameter(property = "checkstyle.output.format", defaultValue = "xml") private String outputFileFormat; @@ -619,10 +620,16 @@ protected AuditListener getListener() throws MavenReportException { listener = new XMLLogger(out, OutputStreamOptions.CLOSE); } else if ("plain".equals(outputFileFormat)) { listener = new DefaultLogger(out, OutputStreamOptions.CLOSE); + } else if ("sarif".equals(outputFileFormat)) { + try { + listener = new SarifLogger(out, OutputStreamOptions.CLOSE); + } catch (IOException e) { + throw new MavenReportException("Failed to create SarifLogger", e); + } } else { // TODO: failure if not a report throw new MavenReportException( - "Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'."); + "Invalid output file format: (" + outputFileFormat + "). Must be 'plain', 'sarif' or 'xml'."); } } diff --git a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java index d6e3cb23..14c8443b 100644 --- a/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java +++ b/src/main/java/org/apache/maven/plugins/checkstyle/CheckstyleViolationCheckMojo.java @@ -34,6 +34,7 @@ import java.util.Map; import com.puppycrawl.tools.checkstyle.DefaultLogger; +import com.puppycrawl.tools.checkstyle.SarifLogger; import com.puppycrawl.tools.checkstyle.XMLLogger; import com.puppycrawl.tools.checkstyle.api.AuditListener; import com.puppycrawl.tools.checkstyle.api.AutomaticBean.OutputStreamOptions; @@ -92,7 +93,7 @@ public class CheckstyleViolationCheckMojo extends AbstractMojo { /** * Specifies the format of the output to be used when writing to the output - * file. Valid values are "plain" and "xml". + * file. Valid values are "plain", "sarif" and "xml". */ @Parameter(property = "checkstyle.output.format", defaultValue = "xml") private String outputFileFormat; @@ -802,6 +803,21 @@ private AuditListener getListener() throws MojoFailureException, MojoExecutionEx } catch (IOException e) { throw new MojoExecutionException("Unable to create temporary file", e); } + } else if ("sarif".equals(outputFileFormat)) { + try { + // Write a sarif output file to the standard output file, + // and write an XML output file to the temp directory that can be used to count violations + outputXmlFile = + Files.createTempFile("checkstyle-result", ".xml").toFile(); + outputXmlFile.deleteOnExit(); + OutputStream xmlOut = getOutputStream(outputXmlFile); + CompositeAuditListener compoundListener = new CompositeAuditListener(); + compoundListener.addListener(new XMLLogger(xmlOut, OutputStreamOptions.CLOSE)); + compoundListener.addListener(new SarifLogger(out, OutputStreamOptions.CLOSE)); + listener = compoundListener; + } catch (IOException e) { + throw new MojoExecutionException("Unable to create temporary file", e); + } } else { throw new MojoFailureException( "Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'.");