2

when i instrument my classes using Maven 2 using the command

mvn cobertura:instrument

The output (the instrumented classes) are put in \target\generated-classes. Is there a way to change the output location to \target\classes?

I checked the instrumentation tasks of the cobertura-maven plugin but this does not give me a solution sofar.

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Marco
  • 15,101
  • 33
  • 107
  • 174
  • Pascal. Did you find a solition for this that would enable the instrumented classes to be generated in the classes folder, and further, be picked up by m2eclipse to be published to tomcat? – sfk Aug 31 '11 at 16:23

6 Answers6

3

You have not said why you want to overwrite the default location, but I assume it is so that you can use the instrumented classes from another project, or perhaps include them in a web archive or something similar.

I added the following to my pom.xml

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>instrumented-classes</id>
            <goals>
                <goal>jar</goal>
            </goals>
            <phase>package</phase>
            <configuration>
                <classifier>instrumented</classifier>
                <classesDirectory>${project.build.directory}/generated-classes/cobertura</classesDirectory>
            </configuration>
         </execution>
    </executions>
</plugin>

This makes maven generate an additional jar file called projectname-instrumented.jar

It is then possible to depend on this jar file from any other pom (including for example a web module) using

<depends>
    <group>mygroup</group>
    <project>projectname</project>
    <version>1</version>
    <classifier>instrumented</classifier>
</depends>

I did not test this 100% but have used similar mechanisms in the past

djones
  • 31
  • 2
1

As far as I understand, the instrumented classes are only needed by cobertura for report generation. If you create them in target/classes, they will overwrite the original class files.

If you need the instrumented files in a jar as a result, you can configure the maven-jar-plugin to pick up the files from the target/generated-classes directory instead of or in addition to the files from the standard ${build.project.outputDirectory}.

Edit

Have a look at the maven-jar-plugin description. To only use target/generated-classes, the following addition to your POM should work - try it and modify it to your needs:

<project>
  ...
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.3</version> <!-- replace with correct version nbr! -->
        <configuration>
          <includes>
            <include>${project.build.directory}/generated-classes/**/*.class</include>
          </includes>
          <excludes>
            <exclude>${project.build.directory}/classes/**/*.class</include>
          </excludes>

        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

${project.build.directory} points to your target folder, ${project.build.ouputDirectory} to target/classes. I do not know if you can simply set ${project.build.ouputDirectory} to a new value - have a look at the this chapter of the maven book, maybe you find some hints

Edit 2

Alternativly or additionally you can use maven to copy the files from target/generated-classes to target/classes after coberture:instrument has finished. This question has one answer with an example POM (fragment), you just have to identify the correct phase (process-resources is definitely too early for your case)

Community
  • 1
  • 1
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • That would be optimal to overwrite the target/classes, in that way the packaging of my wars, jars and ears will be untouched. Is it enough to overwrite/set the ${project.build.outputDirectory} for generation or packaging? – Marco Jan 08 '10 at 10:39
  • I need those instrumented classes inside my jars/wars and ears. So i am looking for a way on how to accomplish this the easy way. – Marco Jan 08 '10 at 10:53
1

Did you try "mvn cobertura:instrument install"? It will generate a jar file including all the cobertura version classes. If you want to change back original version, just run the command without "cobertura:instrument".

Xt Zh
  • 11
  • 1
0

I just implemented the solution proposed by Andreas_D, modified my pom and uses the maven-resources-plugin. So on some stage of my build the Cobertura generated files are copied to the /target/classes directory.

Marco
  • 15,101
  • 33
  • 107
  • 174
0

You can configure it using <classesDirectory>[Your DIR]</classesDirectory>

ravinikam
  • 3,666
  • 6
  • 28
  • 25
0

In cobertura-maven-plugin version 2.4 this is still not supported. I've just created an improvement ticket, patch is attached to the ticket.

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
Stevo Slavić
  • 2,298
  • 2
  • 24
  • 32