diff options
author | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-08-27 03:29:22 -0700 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2013-08-27 03:29:22 -0700 |
commit | 29cf17c844ecac223b3643d3df990d1892eca0db (patch) | |
tree | 2cbd54f1d0b5e42e33f94d35bb9553685bc36d8a | |
parent | 3e767fae60394a0977c09aeb068495df6a215e19 (diff) | |
parent | e2dd04ff17a6bb5aea171b084c9f84754048d5a8 (diff) | |
download | jacoco-29cf17c844ecac223b3643d3df990d1892eca0db.tar.gz |
Merge pull request #126 from hrmohr/MergeMojo
MergeMojo implementation based on MergeTask
12 files changed, 372 insertions, 1 deletions
diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-merge/pom.xml b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-merge/pom.xml new file mode 100644 index 00000000..905733c6 --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-merge/pom.xml @@ -0,0 +1,50 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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: + Mads Mohr Christensen - implementation of MergeMojo +--> +<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>it-merge-passes</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>it-merge-passes-merge</artifactId> + + <build> + <plugins> + <plugin> + <groupId>@project.groupId@</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <executions> + <execution> + <id>merge</id> + <goals> + <goal>merge</goal> + </goals> + <configuration> + <fileSets> + <fileSet> + <directory>${project.parent.build.directory}</directory> + <includes> + <include>*.exec</include> + </includes> + </fileSet> + </fileSets> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project>
\ No newline at end of file diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/pom.xml b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/pom.xml new file mode 100644 index 00000000..47dcb8f6 --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/pom.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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: + Mads Mohr Christensen - implementation of MergeMojo +--> +<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>it-merge-passes</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>it-merge-passes-project1</artifactId> + +</project> diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/src/main/java/Example.java b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/src/main/java/Example.java new file mode 100644 index 00000000..6f95e5fa --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/src/main/java/Example.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * 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: + * Mads Mohr Christensen - implementation of MergeMojo + * + *******************************************************************************/ +public class Example { + + public void sayHello() { + System.out.println("Hello world"); + } + +} diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/src/test/java/ExampleTest.java b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/src/test/java/ExampleTest.java new file mode 100644 index 00000000..b7fee1ef --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project1/src/test/java/ExampleTest.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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: + * Mads Mohr Christensen - implementation of MergeMojo + * + *******************************************************************************/ +import org.junit.Test; + +public class ExampleTest { + + @Test + public void test() { + new Example().sayHello(); + } + +} diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/pom.xml b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/pom.xml new file mode 100644 index 00000000..0781314d --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/pom.xml @@ -0,0 +1,24 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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: + Mads Mohr Christensen - implementation of MergeMojo +--> +<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>it-merge-passes</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> + + <artifactId>it-merge-passes-project2</artifactId> + +</project> diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/src/main/java/Example.java b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/src/main/java/Example.java new file mode 100644 index 00000000..6f95e5fa --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/src/main/java/Example.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * 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: + * Mads Mohr Christensen - implementation of MergeMojo + * + *******************************************************************************/ +public class Example { + + public void sayHello() { + System.out.println("Hello world"); + } + +} diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/src/test/java/ExampleTest.java b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/src/test/java/ExampleTest.java new file mode 100644 index 00000000..b7fee1ef --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/it-merge-passes-project2/src/test/java/ExampleTest.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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: + * Mads Mohr Christensen - implementation of MergeMojo + * + *******************************************************************************/ +import org.junit.Test; + +public class ExampleTest { + + @Test + public void test() { + new Example().sayHello(); + } + +} diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/pom.xml b/jacoco-maven-plugin.test/it/it-merge-passes/pom.xml new file mode 100644 index 00000000..a53be12f --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/pom.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + 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: + Mads Mohr Christensen - implementation of MergeMojo +--> +<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-merge-passes</artifactId> + <packaging>pom</packaging> + + <modules> + <module>it-merge-passes-project1</module> + <module>it-merge-passes-project2</module> + <module>it-merge-passes-merge</module> + </modules> + + <build> + <plugins> + <plugin> + <groupId>@project.groupId@</groupId> + <artifactId>jacoco-maven-plugin</artifactId> + <executions> + <execution> + <id>prepare-agent</id> + <goals> + <goal>prepare-agent</goal> + </goals> + <configuration> + <destFile>${project.parent.build.directory}/${project.artifactId}.exec</destFile> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <argLine>${argLine}</argLine> + </configuration> + </plugin> + </plugins> + </build> + +</project> diff --git a/jacoco-maven-plugin.test/it/it-merge-passes/verify.bsh b/jacoco-maven-plugin.test/it/it-merge-passes/verify.bsh new file mode 100644 index 00000000..0ec3faaa --- /dev/null +++ b/jacoco-maven-plugin.test/it/it-merge-passes/verify.bsh @@ -0,0 +1,21 @@ +/******************************************************************************* + * 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: + * Mads Mohr Christensen - implementation of MergeMojo + * + *******************************************************************************/ + import java.io.*; + import org.codehaus.plexus.util.*; + + String buildLog = FileUtils.fileRead( new File( basedir, "build.log" ) ); + if ( buildLog.indexOf( "Loading execution data file" ) < 0 ) { + throw new RuntimeException( "Could not load execution data file" ); + } + if ( buildLog.indexOf( "Writing merged execution data to" ) < 0 ) { + throw new RuntimeException( "Could not write merged execution data" ); + }
\ No newline at end of file diff --git a/jacoco-maven-plugin/pom.xml b/jacoco-maven-plugin/pom.xml index 098b673b..f3a524f9 100644 --- a/jacoco-maven-plugin/pom.xml +++ b/jacoco-maven-plugin/pom.xml @@ -46,6 +46,11 @@ <artifactId>plexus-utils</artifactId> <version>1.5.6</version> </dependency> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>file-management</artifactId> + <version>1.2.1</version> + </dependency> <dependency> <groupId>org.apache.maven.reporting</groupId> diff --git a/jacoco-maven-plugin/src/org/jacoco/maven/MergeMojo.java b/jacoco-maven-plugin/src/org/jacoco/maven/MergeMojo.java new file mode 100644 index 00000000..8423f6b8 --- /dev/null +++ b/jacoco-maven-plugin/src/org/jacoco/maven/MergeMojo.java @@ -0,0 +1,111 @@ +/******************************************************************************* + * 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: + * Mads Mohr Christensen - implementation of MergeMojo + * + *******************************************************************************/ +package org.jacoco.maven; + +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.shared.model.fileset.FileSet; +import org.apache.maven.shared.model.fileset.util.FileSetManager; +import org.jacoco.core.data.ExecFileLoader; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +/** + * Mojo for merging a set of execution data files (*.exec) into a single file + * + * @phase generate-resources + * @goal merge + * @requiresProject true + * @threadSafe + */ +public class MergeMojo extends AbstractJacocoMojo { + + private static final String MSG_SKIPPING = "Skipping JaCoCo merge execution due to missing execution data files"; + + /** + * Path to the output file for execution data. + * + * @parameter expression="${jacoco.destFile}" + * default-value="${project.build.directory}/jacoco.exec" + */ + private File destFile; + + /** + * This mojo accepts any number of execution data file sets. + * + * @parameter expression="${jacoco.fileSets}" + * @required + */ + private List<FileSet> fileSets; + + @Override + protected void executeMojo() throws MojoExecutionException, MojoFailureException { + if (!canMergeReports()) { + return; + } + executeMerge(); + } + + private boolean canMergeReports() { + if (fileSets == null || fileSets.isEmpty()) { + getLog().info(MSG_SKIPPING); + return false; + } + return true; + } + + private void executeMerge() throws MojoExecutionException { + final ExecFileLoader loader = new ExecFileLoader(); + + load(loader); + save(loader); + } + + private void load(final ExecFileLoader loader) throws MojoExecutionException { + final FileSetManager fileSetManager = new FileSetManager(getLog()); + for (FileSet fileSet : fileSets) { + for (String includedFilename : fileSetManager.getIncludedFiles(fileSet)) { + final File inputFile = new File(fileSet.getDirectory(), includedFilename); + if (inputFile.isDirectory()) { + continue; + } + try { + getLog().info("Loading execution data file " + inputFile.getAbsolutePath()); + loader.load(inputFile); + } catch (IOException e) { + throw new MojoExecutionException("Unable to read " + inputFile.getAbsolutePath(), e); + } + } + } + } + + private void save(final ExecFileLoader loader) throws MojoExecutionException { + if (loader.getExecutionDataStore().getContents().isEmpty()) { + getLog().info(MSG_SKIPPING); + return; + } + getLog().info("Writing merged execution data to " + destFile.getAbsolutePath()); + try { + File parent = destFile.getParentFile(); + if (!parent.exists()) { + parent.mkdirs(); + } + destFile.createNewFile(); + loader.save(destFile, false); + } catch (IOException e) { + throw new MojoExecutionException("Unable to write merged file " + destFile.getAbsolutePath(), e); + } + } + +} diff --git a/org.jacoco.ant/src/org/jacoco/ant/MergeTask.java b/org.jacoco.ant/src/org/jacoco/ant/MergeTask.java index 974c9f0f..166e3181 100644 --- a/org.jacoco.ant/src/org/jacoco/ant/MergeTask.java +++ b/org.jacoco.ant/src/org/jacoco/ant/MergeTask.java @@ -27,7 +27,7 @@ import org.apache.tools.ant.util.FileUtils; import org.jacoco.core.data.ExecFileLoader; /** - * Task for merging a set of execution data store files into a single file + * Task for merging a set of execution data files (*.exec) into a single file */ public class MergeTask extends Task { |