Skip to content

Commit

Permalink
Merge pull request jacoco#1 from johnoliver/master
Browse files Browse the repository at this point in the history
Add ability to add source and class dirs to the current projects list
  • Loading branch information
karianna committed Mar 13, 2013
2 parents 1b1935e + 9a762b2 commit 90074be
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 5 deletions.
43 changes: 40 additions & 3 deletions jacoco-maven-plugin/src/org/jacoco/maven/BundleCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.maven.project.MavenProject;
Expand Down Expand Up @@ -55,19 +57,54 @@ public BundleCreator(final MavenProject project, final FileFilter fileFilter) {
*/
public IBundleCoverage createBundle(
final ExecutionDataStore executionDataStore) throws IOException {
return createBundle(executionDataStore, null);
}

/**
* Create an IBundleCoverage for the given ExecutionDataStore.
*
* @param executionDataStore
* the execution data.
* @param additionalClassesDirs
* additional class dirs to be scanned for class files
* @return the coverage data.
* @throws IOException
* if class files can't be read
*/
public IBundleCoverage createBundle(
final ExecutionDataStore executionDataStore,
final Collection<File> additionalClassesDirs) throws IOException {
final CoverageBuilder builder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(executionDataStore, builder);
final File classesDir = new File(this.project.getBuild()
.getOutputDirectory());

@SuppressWarnings("unchecked")
final List<File> filesToAnalyze = FileUtils.getFiles(classesDir,
fileFilter.getIncludes(), fileFilter.getExcludes());
final List<File> filesToAnalyze = new ArrayList<File>();
addDirToFileList(classesDir, filesToAnalyze);

if (additionalClassesDirs != null) {
for (final File file : additionalClassesDirs) {
addDirToFileList(file, filesToAnalyze);
}
}

for (final File file : filesToAnalyze) {
analyzer.analyzeAll(file);
}

return builder.getBundle(this.project.getName());
}

private void addDirToFileList(final File classesDir,
final List<File> filesToAnalyze) throws IOException {
if (doesDirectoryExist(classesDir)) {
filesToAnalyze.addAll(FileUtils.getFiles(classesDir,
fileFilter.getIncludes(), fileFilter.getExcludes()));
}
}

private boolean doesDirectoryExist(final File classesDir) {
return classesDir != null && classesDir.exists()
&& classesDir.isDirectory();
}
}
53 changes: 51 additions & 2 deletions jacoco-maven-plugin/src/org/jacoco/maven/ReportMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class ReportMojo extends AbstractMavenReport {
*
* @parameter default-value="${project.build.directory}/jacoco.exec"
*/
private File dataFile;
protected File dataFile;

/**
* A list of class files to include in the report. May use wildcard
Expand Down Expand Up @@ -124,6 +124,22 @@ public class ReportMojo extends AbstractMavenReport {
*/
private Renderer siteRenderer;

/**
* A list of source folders in addition to the current projects source
* folder to be scanned for source files.
*
* @parameter
*/
private List<String> sourceFolders;

/**
* A list of class folders in addition to the current projects class folder
* to be scanned for class files.
*
* @parameter
*/
private List<String> classFolders;

private SessionInfoStore sessionInfoStore;

private ExecutionDataStore executionDataStore;
Expand Down Expand Up @@ -259,7 +275,16 @@ private void createReport(final IReportGroupVisitor visitor)
this.getExcludes());
final BundleCreator creator = new BundleCreator(this.getProject(),
fileFilter);
final IBundleCoverage bundle = creator.createBundle(executionDataStore);

final List<File> classFoldersList = new ArrayList<File>();
if (classFolders != null) {
for (final String folder : classFolders) {
classFoldersList.add(new File(folder));
}
}

final IBundleCoverage bundle = creator.createBundle(executionDataStore,
classFoldersList);

final SourceFileCollection locator = new SourceFileCollection(
getCompileSourceRoots(), sourceEncoding);
Expand Down Expand Up @@ -347,6 +372,30 @@ private List<File> getCompileSourceRoots() {
for (final Object path : getProject().getCompileSourceRoots()) {
result.add(resolvePath((String) path));
}

if (sourceFolders != null) {
for (final String dir : sourceFolders) {
result.add(new File(dir));
}
}

return result;
}

public List<String> getSourceFolders() {
return sourceFolders;
}

public void setSourceFolders(final List<String> sourceFolders) {
this.sourceFolders = sourceFolders;
}

public List<String> getClassFolders() {
return classFolders;
}

public void setClassFolders(final List<String> classFolders) {
this.classFolders = classFolders;
}

}
95 changes: 95 additions & 0 deletions jacoco-maven-plugin/src/org/jacoco/maven/ReportPomMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*******************************************************************************
* Copyright (c) 2009, 2013 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
* Kyle Lieber - implementation of CheckMojo
*
*******************************************************************************/
package org.jacoco.maven;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.maven.project.MavenProject;
import org.apache.maven.reporting.MavenReportException;

/**
* Creates a code coverage report from an exec file appended to from multiple
* reactor projects (HTML, XML, and CSV).
*
* @goal pom-report
* @aggregate
*/
public class ReportPomMojo extends ReportMojo {

/**
* Flag used to suppress execution.
*
* @parameter expression="${jacoco.skip}" default-value="false"
*/
private boolean skip;

/**
* Maven project.
*
* @parameter expression="${project}"
* @readonly
*/
private MavenProject project;

/**
* The projects in the reactor.
*
* @parameter expression="${reactorProjects}"
* @readonly
*/
private List<MavenProject> reactorProjects;

@Override
public boolean canGenerateReport() {
if (!"pom".equals(project.getPackaging())) {
getLog().info(
"Skipping JaCoCo since this is not of packaging type 'pom'");
return false;
}
if (skip) {
getLog().info("Skipping JaCoCo execution");
return false;
}
if (!dataFile.exists()) {
getLog().info(
"Skipping JaCoCo execution due to missing execution data file");
return false;
}
return true;
}

@Override
protected void executeReport(final Locale locale)
throws MavenReportException {
List<String> sourceFolders = super.getSourceFolders();
if (sourceFolders == null) {
sourceFolders = new ArrayList<String>();
}

List<String> classFolders = super.getClassFolders();
if (classFolders == null) {
classFolders = new ArrayList<String>();
}

for (final MavenProject reactor : reactorProjects) {
sourceFolders.addAll(reactor.getCompileSourceRoots());
classFolders.add(reactor.getBuild().getOutputDirectory());
}

setSourceFolders(sourceFolders);
setClassFolders(classFolders);
super.executeReport(locale);
}
}

0 comments on commit 90074be

Please sign in to comment.