Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven - Tweaking the phase where to run a plugin declared in the reporting section

I am trying to tweak the phase when a maven plugin execution will run in maven-2.

My specific issue is with attempting to run the cobertura:instrument step bound to the lifecycle phase process-test-classes, so that it doesn't conflict with other plugins that use aspectj (and remove instrumentation code, thus generating a coverage report of 0%). But my question is more generic.

Within the deafault lifecycle, I have managed to do that by adding an executions section in my plugin declaration:

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>instrument-late</id>
                    <phase>process-test-classes</phase>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        ...

This way, when I run mvn test everything works fine, cobertura:instrument is run at the phase I want, classes get instrumented, tests run with instrumented classes, etc. This is summarized output:

[INFO] [clean:clean {execution: default-clean}]
[INFO] [buildnumber:create {execution: default}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-utf8}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-8859_1}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] [compiler:compile {execution: default-compile}]
[INFO] [jar:jar {execution: lib}]
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Preparing hibernate3:hbm2ddl
[WARNING] Removing: hbm2ddl from forked lifecycle, to prevent recursive invocation.
[INFO] [buildnumber:create {execution: default}]
[INFO] Change the default 'svn' provider implementation to 'javasvn'.
[INFO] Checking for local modifications: skipped.
[INFO] Updating project files from SCM: skipped.
[INFO] Storing buildNumber: 2082 at timestamp: 1299861835678
[INFO] Storing buildScmBranch: trunk
[INFO] [native2ascii:native2ascii {execution: native2ascii-utf8}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-8859_1}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] [hibernate3:hbm2ddl {execution: default}]
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] [jar:test-jar {execution: tests}]
[INFO] [dbunit:operation {execution: test-compile}]
[INFO] [cobertura:instrument {execution: instrument-late}]
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: /home/carles/dev/ism4web/portasigma/portasigma-web/target/surefire-reports

...

Results :

Tests run: 62, Failures: 0, Errors: 0, Skipped: 0

Flushing results...
Flushing results done
Cobertura: Loaded information on 74 classes.
Cobertura: Saved information on 74 classes.
[INFO] [dbunit:operation {execution: test}]

However, when I do mvn site I seem to have no control over the execution phase for the plugin. My reporting section contains:

<reporting>
    <plugins>
        ...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
        </plugin>

And the output of mvn site (summarized) says:

[INFO] [clean:clean {execution: default-clean}]
[INFO] [buildnumber:create {execution: default}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-utf8}]
[INFO] [native2ascii:native2ascii {execution: native2ascii-8859_1}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] [compiler:compile {execution: default-compile}]
[INFO] [jar:jar {execution: lib}]
[INFO] [cobertura:instrument {execution: default-instrument}]
[INFO] [hibernate3:hbm2ddl {execution: default}]
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] [jar:test-jar {execution: tests}]
[INFO] [dbunit:operation {execution: test-compile}]
[INFO] [cobertura:instrument {execution: instrument-late}]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Unable to prepare instrumentation directory.
Embedded error: source and destination are the same directory.

Instrument is called twice, one at the (I presume) default phase defined by the plugin, and another one at my redefined phase. I guess that comes from the plugin running during as part of the site lifecycle.

THE QUESTION: I haven't found how to tweak the plugin execution within the reporting section / site lifecycle. Any hints?

like image 561
Carles Barrobés Avatar asked Mar 11 '11 17:03

Carles Barrobés


People also ask

In which section of POM do you configure Maven plugins?

Build plugins They execute during the build process and should be configured in the <build/> element of pom.

Where do I put plugins in POM xml?

<plugin> should be placed into <plugins> section which should be placed into <build> or <pluginManagement> section. The order of <dependency> or <build> section doesn't matter. I have a multi pom project for api automation testing. One of the maven projects has automation tests.

What are the correct type of Maven plugins?

Introduction. In Maven, there are two kinds of plugins, build and reporting: Build plugins are executed during the build and configured in the <build/> element. Reporting plugins are executed during the site generation and configured in the <reporting/> element.


1 Answers

I'm guessing here but I think what is happening is that because the cobertura goal runs in its own lifecycle the instrumentation step is called twice, once from the maven lifecycle and once from the cobertura lifecycle.

like image 144
Gaurav Avatar answered Oct 02 '22 03:10

Gaurav