Index: src/main/java/org/codehaus/mojo/cobertura/CoberturaInstrumentMojo.java =================================================================== --- src/main/java/org/codehaus/mojo/cobertura/CoberturaInstrumentMojo.java (revision 4684) +++ src/main/java/org/codehaus/mojo/cobertura/CoberturaInstrumentMojo.java (working copy) @@ -22,6 +22,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; +import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; @@ -30,11 +31,13 @@ import org.apache.maven.artifact.ArtifactUtils; import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.handler.ArtifactHandler; +import org.apache.maven.model.Plugin; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.mojo.cobertura.configuration.ConfigInstrumentation; import org.codehaus.mojo.cobertura.tasks.InstrumentTask; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.IOUtil; +import org.codehaus.plexus.util.xml.Xpp3Dom; /** * Instrument the compiled classes. @@ -146,12 +149,63 @@ { IOUtil.close( fos ); } + + // Even better way, no need for a patched Cobertura. + if ( project.getBuildPlugins() != null ) { + for ( Iterator iterator = project.getBuildPlugins().iterator(); iterator.hasNext(); ) { + Plugin plugin = (Plugin) iterator.next(); + if ("org.apache.maven.plugins".equals( plugin.getGroupId() ) && + "maven-surefire-plugin".equals( plugin.getArtifactId() ) ) { + addDatafileConfiguration((Xpp3Dom)plugin.getConfiguration()); + } + } + } // Set the instrumented classes to be the new output directory (for other plugins to pick up) project.getBuild().setOutputDirectory( instrumentedDirectory.getPath() ); System.setProperty( "project.build.outputDirectory", instrumentedDirectory.getPath() ); } } + + /** + * Adds Cobertura runtime configuration to the Maven Surefire plugin + * configuration, so that the instrumented unit tests can find the + * Cobertura datafile. + *
+ * This is the equivalent of having the following configuration in + * the project's pom: + * <plugin> + * <groupId>org.apache.maven.plugins</groupId> + * <artifactId>maven-surefire-plugin</artifactId> + * <configuration> + * ...(existing configuration)... + * <systemProperties> + * <property> + * <name>net.sourceforge.cobertura.datafile</name> + * <value>${project.build.directory}/cobertura/cobertura.ser</value> + * </property> + * </systemProperties> + * </configuration> + * </plugin> + * + * @param configuration The Maven Surefire plugin configuration node. + */ + private void addDatafileConfiguration(Xpp3Dom configuration) { + // set the cobertura datafile property to be passed to the unit tests. + Xpp3Dom props = configuration.getChild("systemProperties"); + if (props == null) { + props = new Xpp3Dom("systemProperties"); + configuration.addChild(props); + } + Xpp3Dom property = new Xpp3Dom("property"); + props.addChild(property); + Xpp3Dom name = new Xpp3Dom("name"); + name.setValue("net.sourceforge.cobertura.datafile"); + property.addChild(name); + Xpp3Dom value = new Xpp3Dom("value"); + value.setValue(dataFile.getPath()); + property.addChild(value); + } private void addCoberturaDependenciesToTestClasspath() throws MojoExecutionException Index: src/test/java/org/codehaus/mojo/cobertura/stubs/InstrumentMavenProjectStub.java =================================================================== --- src/test/java/org/codehaus/mojo/cobertura/stubs/InstrumentMavenProjectStub.java (revision 4684) +++ src/test/java/org/codehaus/mojo/cobertura/stubs/InstrumentMavenProjectStub.java (working copy) @@ -25,6 +25,7 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Set; @@ -68,7 +69,11 @@ return build; } - public Artifact getArtifact() + public List getBuildPlugins() { + return new ArrayList(); + } + + public Artifact getArtifact() { return new ArtifactStub(); }