Created
August 29, 2012 21:10
-
-
Save marchof/3519018 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Index: jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java | |
=================================================================== | |
--- jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java (revision 1686) | |
+++ jacoco-maven-plugin/src/org/jacoco/maven/AgentMojo.java (working copy) | |
@@ -16,6 +16,7 @@ | |
import java.util.Properties; | |
import org.apache.maven.artifact.Artifact; | |
+import org.apache.maven.project.MavenProject; | |
import org.codehaus.plexus.util.StringUtils; | |
import org.jacoco.core.runtime.AgentOptions; | |
@@ -153,6 +154,13 @@ | |
*/ | |
private Integer port; | |
+ /** | |
+ * Whether to build an aggregated report at the root, or build individual reports. | |
+ * | |
+ * @parameter expression="${aggregate}" default-value="false" | |
+ */ | |
+ private boolean aggregate; | |
+ | |
@Override | |
public void executeMojo() { | |
final String vmArgument = StringUtils.quoteAndEscape( | |
@@ -174,7 +182,7 @@ | |
private AgentOptions createAgentOptions() { | |
final AgentOptions agentOptions = new AgentOptions(); | |
- final String destPath = destFile.getAbsolutePath(); | |
+ final String destPath = getDestFile().getAbsolutePath(); | |
agentOptions.setDestfile(destPath); | |
if (append != null) { | |
agentOptions.setAppend(append.booleanValue()); | |
@@ -227,4 +235,24 @@ | |
projectProperties.put(name, newValue); | |
} | |
+ private File getDestFile() { | |
+ final File result; | |
+ if (aggregate) { | |
+ MavenProject prj = getProject(); | |
+ while (!prj.isExecutionRoot()) { | |
+ prj = prj.getParent(); | |
+ } | |
+ if (destFile.isAbsolute()) { | |
+ String relDestFile = destFile.getAbsolutePath().substring( | |
+ getProject().getBasedir().getAbsolutePath().length()); | |
+ result = new File(prj.getBasedir(), relDestFile); | |
+ } else { | |
+ result = new File(prj.getBasedir(), destFile.getPath()); | |
+ } | |
+ } else { | |
+ result = destFile; | |
+ } | |
+ return result; | |
+ } | |
+ | |
} | |
Index: jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java | |
=================================================================== | |
--- jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java (revision 1686) | |
+++ jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java (working copy) | |
@@ -18,8 +18,10 @@ | |
import java.io.InputStreamReader; | |
import java.io.Reader; | |
import java.util.ArrayList; | |
+import java.util.HashSet; | |
import java.util.List; | |
import java.util.Locale; | |
+import java.util.Set; | |
import org.apache.maven.doxia.siterenderer.Renderer; | |
import org.apache.maven.plugin.MojoExecutionException; | |
@@ -113,6 +115,13 @@ | |
private boolean skip; | |
/** | |
+ * Whether to build an aggregated report at the root, or build individual reports. | |
+ * | |
+ * @parameter expression="${aggregate}" default-value="false" | |
+ */ | |
+ private boolean aggregate; | |
+ | |
+ /** | |
* Maven project. | |
* | |
* @parameter expression="${project}" | |
@@ -121,6 +130,14 @@ | |
private MavenProject project; | |
/** | |
+ * The projects in the reactor for aggregation report. | |
+ * | |
+ * @parameter expression="${reactorProjects}" | |
+ * @readonly | |
+ */ | |
+ private List<MavenProject> reactorProjects; | |
+ | |
+ /** | |
* Doxia Site Renderer. | |
* | |
* @component | |
@@ -153,11 +170,19 @@ | |
return outputDirectory.getAbsolutePath(); | |
} | |
+ protected boolean isAggregate() { | |
+ return aggregate; | |
+ } | |
+ | |
@Override | |
protected MavenProject getProject() { | |
return project; | |
} | |
+ protected List<MavenProject> getReactorProjects() { | |
+ return reactorProjects; | |
+ } | |
+ | |
@Override | |
protected Renderer getSiteRenderer() { | |
return siteRenderer; | |
@@ -183,7 +208,10 @@ | |
@Override | |
public boolean canGenerateReport() { | |
- if ("pom".equals(project.getPackaging())) { | |
+ if (aggregate && !project.isExecutionRoot()) { | |
+ return false; | |
+ } | |
+ if ("pom".equals(project.getPackaging()) && !aggregate) { | |
getLog().info( | |
"Skipping JaCoCo for project with packaging type 'pom'"); | |
return false; | |
@@ -271,10 +299,22 @@ | |
private IBundleCoverage createBundle() throws IOException { | |
final CoverageBuilder builder = new CoverageBuilder(); | |
final Analyzer analyzer = new Analyzer(executionDataStore, builder); | |
- final File classesDir = new File(getProject().getBuild() | |
- .getOutputDirectory()); | |
+ final List<File> classesDirs = new ArrayList<File>(); | |
+ if (isAggregate()) { | |
+ for (final MavenProject localProject : getReactorProjects()) { | |
+ final File classesDir = new File(localProject.getBuild() | |
+ .getOutputDirectory()); | |
+ if (classesDir.isDirectory()) { | |
+ classesDirs.add(classesDir); | |
+ } | |
+ } | |
+ } else { | |
+ final File classesDir = new File(getProject().getBuild() | |
+ .getOutputDirectory()); | |
+ classesDirs.add(classesDir); | |
+ } | |
- final List<File> filesToAnalyze = getFilesToAnalyze(classesDir); | |
+ final List<File> filesToAnalyze = getFilesToAnalyze(classesDirs); | |
for (final File file : filesToAnalyze) { | |
analyzer.analyzeAll(file); | |
@@ -342,37 +382,70 @@ | |
} | |
} | |
- private File resolvePath(final String path) { | |
+ private File resolvePath(final String path, final MavenProject prj) { | |
File file = new File(path); | |
if (!file.isAbsolute()) { | |
- file = new File(getProject().getBasedir(), path); | |
+ file = new File(prj.getBasedir(), path); | |
} | |
return file; | |
} | |
private List<File> getCompileSourceRoots() { | |
final List<File> result = new ArrayList<File>(); | |
- for (final Object path : getProject().getCompileSourceRoots()) { | |
- result.add(resolvePath((String) path)); | |
+ if (isAggregate()) { | |
+ for (final MavenProject localProject : getReactorProjects()) { | |
+ for (final Object path : localProject.getCompileSourceRoots()) { | |
+ result.add(resolvePath((String) path, localProject)); | |
+ } | |
+ } | |
+ } else { | |
+ for (final Object path : getProject().getCompileSourceRoots()) { | |
+ result.add(resolvePath((String) path, getProject())); | |
+ } | |
} | |
return result; | |
} | |
- protected List<File> getFilesToAnalyze(final File rootDir) | |
+ protected List<File> getFilesToAnalyze(final List<File> rootDirs) | |
throws IOException { | |
- final String includes; | |
- if (getIncludes() != null && !getIncludes().isEmpty()) { | |
- includes = StringUtils.join(getIncludes().iterator(), ","); | |
- } else { | |
- includes = "**"; | |
+ final List<File> filesToAnalyze = new ArrayList<File>(); | |
+ final Set<String> alreadySeen = new HashSet<String>(); | |
+ for (File rootDir : rootDirs) { | |
+ 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 = ""; | |
+ } | |
+ final List<File> candidates = FileUtils.getFiles(rootDir, includes, excludes); | |
+ final List<File> analyzeFiles = new ArrayList<File>(); | |
+ for (final File candidate : candidates) { | |
+ final String relPath = extractRelativePath(rootDir, candidate); | |
+ if (!alreadySeen.contains(relPath)) { | |
+ alreadySeen.add(relPath); | |
+ analyzeFiles.add(candidate); | |
+ } | |
+ } | |
+ filesToAnalyze.addAll(analyzeFiles); | |
} | |
- final String excludes; | |
- if (getExcludes() != null && !getExcludes().isEmpty()) { | |
- excludes = StringUtils.join(getExcludes().iterator(), ","); | |
+ return filesToAnalyze; | |
+ } | |
+ | |
+ private String extractRelativePath(final File rootDir, final File candidate) | |
+ { | |
+ final String result; | |
+ if (candidate.isAbsolute()) { | |
+ result = candidate.getAbsolutePath().substring(rootDir.getAbsolutePath().length()); | |
} else { | |
- excludes = ""; | |
+ result = candidate.getPath(); | |
} | |
- return FileUtils.getFiles(rootDir, includes, excludes); | |
+ return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment