Trac #160: Maven plug-in : respect includes/excludes properties in Report Mojo
diff --git a/jacoco-maven-plugin.test/it/it-customize-agent/pom.xml b/jacoco-maven-plugin.test/it/it-customize-agent/pom.xml
index 178d36a..940bb67 100644
--- a/jacoco-maven-plugin.test/it/it-customize-agent/pom.xml
+++ b/jacoco-maven-plugin.test/it/it-customize-agent/pom.xml
@@ -26,7 +26,7 @@
     <jacoco.destFile>${project.build.directory}/coverage.exec</jacoco.destFile>
     <jacoco.append>false</jacoco.append>
     <jacoco.includes>*</jacoco.includes>
-    <jacoco.excludes>java.*</jacoco.excludes>
+    <jacoco.excludes>java.*,sun.*</jacoco.excludes>
     <jacoco.exclClassLoaders>sun.reflect.DelegatingClassLoader:MyClassLoader</jacoco.exclClassLoaders>
     <jacoco.sessionId>session</jacoco.sessionId>
     <jacoco.dumpOnExit>true</jacoco.dumpOnExit>
diff --git a/jacoco-maven-plugin.test/it/it-customize-agent/verify.bsh b/jacoco-maven-plugin.test/it/it-customize-agent/verify.bsh
index c96ea5b..f9579dc 100644
--- a/jacoco-maven-plugin.test/it/it-customize-agent/verify.bsh
+++ b/jacoco-maven-plugin.test/it/it-customize-agent/verify.bsh
@@ -15,7 +15,7 @@
 String agentOptions = "destfile=" + basedir + File.separator + "target" + File.separator + "coverage.exec"
     + ",append=false"
     + ",includes=*"
-    + ",excludes=java.*"
+    + ",excludes=java.*:sun.*"
     + ",exclclassloader=sun.reflect.DelegatingClassLoader:MyClassLoader"
     + ",sessionid=session"
     + ",dumponexit=true"
diff --git a/jacoco-maven-plugin.test/it/it-includes-excludes/pom.xml b/jacoco-maven-plugin.test/it/it-includes-excludes/pom.xml
new file mode 100644
index 0000000..7814009
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-includes-excludes/pom.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   Copyright (c) 2009, 2011 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-includes-excludes</artifactId>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>@project.groupId@</groupId>
+        <artifactId>jacoco-maven-plugin</artifactId>
+        <configuration>
+          <excludes>
+            <exclude>**/FileUtil*</exclude>
+            <exclude>**/TestUtil*</exclude>
+          </excludes>
+        </configuration>
+        <executions>
+          <execution>
+            <goals>
+              <goal>prepare-agent</goal>
+            </goals>
+          </execution>
+          <execution>
+            <id>report</id>
+            <phase>prepare-package</phase>
+            <goals>
+              <goal>report</goal>
+            </goals>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/DatabaseUtil.java b/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/DatabaseUtil.java
new file mode 100644
index 0000000..4dc1975
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/DatabaseUtil.java
@@ -0,0 +1,19 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2011 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
+ *
+ *******************************************************************************/
+package org.project;
+
+
+public class DatabaseUtil {
+  public void doStuff() {
+    System.out.println("Do DatabaseUtil Stuff");
+  }
+}
diff --git a/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/FileUtil.java b/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/FileUtil.java
new file mode 100644
index 0000000..d2c205a
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/FileUtil.java
@@ -0,0 +1,19 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2011 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
+ *
+ *******************************************************************************/
+package org.project;
+
+
+public class FileUtil {
+  public void doStuff() {
+    System.out.println("Do FileUtil Stuff");
+  }
+}
diff --git a/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/TestUtil.java b/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/TestUtil.java
new file mode 100644
index 0000000..06c0bd0
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-includes-excludes/src/main/java/org/project/TestUtil.java
@@ -0,0 +1,19 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2011 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
+ *
+ *******************************************************************************/
+package org.project;
+
+
+public class TestUtil {
+  public void doStuff() {
+    System.out.println("Do TestUtil Stuff");
+  }
+}
diff --git a/jacoco-maven-plugin.test/it/it-includes-excludes/src/test/java/org/project/TestDatabaseUtil.java b/jacoco-maven-plugin.test/it/it-includes-excludes/src/test/java/org/project/TestDatabaseUtil.java
new file mode 100644
index 0000000..46a2b81
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-includes-excludes/src/test/java/org/project/TestDatabaseUtil.java
@@ -0,0 +1,21 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2011 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
+ *
+ *******************************************************************************/
+package org.project;
+
+import org.junit.Test;
+
+public class TestDatabaseUtil {
+  @Test
+  public void testDoStuff() {
+    new DatabaseUtil().doStuff();
+  }
+}
diff --git a/jacoco-maven-plugin.test/it/it-includes-excludes/verify.bsh b/jacoco-maven-plugin.test/it/it-includes-excludes/verify.bsh
new file mode 100644
index 0000000..7b809b2
--- /dev/null
+++ b/jacoco-maven-plugin.test/it/it-includes-excludes/verify.bsh
@@ -0,0 +1,32 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2011 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.*;
+
+String agentOptions = "excludes=**/FileUtil*:**/TestUtil*";
+
+String buildLog = FileUtils.fileRead( new File( basedir, "build.log" ) );
+if ( buildLog.indexOf( agentOptions ) < 0 ) {
+    throw new RuntimeException("Include/Exclude was not configured correct" );
+}
+
+File databaseUtilReportFile = new File( basedir, "target/site/jacoco/org.project/DatabaseUtil.html" );
+if ( !databaseUtilReportFile.isFile() )
+{
+    throw new FileNotFoundException( "DatabaseUtil should NOT be excluded: " + databaseUtilReportFile );
+}
+
+File testUtilReportFile = new File( basedir, "target/site/jacoco/org.project/TestUtil.html" );
+if ( testUtilReportFile.isFile() )
+{
+    throw new RuntimeException( "TestUtil SHOULD be excluded: " + testUtilReportFile );
+}
diff --git a/jacoco-maven-plugin/src/org/jacoco/maven/AbstractJacocoMojo.java b/jacoco-maven-plugin/src/org/jacoco/maven/AbstractJacocoMojo.java
index 062bdad..65ed0a5 100644
--- a/jacoco-maven-plugin/src/org/jacoco/maven/AbstractJacocoMojo.java
+++ b/jacoco-maven-plugin/src/org/jacoco/maven/AbstractJacocoMojo.java
@@ -11,6 +11,8 @@
  *******************************************************************************/
 package org.jacoco.maven;
 
+import java.util.List;
+
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.project.MavenProject;
 
@@ -28,6 +30,23 @@
 	private MavenProject project;
 
 	/**
+	 * A list of class files to include in instrumentation/analysis/reports. May
+	 * use wildcard characters (* and ?). When not specified - everything will
+	 * be included.
+	 * 
+	 * @parameter expression="${jacoco.includes}"
+	 */
+	private List<String> includes;
+
+	/**
+	 * A list of class files to exclude from instrumentation/analysis/reports.
+	 * May use wildcard characters (* and ?).
+	 * 
+	 * @parameter expression="${jacoco.excludes}"
+	 */
+	private List<String> excludes;
+
+	/**
 	 * Flag used to suppress execution.
 	 * 
 	 * @parameter expression="${jacoco.skip}" default-value="false"
@@ -59,4 +78,11 @@
 		return project;
 	}
 
+	protected List<String> getIncludes() {
+		return includes;
+	}
+
+	protected List<String> getExcludes() {
+		return excludes;
+	}
 }
diff --git a/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java b/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java
index 1bcd241..b78ba12 100644
--- a/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java
+++ b/jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java
@@ -88,27 +88,6 @@
 	private Boolean append;
 
 	/**
-	 * A list of class names that should be included in execution analysis. The
-	 * list entries are separated by a colon (:) and may use wildcard characters
-	 * (* and ?). Except for performance optimization or technical corner cases
-	 * this option is normally not required.
-	 * 
-	 * @parameter expression="${jacoco.includes}"
-	 */
-	private String includes;
-
-	/**
-	 * A list of class names that should be excluded from execution analysis.
-	 * 
-	 * The list entries are separated by a colon (:) and may use wildcard
-	 * characters (* and ?). Except for performance optimization or technical
-	 * corner cases this option is normally not required.
-	 * 
-	 * @parameter expression="${jacoco.excludes}"
-	 */
-	private String excludes;
-
-	/**
 	 * A list of class loader names, that should be excluded from execution
 	 * analysis. The list entries are separated by a colon (:) and may use
 	 * wildcard characters (* and ?). This option might be required in case of
@@ -199,11 +178,15 @@
 		if (append != null) {
 			agentOptions.setAppend(append.booleanValue());
 		}
-		if (includes != null) {
-			agentOptions.setIncludes(includes);
+		if (getIncludes() != null && !getIncludes().isEmpty()) {
+			String agentIncludes = StringUtils.join(getIncludes().iterator(),
+					":");
+			agentOptions.setIncludes(agentIncludes);
 		}
-		if (excludes != null) {
-			agentOptions.setExcludes(excludes);
+		if (getExcludes() != null && !getExcludes().isEmpty()) {
+			String agentExcludes = StringUtils.join(getExcludes().iterator(),
+					":");
+			agentOptions.setExcludes(agentExcludes);
 		}
 		if (exclClassLoaders != null) {
 			agentOptions.setExclClassloader(exclClassLoaders);
diff --git a/jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java b/jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java
index b1b447d..7cc3d85 100644
--- a/jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java
+++ b/jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java
@@ -11,15 +11,12 @@
  *******************************************************************************/
 package org.jacoco.maven;
 
-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.io.*;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.StringUtils;
 import org.jacoco.core.analysis.Analyzer;
 import org.jacoco.core.analysis.CoverageBuilder;
 import org.jacoco.core.analysis.IBundleCoverage;
@@ -27,11 +24,7 @@
 import org.jacoco.core.data.ExecutionDataReader;
 import org.jacoco.core.data.ExecutionDataStore;
 import org.jacoco.core.data.SessionInfoStore;
-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.*;
 import org.jacoco.report.csv.CSVFormatter;
 import org.jacoco.report.html.HTMLFormatter;
 import org.jacoco.report.xml.XMLFormatter;
@@ -139,7 +132,13 @@
 		final Analyzer analyzer = new Analyzer(executionDataStore, builder);
 		final File classesDir = new File(getProject().getBuild()
 				.getOutputDirectory());
-		analyzer.analyzeAll(classesDir);
+
+		List<File> filesToAnalyze = getFilesToAnalyze(classesDir);
+
+		for (File file : filesToAnalyze) {
+			analyzer.analyzeAll(file);
+		}
+
 		return builder.getBundle(getProject().getName());
 	}
 
@@ -218,4 +217,20 @@
 		return result;
 	}
 
+	protected List<File> getFilesToAnalyze(File rootDir) throws IOException {
+		final String includes;
+		if (getIncludes() != null && !getIncludes().isEmpty()) {
+			includes = StringUtils.join(getIncludes().iterator(), ",");
+		} else {
+			includes = "**";
+		}
+		final String excludes;
+		if (getExcludes() != null && !getExcludes().isEmpty()) {
+			excludes = StringUtils.join(getExcludes().iterator(), ",");
+		} else {
+			excludes = "";
+		}
+		return FileUtils.getFiles(rootDir, includes, excludes);
+	}
+
 }
diff --git a/org.jacoco.doc/docroot/doc/changes.html b/org.jacoco.doc/docroot/doc/changes.html
index cfccbfe..fd00603 100644
--- a/org.jacoco.doc/docroot/doc/changes.html
+++ b/org.jacoco.doc/docroot/doc/changes.html
@@ -20,6 +20,10 @@
 

 <h2>Trunk Build @qualified.bundle.version@ (@build.date@)</h2>

 

+<h3>New Features</h3>

+<ul>

+  <li>Maven plug-in : respect includes/excludes properties in Report Mojo (Trac #160).</li>

+</ul>

 

 <h2>Release 0.5.3 (2011/07/06)</h2>