4

I'm converting an ANT build to Maven. I don't use Sonar.

In Maven, Jacoco doesn't seem to report about coverage of the unit tests themselves, while ANT does. I've been trying to get this for my Maven build as well, but I haven't been able to find anything.

It seems like I should add an <include> to the prepare-agent goal, but I'm not sure what to include. I've tried src/test/java/* and all kinds of variations on that theme, but none works.

How can I configure Jacoco in Maven such that it does report the coverage of unit test code?

4

1 回答 1

4

事实证明,这样做的唯一方法是使用maven-antrun-plugin.

无需向目标添加 an <include>prepare-agent因为所有信息都存在于jacoco.exec它生成的文件中,包括单元测试代码。

但是,report目标不包括它,也不能配置为使用它。您需要专门设置classfilessourcefiles属性,而 Maven Jacoco 插件不允许您这样做。

因此,您需要 Maven Antrun 插件,并从那里配置和调用它。

<plugins>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
        <executions>
            <execution>
                <id>default-prepare-agent</id>
                <goals>
                    <goal>prepare-agent</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
            <execution>
                <id>default-report</id>
                <phase>package</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <target>
                        <taskdef name="report" classname="org.jacoco.ant.ReportTask" classpathref="maven.plugin.classpath" />
                        <report>
                            <executiondata>
                                <file file="${project.build.directory}/jacoco.exec" />
                            </executiondata>
                            <structure name="Coverage">
                                <classfiles>
                                    <fileset dir="${project.build.directory}/classes"/>
                                    <fileset dir="${project.build.directory}/test-classes"/>
                                </classfiles>
                                <sourcefiles encoding="UTF-8">
                                    <fileset dir="src/main/java"/>
                                    <fileset dir="src/test/java"/>
                                </sourcefiles>
                            </structure>
                            <check failonviolation="true" violationsproperty="violation">
                                <rule element="BUNDLE">
                                    <limit counter="INSTRUCTION" value="COVEREDRATIO" minimum="0.95" />
                                </rule>
                            </check>
                            <html destdir="${project.build.directory}/jacoco-internal"/>
                        </report>
                    </target>
                </configuration>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>org.jacoco</groupId>
                <artifactId>org.jacoco.ant</artifactId>
                <version>${jacoco.version}</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>
于 2014-10-14T09:56:58.337 回答