Tests running twice when using cobertura and maven site plugin

5.1k Views Asked by At

I am using maven site plugin and cobertura together to run unit tests and generate report. Everything is working fine , but only problem is all unit tests are running twice.

I tried to set forkMode as never for maven-site-plugin but even then I am facing the same problem.

Any help would be appreciated.

My command: mvn cobertura:cobertura -Dcobertura.report.format=html

My pom:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <configuration>
                    <aggregate>true</aggregate>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <configuration>
                    <aggregate>false</aggregate>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>findbugs-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
            </plugin>
        </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
                <instrumentation>
                    <excludes>
                        <exclude>**/test/**/*.class</exclude>
                    </excludes>
                </instrumentation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-pmd-plugin</artifactId>
            <version>3.0.1</version>
            <configuration>
                <targetJdk>1.6</targetJdk>
                <linkXref>true</linkXref>
                <sourceEncoding>ISO-8859-1</sourceEncoding>
                <format>xml</format>
                <aggregate>true</aggregate>
                <verbose>true</verbose>
                <rulesets>
                    <ruleset>favorites.xml</ruleset>
                </rulesets>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-javadoc-plugin</artifactId>
            <version>2.9.1</version>
            <configuration>
                <aggregate>true</aggregate>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>findbugs-maven-plugin</artifactId>
            <version>2.5.2</version>
            <configuration>
                <xmlOutput>true</xmlOutput>
            </configuration>
        </plugin>
    </plugins>
</reporting>
3

There are 3 best solutions below

1
On

I believe this is a normal behaviour.

You are using two distinct reports which bases on the same thing :

  • the test report requires the test to be run but does not enable coverage.
  • the coverage report requires the test to be run with coverage.

But both reports does not know of each other, that why it is run twice.

[UPDATE] after reading this mailing list, it says that you should disable the test (using skipTests) preferably in a profile.

As for your command line, it would give:

mvn cobertura:cobertura -DskipTests -Dcobertura.report.format=html

Note however that I could not get cobertura working (got Encountered " "final" "final "" at line 106, column 12.) with my project, so I don't know if it worked.

0
On

I ended up in creating 2 profiles, one for cobertura and another for site, which will build findbugs, CPD and PMD analysis. Not sure if this is the correct way, but solves my problem.

Hope this will be helpful for somebody.

1
On

Tests will always be run twice with the cobertura-maven-plugin.

If you need cobertura reports and want tests to be run only once, you can try the qualinsight-mojo-cobertura-core plugin. You'll find the documentation on the project's page: https://github.com/QualInsight/qualinsight-mojo-cobertura .