Skip to content

Instantly share code, notes, and snippets.

@marchof
Created August 29, 2012 18:20
Show Gist options
  • Save marchof/3516547 to your computer and use it in GitHub Desktop.
Save marchof/3516547 to your computer and use it in GitHub Desktop.
Index: jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java
===================================================================
--- jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java (revision 1610)
+++ jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java (working copy)
@@ -13,6 +13,7 @@
import java.io.*;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Locale;
@@ -72,11 +73,18 @@
private String sourceEncoding;
/**
- * File with execution data.
+ * File with execution data. Will be ignored if {@code dataFiles} is specified.
*
* @parameter default-value="${project.build.directory}/jacoco.exec"
*/
private File dataFile;
+
+ /**
+ * A list of files with execution data. When not specified - falls back to {@code dataFile}.
+ *
+ * @parameter
+ */
+ private List<File> dataFiles;
/**
* A list of class files to include in instrumentation/analysis/reports. May
@@ -94,6 +102,14 @@
* @parameter
*/
private List<String> excludes;
+
+ /**
+ * A list of report formats (HTML, XML, CSV) to produce. When not specified - all
+ * formats will be produces.
+ *
+ * @parameter
+ */
+ private List<String> formats;
/**
* Flag used to suppress execution.
@@ -152,6 +168,13 @@
protected Renderer getSiteRenderer() {
return siteRenderer;
}
+
+ protected List<File> getDataFiles() {
+ if (dataFiles == null || dataFiles.isEmpty()) {
+ return Collections.singletonList(dataFile);
+ }
+ return dataFiles;
+ }
protected List<String> getIncludes() {
return includes;
@@ -204,12 +227,7 @@
@Override
protected void executeReport(Locale locale) throws MavenReportException {
- try {
- loadExecutionData();
- } catch (final IOException e) {
- getLog().error(
- "Unable to read execution data file " + dataFile + ": "
- + e.getMessage(), e);
+ if (!loadExecutionData()) {
return;
}
try {
@@ -223,21 +241,44 @@
}
}
- private void loadExecutionData() throws IOException {
+ private boolean loadExecutionData() {
sessionInfoStore = new SessionInfoStore();
executionDataStore = new ExecutionDataStore();
- FileInputStream in = null;
- try {
- in = new FileInputStream(dataFile);
- final ExecutionDataReader reader = new ExecutionDataReader(in);
- reader.setSessionInfoVisitor(sessionInfoStore);
- reader.setExecutionDataVisitor(executionDataStore);
- reader.read();
- } finally {
- if (in != null) {
- in.close();
+
+ int numFilesMerged = 0;
+ for (File executionFile : getDataFiles()) {
+ if (numFilesMerged == 0) {
+ getLog().info("Loading " + executionFile);
}
+ else {
+ getLog().info("Merging " + executionFile);
+ }
+ FileInputStream in = null;
+ try {
+ in = new FileInputStream(executionFile);
+ final ExecutionDataReader reader = new ExecutionDataReader(in);
+ reader.setSessionInfoVisitor(sessionInfoStore);
+ reader.setExecutionDataVisitor(executionDataStore);
+ reader.read();
+ numFilesMerged++;
+ }
+ catch (final IOException e) {
+ getLog().error("Unable to read execution data file " + executionFile + ": "
+ + e.getMessage(), e);
+ }
+ finally {
+ if (in != null) {
+ try {
+ in.close();
+ }
+ catch (final IOException e) {
+ getLog().error("Failed to close execution data file " + executionFile + ": "
+ + e.getMessage(), e);
+ }
+ }
+ }
}
+ return numFilesMerged > 0;
}
private void createReport(final IReportGroupVisitor visitor)
@@ -277,25 +318,47 @@
outputDirectory.mkdirs();
- final XMLFormatter xmlFormatter = new XMLFormatter();
- xmlFormatter.setOutputEncoding(outputEncoding);
- visitors.add(xmlFormatter.createVisitor(new FileOutputStream(new File(
- outputDirectory, "jacoco.xml"))));
+ if (isFormatEnabled("XML")) {
+ final XMLFormatter xmlFormatter = new XMLFormatter();
+ xmlFormatter.setOutputEncoding(outputEncoding);
+ visitors.add(xmlFormatter.createVisitor(new FileOutputStream(new File(
+ outputDirectory, "jacoco.xml"))));
+ }
- final CSVFormatter formatter = new CSVFormatter();
- formatter.setOutputEncoding(outputEncoding);
- visitors.add(formatter.createVisitor(new FileOutputStream(new File(
- outputDirectory, "jacoco.csv"))));
+ if (isFormatEnabled("CSV")) {
+ final CSVFormatter formatter = new CSVFormatter();
+ formatter.setOutputEncoding(outputEncoding);
+ visitors.add(formatter.createVisitor(new FileOutputStream(new File(
+ outputDirectory, "jacoco.csv"))));
+ }
- final HTMLFormatter htmlFormatter = new HTMLFormatter();
- // formatter.setFooterText(footer);
- htmlFormatter.setOutputEncoding(outputEncoding);
- // formatter.setLocale(locale);
- visitors.add(htmlFormatter.createVisitor(new FileMultiReportOutput(
- outputDirectory)));
+ if (isFormatEnabled("HTML")) {
+ final HTMLFormatter htmlFormatter = new HTMLFormatter();
+ // formatter.setFooterText(footer);
+ htmlFormatter.setOutputEncoding(outputEncoding);
+ // formatter.setLocale(locale);
+ visitors.add(htmlFormatter.createVisitor(new FileMultiReportOutput(
+ outputDirectory)));
+ }
+
+ if (visitors.isEmpty()) {
+ getLog().error("No valid report format specified");
+ }
return new MultiReportVisitor(visitors);
}
+
+ private boolean isFormatEnabled(String format) {
+ if (formats == null || formats.isEmpty()) {
+ return true;
+ }
+ for (String fmt : formats) {
+ if (format.compareToIgnoreCase(fmt) == 0) {
+ return true;
+ }
+ }
+ return false;
+ }
private static class SourceFileCollection implements ISourceFileLocator {
Index: jacoco-maven-plugin.test/it/it-report-with-format/verify.bsh
===================================================================
--- jacoco-maven-plugin.test/it/it-report-with-format/verify.bsh (revision 0)
+++ jacoco-maven-plugin.test/it/it-report-with-format/verify.bsh (revision 0)
@@ -0,0 +1,28 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Evgeny Mandrikov - initial API and implementation
+ *
+ *******************************************************************************/
+import java.io.*;
+import org.codehaus.plexus.util.*;
+
+File xmlReportFile = new File( basedir, "target/site/jacoco/jacoco.xml" );
+if ( !xmlReportFile.isFile() ) {
+ throw new RuntimeException( "XML report was not created" );
+}
+
+File htmlReportFile = new File( basedir, "target/site/jacoco/index.html" );
+if ( htmlReportFile.exists() ) {
+ throw new RuntimeException( "HTML report was created" );
+}
+
+File csvReportFile = new File( basedir, "target/site/jacoco/jacoco.csv" );
+if ( csvReportFile.exists() ) {
+ throw new RuntimeException( "CSV report was created" );
+}
Index: jacoco-maven-plugin.test/it/it-report-with-format/src/test/java/ExampleTest.java
===================================================================
--- jacoco-maven-plugin.test/it/it-report-with-format/src/test/java/ExampleTest.java (revision 0)
+++ jacoco-maven-plugin.test/it/it-report-with-format/src/test/java/ExampleTest.java (revision 0)
@@ -0,0 +1,21 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Evgeny Mandrikov - initial API and implementation
+ *
+ *******************************************************************************/
+import org.junit.Test;
+
+public class ExampleTest {
+
+ @Test
+ public void test() {
+ new Example().sayHello();
+ }
+
+}
Index: jacoco-maven-plugin.test/it/it-report-with-format/src/main/java/Example.java
===================================================================
--- jacoco-maven-plugin.test/it/it-report-with-format/src/main/java/Example.java (revision 0)
+++ jacoco-maven-plugin.test/it/it-report-with-format/src/main/java/Example.java (revision 0)
@@ -0,0 +1,18 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Evgeny Mandrikov - initial API and implementation
+ *
+ *******************************************************************************/
+public class Example {
+
+ public void sayHello() {
+ System.out.println("Hello world");
+ }
+
+}
Index: jacoco-maven-plugin.test/it/it-report-with-format/pom.xml
===================================================================
--- jacoco-maven-plugin.test/it/it-report-with-format/pom.xml (revision 0)
+++ jacoco-maven-plugin.test/it/it-report-with-format/pom.xml (revision 0)
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
+ All rights reserved. This program and the accompanying materials
+ are made available under the terms of the Eclipse Public License v1.0
+ which accompanies this distribution, and is available at
+ http://www.eclipse.org/legal/epl-v10.html
+
+ Contributors:
+ Evgeny Mandrikov - 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-without-debug</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>
+ <phase>prepare-package</phase>
+ <goals>
+ <goal>report</goal>
+ </goals>
+ <configuration>
+ <dataFiles>
+ <dataFile>${project.build.directory}/jacoco.exec</dataFile>
+ </dataFiles>
+ <formats>
+ <format>xml</format>
+ </formats>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment