Maven插件上的Cobertura代码覆盖测试

时间:2014-07-21 15:10:27

标签: java maven maven-plugin cobertura maven-cobertura-plugin

我正在尝试为我开发的简单Maven插件生成代码覆盖率报告。 Cobertura在我的项目中正确生成了三个类的报告,但即使测试成功执行,它也会报告0%的代码覆盖率。我已经在调试模式下运行它,并且没有Cobertura报告的错误或堆栈跟踪。

我在POM文件中的配置非常简单:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-artifact</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-compat</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.0.5</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
    </dependency> 

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.0.5</version>
    </dependency>  

    <dependency>
        <groupId>org.apache.maven.plugin-tools</groupId>
        <artifactId>maven-plugin-annotations</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <!-- version 2.1 uses sonatype aether. anything after 2.1 uses eclipse aether. -->
        <groupId>org.apache.maven.plugin-testing</groupId>
        <artifactId>maven-plugin-testing-harness</artifactId>
        <scope>test</scope>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.twdata.maven</groupId>
        <artifactId>mojo-executor-maven-plugin</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <type>maven-plugin</type>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <executions>
                <execution>
                    <id>test-custom-plugin</id>
                    <phase>test</phase>
                    <goals> 
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <forkMode>never</forkMode>
                        <forkCount>0</forkCount>
                        <reuseForks>true</reuseForks>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-plugin-plugin</artifactId>
            <version>3.2</version>
            <configuration>
                <goalPrefix>MyCustomPlugin</goalPrefix>
            </configuration>
            <executions>
                <execution>
                    <id>default-descriptor</id>
                    <goals>
                        <goal>descriptor</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
                <execution>
                    <id>help-descriptor</id>
                    <goals>
                        <goal>helpmojo</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.5.2</version>
        </plugin>
    </plugins>
</build>    

Cobertura适用于我所有其他项目(到目前为止),是否有任何理由不能报告Maven插件项目的覆盖范围?

1 个答案:

答案 0 :(得分:0)

似乎问题出在我的surefire配置上。我把它改成了

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <executions>
                <execution>
                    <id>test-custom-plugin</id>
                    <phase>test</phase>
                    <goals> 
                        <goal>test</goal>
                    </goals>
                    <configuration>
                        <forkCount>1</forkCount>
                        <reuseForks>true</reuseForks>
                    </configuration>
                </execution>
            </executions>
        </plugin>

现在可以正确生成报告。我想到了从我看到的与为声纳插件生成报告相关的类似问题的一些帖子中更改fork选项的想法,并且更改fork选项解决了这个问题。