Java – How to change the default output from a Maven 2 / Cobertura instrument goal

coberturacode-coveragejavamaven-2testing

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.

Best Answer

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)