Skip to content

Commit

Permalink
Add maven parameter to control which reports to generate (#1175)
Browse files Browse the repository at this point in the history
Co-authored-by: troosan <antoine.de.troostembergh@gmail.com>
Co-authored-by: Marc R. Hoffmann <hoffmann@mountainminds.com>
  • Loading branch information
troosan and marchof committed May 1, 2021
1 parent b23461d commit acce036
Show file tree
Hide file tree
Showing 11 changed files with 229 additions and 53 deletions.
52 changes: 52 additions & 0 deletions jacoco-maven-plugin.test/it/it-report-select-formats/pom.xml
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
This program and the accompanying materials are made available under
the terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0
SPDX-License-Identifier: EPL-2.0
Contributors:
Marc R. Hoffmann - initial API and implementation
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>jacoco</groupId>
<artifactId>setup-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>it-report-select-formats</artifactId>

<build>
<plugins>
<plugin>
<groupId>@project.groupId@</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
<configuration>
<formats>
<format>XML</format>
<format>CSV</format>
</formats>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@@ -0,0 +1,15 @@
/*******************************************************************************
* Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
public class Example {

}
@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
import org.junit.Test;

public class ExampleTest {

@Test
public void test() {
new Example();
}

}
28 changes: 28 additions & 0 deletions jacoco-maven-plugin.test/it/it-report-select-formats/verify.bsh
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
import java.io.*;

File htmlReportFile = new File( basedir, "target/site/jacoco/index.html" );
if ( htmlReportFile.isFile() ) {
throw new RuntimeException( "Unexpected HTML report was created" );
}

File xmlReportFile = new File( basedir, "target/site/jacoco/jacoco.xml" );
if ( !xmlReportFile.isFile() ) {
throw new RuntimeException( "XML report was not created" );
}

File csvReportFile = new File( basedir, "target/site/jacoco/jacoco.csv" );
if ( !csvReportFile.isFile() ) {
throw new RuntimeException( "CSV report was not created" );
}
24 changes: 21 additions & 3 deletions jacoco-maven-plugin/src/org/jacoco/maven/AbstractReportMojo.java
Expand Up @@ -8,10 +8,12 @@
*
* Contributors:
* Evgeny Mandrikov - initial API and implementation
* troosan - add support for format selection
*
*******************************************************************************/
package org.jacoco.maven;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -39,6 +41,15 @@ public abstract class AbstractReportMojo extends AbstractMojo
@Parameter(property = "project.reporting.outputEncoding", defaultValue = "UTF-8")
String outputEncoding;

/**
* A list of report formats to generate. Supported formats are HTML, XML and
* CSV. Defaults to all formats if no values are given.
*
* @since 0.8.7
*/
@Parameter(defaultValue = "HTML,XML,CSV")
List<ReportFormat> formats;

/**
* Name of the root node HTML report pages.
*
Expand Down Expand Up @@ -140,6 +151,8 @@ public boolean canGenerateReport() {

abstract boolean canGenerateReportRegardingClassesDirectory();

abstract File getOutputDirectory();

public void generate(
@SuppressWarnings("deprecation") final org.codehaus.doxia.sink.Sink sink,
final Locale locale) throws MavenReportException {
Expand Down Expand Up @@ -186,12 +199,17 @@ private void executeReport(final Locale locale)
}
}

private void addFormatters(final ReportSupport support, final Locale locale)
throws IOException {
getOutputDirectory().mkdirs();
for (final ReportFormat f : formats) {
support.addVisitor(f.createVisitor(this, locale));
}
}

abstract void loadExecutionData(final ReportSupport support)
throws IOException;

abstract void addFormatters(final ReportSupport support,
final Locale locale) throws IOException;

abstract void createReport(final IReportGroupVisitor visitor,
final ReportSupport support) throws IOException;

Expand Down
Expand Up @@ -123,10 +123,8 @@ private void loadExecutionData(final ReportSupport support,
}

@Override
void addFormatters(final ReportSupport support, final Locale locale)
throws IOException {
support.addAllFormatters(outputDirectory, outputEncoding, footer,
locale);
File getOutputDirectory() {
return outputDirectory;
}

@Override
Expand Down
80 changes: 80 additions & 0 deletions jacoco-maven-plugin/src/org/jacoco/maven/ReportFormat.java
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.maven;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;

import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.csv.CSVFormatter;
import org.jacoco.report.html.HTMLFormatter;
import org.jacoco.report.xml.XMLFormatter;

/**
* Configurable output formats for the report goals.
*/
public enum ReportFormat {

/**
* Multi-page html report.
*/
HTML() {
@Override
IReportVisitor createVisitor(final AbstractReportMojo mojo,
final Locale locale) throws IOException {
final HTMLFormatter htmlFormatter = new HTMLFormatter();
htmlFormatter.setOutputEncoding(mojo.outputEncoding);
htmlFormatter.setLocale(locale);
if (mojo.footer != null) {
htmlFormatter.setFooterText(mojo.footer);
}
return htmlFormatter.createVisitor(
new FileMultiReportOutput(mojo.getOutputDirectory()));
}
},

/**
* Single-file XML report.
*/
XML() {
@Override
IReportVisitor createVisitor(final AbstractReportMojo mojo,
final Locale locale) throws IOException {
final XMLFormatter xml = new XMLFormatter();
xml.setOutputEncoding(mojo.outputEncoding);
return xml.createVisitor(new FileOutputStream(
new File(mojo.getOutputDirectory(), "jacoco.xml")));
}
},

/**
* Single-file CSV report.
*/
CSV() {
@Override
IReportVisitor createVisitor(final AbstractReportMojo mojo,
final Locale locale) throws IOException {
final CSVFormatter csv = new CSVFormatter();
csv.setOutputEncoding(mojo.outputEncoding);
return csv.createVisitor(new FileOutputStream(
new File(mojo.getOutputDirectory(), "jacoco.csv")));
}
};

abstract IReportVisitor createVisitor(AbstractReportMojo mojo,
final Locale locale) throws IOException;

}
6 changes: 2 additions & 4 deletions jacoco-maven-plugin/src/org/jacoco/maven/ReportITMojo.java
Expand Up @@ -67,10 +67,8 @@ void loadExecutionData(final ReportSupport support) throws IOException {
}

@Override
void addFormatters(final ReportSupport support, final Locale locale)
throws IOException {
support.addAllFormatters(outputDirectory, outputEncoding, footer,
locale);
File getOutputDirectory() {
return outputDirectory;
}

@Override
Expand Down
6 changes: 2 additions & 4 deletions jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java
Expand Up @@ -62,10 +62,8 @@ void loadExecutionData(final ReportSupport support) throws IOException {
}

@Override
void addFormatters(final ReportSupport support, final Locale locale)
throws IOException {
support.addAllFormatters(outputDirectory, outputEncoding, footer,
locale);
File getOutputDirectory() {
return outputDirectory;
}

@Override
Expand Down
40 changes: 2 additions & 38 deletions jacoco-maven-plugin/src/org/jacoco/maven/ReportSupport.java
Expand Up @@ -17,14 +17,12 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
Expand All @@ -33,17 +31,13 @@
import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportGroupVisitor;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.ISourceFileLocator;
import org.jacoco.report.MultiReportVisitor;
import org.jacoco.report.check.IViolationsOutput;
import org.jacoco.report.check.Rule;
import org.jacoco.report.check.RulesChecker;
import org.jacoco.report.csv.CSVFormatter;
import org.jacoco.report.html.HTMLFormatter;
import org.jacoco.report.xml.XMLFormatter;

/**
* Encapsulates the tasks to create reports for Maven projects. Instances are
Expand Down Expand Up @@ -89,38 +83,8 @@ public void loadExecutionData(final File execFile) throws IOException {
loader.load(execFile);
}

public void addXmlFormatter(final File targetfile, final String encoding)
throws IOException {
final XMLFormatter xml = new XMLFormatter();
xml.setOutputEncoding(encoding);
formatters.add(xml.createVisitor(new FileOutputStream(targetfile)));
}

public void addCsvFormatter(final File targetfile, final String encoding)
throws IOException {
final CSVFormatter csv = new CSVFormatter();
csv.setOutputEncoding(encoding);
formatters.add(csv.createVisitor(new FileOutputStream(targetfile)));
}

public void addHtmlFormatter(final File targetdir, final String encoding,
final String footer, final Locale locale) throws IOException {
final HTMLFormatter htmlFormatter = new HTMLFormatter();
htmlFormatter.setOutputEncoding(encoding);
htmlFormatter.setLocale(locale);
if (footer != null) {
htmlFormatter.setFooterText(footer);
}
formatters.add(htmlFormatter
.createVisitor(new FileMultiReportOutput(targetdir)));
}

public void addAllFormatters(final File targetdir, final String encoding,
final String footer, final Locale locale) throws IOException {
targetdir.mkdirs();
addXmlFormatter(new File(targetdir, "jacoco.xml"), encoding);
addCsvFormatter(new File(targetdir, "jacoco.csv"), encoding);
addHtmlFormatter(targetdir, encoding, footer, locale);
public void addVisitor(final IReportVisitor visitor) {
formatters.add(visitor);
}

public void addRulesChecker(final List<Rule> rules,
Expand Down
3 changes: 3 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -28,6 +28,9 @@ <h3>New Features</h3>
<a href="https://github.com/jacoco/jacoco/issues/1176">#1176</a>).</li>
<li>Experimental support for Java 17 class files
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1132">#1132</a>).</li>
<li>New <code>formats</code> parameter for Maven report goals to specify the
generated report formats. Contributed by troosan.
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1175">#1175</a>).</li>
<li>Branch added by the Kotlin compiler version 1.4.0 and above for "unsafe" cast
operator is filtered out during generation of report
(GitHub <a href="https://github.com/jacoco/jacoco/issues/1143">#1143</a>,
Expand Down

0 comments on commit acce036

Please sign in to comment.