Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Maven Mojo to perform offline instrumentation
- Loading branch information
Showing
7 changed files
with
280 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
jacoco-maven-plugin.test/it/it-offline-instrumentation/pom.xml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?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: | ||
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-offline-instrumentation</artifactId> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>@project.groupId@</groupId> | ||
<artifactId>org.jacoco.agent</artifactId> | ||
<classifier>runtime</classifier> | ||
<version>@project.version@</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>@project.groupId@</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>instrument-classes</id> | ||
<goals> | ||
<goal>instrument</goal> | ||
</goals> | ||
<configuration> | ||
<excludes> | ||
<exclude>**/DoNotInstrument.class</exclude> | ||
</excludes> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>restore-instrumented-classes</id> | ||
<goals> | ||
<goal>restore</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>report</id> | ||
<phase>prepare-package</phase> | ||
<goals> | ||
<goal>report</goal> | ||
</goals> | ||
<configuration> | ||
<dataFile>${project.build.directory}/coverage.exec</dataFile> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<configuration> | ||
<systemPropertyVariables> | ||
<jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile> | ||
</systemPropertyVariables> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
18 changes: 18 additions & 0 deletions
18
jacoco-maven-plugin.test/it/it-offline-instrumentation/src/main/java/DoNotInstrument.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
public class DoNotInstrument { | ||
|
||
public void sayHello() { | ||
System.out.println("Hello world"); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
jacoco-maven-plugin.test/it/it-offline-instrumentation/src/main/java/Example.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
public class Example { | ||
|
||
public void sayHello() { | ||
System.out.println("Hello world"); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
jacoco-maven-plugin.test/it/it-offline-instrumentation/src/test/java/ExampleTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
* Evgeny Mandrikov - initial API and implementation | ||
* | ||
*******************************************************************************/ | ||
import org.junit.Test; | ||
|
||
public class ExampleTest { | ||
|
||
@Test | ||
public void test() { | ||
new Example().sayHello(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
jacoco-maven-plugin.test/it/it-offline-instrumentation/verify.bsh
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/******************************************************************************* | ||
* 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 | ||
* | ||
*******************************************************************************/ | ||
|
||
File file = new File( basedir, "target/generated-classes/jacoco/Example.class" ); | ||
if ( !file.isFile() ) { | ||
throw new RuntimeException( "Could not find backup of instrumented class: " + file ); | ||
} | ||
|
||
file = new File( basedir, "target/coverage.exec" ); | ||
if ( !file.isFile() ) | ||
{ | ||
throw new FileNotFoundException( "Could not find generated dump: " + file ); | ||
} |
79 changes: 79 additions & 0 deletions
79
jacoco-maven-plugin/src/org/jacoco/maven/InstrumentMojo.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/******************************************************************************* | ||
* 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 | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.maven; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.codehaus.plexus.util.FileUtils; | ||
import org.codehaus.plexus.util.IOUtil; | ||
import org.jacoco.core.instr.Instrumenter; | ||
import org.jacoco.core.runtime.OfflineInstrumentationAccessGenerator; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.List; | ||
|
||
/** | ||
* @phase process-classes | ||
* @goal instrument | ||
* @requiresProject true | ||
*/ | ||
public class InstrumentMojo extends AbstractJacocoMojo { | ||
|
||
@Override | ||
public void executeMojo() throws MojoExecutionException, | ||
MojoFailureException { | ||
final File originalClassesDir = new File(getProject().getBuild() | ||
.getDirectory(), "generated-classes/jacoco"); | ||
final File classesDir = new File(getProject().getBuild() | ||
.getOutputDirectory()); | ||
|
||
final List<String> fileNames; | ||
try { | ||
final FileFilter fileFilter = new FileFilter(this.getIncludes(), | ||
this.getExcludes()); | ||
fileNames = FileUtils.getFileNames(classesDir, | ||
fileFilter.getIncludes(), fileFilter.getExcludes(), false); | ||
} catch (final IOException e1) { | ||
throw new MojoExecutionException( | ||
"Unable to get list of files to instrument.", e1); | ||
} | ||
|
||
final Instrumenter instrumenter = new Instrumenter( | ||
new OfflineInstrumentationAccessGenerator()); | ||
for (final String fileName : fileNames) { | ||
if (fileName.endsWith(".class")) { | ||
final File source = new File(classesDir, fileName); | ||
final File backup = new File(originalClassesDir, fileName); | ||
InputStream input = null; | ||
OutputStream output = null; | ||
try { | ||
FileUtils.copyFile(source, backup); | ||
input = new FileInputStream(backup); | ||
output = new FileOutputStream(source); | ||
instrumenter.instrument(input, output); | ||
} catch (final IOException e2) { | ||
throw new MojoExecutionException( | ||
"Unable to instrument file.", e2); | ||
} finally { | ||
IOUtil.close(input); | ||
IOUtil.close(output); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* 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 | ||
* | ||
*******************************************************************************/ | ||
package org.jacoco.maven; | ||
|
||
import org.apache.maven.plugin.MojoExecutionException; | ||
import org.apache.maven.plugin.MojoFailureException; | ||
import org.codehaus.plexus.util.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @phase prepare-package | ||
* @goal restore | ||
* @requiresProject true | ||
*/ | ||
public class RestoreMojo extends AbstractJacocoMojo { | ||
|
||
@Override | ||
protected void executeMojo() throws MojoExecutionException, | ||
MojoFailureException { | ||
final File originalClassesDir = new File(getProject().getBuild() | ||
.getDirectory(), "generated-classes/jacoco"); | ||
final File classesDir = new File(getProject().getBuild() | ||
.getOutputDirectory()); | ||
try { | ||
FileUtils.copyDirectoryStructure(originalClassesDir, classesDir); | ||
} catch (final IOException e) { | ||
throw new MojoFailureException("Unable to restore classes.", e); | ||
} | ||
} | ||
|
||
} |