我们这里没有声纳,现在,我们无法安装它。所以我必须找到一种解决方法并得到一个。此解决方案适用mvn clean install -DrunCobertura=true
于多模块项目中的简单项目。您只需将此配置文件添加到您super pom.xml
的项目中,定义working.dir
属性,它应该可以工作。
<profile>
<id>runCobertura</id>
<activation>
<property>
<name>runCobertura</name>
<value>true</value>
</property>
</activation>
<properties>
<cobertura.format>html</cobertura.format>
<cobertura.working.dir>${working.dir}/${project.version}/cobertura</cobertura.working.dir>
<cobertura.complete.ser.file>${cobertura.working.dir}/complete.ser</cobertura.complete.ser.file>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<inherited>false</inherited>
<configuration>
<filesets>
<fileset>
<directory>.</directory>
<includes>
<include>cobertura.ser</include>
</includes>
</fileset>
<fileset>
<directory>${cobertura.working.dir}</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>cobertura-Instrument</id>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<taskdef resource="tasks.properties"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<if>
<available file="${project.build.outputDirectory}"/>
<then>
<cobertura-instrument>
<fileset dir="${project.build.outputDirectory}">
<include name="**/*.class"/>
</fileset>
</cobertura-instrument>
</then>
</if>
</target>
</configuration>
</execution>
<execution>
<id>cobertura-createCombinedSerFile</id>
<phase>generate-test-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<taskdef resource="tasks.properties"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<if>
<available file="${cobertura.complete.ser.file}"/>
<then>
<cobertura-merge datafile="${basedir}/tmp.ser">
<fileset file="${cobertura.complete.ser.file}"/>
<fileset file="${basedir}/cobertura.ser"/>
</cobertura-merge>
<move file="${basedir}/tmp.ser" tofile="${basedir}/cobertura.ser"/>
</then>
</if>
</target>
</configuration>
</execution>
<execution>
<id>cobertura-copyResultSerFileAndSources</id>
<phase>test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<taskdef resource="tasks.properties"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<if>
<available file="${basedir}/cobertura.ser"/>
<then>
<move file="${basedir}/cobertura.ser" tofile="${cobertura.complete.ser.file}"/>
<mkdir dir="${cobertura.working.dir}/source"/>
<if>
<available file="${basedir}/src/main/java"/>
<then>
<copy todir="${cobertura.working.dir}/source">
<fileset dir="src/main/java">
<include name="**/*.java"/>
</fileset>
</copy>
</then>
</if>
<cobertura-report datafile="${cobertura.complete.ser.file}" format="${cobertura.format}" destdir="${cobertura.working.dir}/report">
<fileset dir="${cobertura.working.dir}/source"/>
</cobertura-report>
</then>
</if>
</target>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>1.9.4.1</version>
</dependency>
<dependency>
<groupId>ant-contrib</groupId>
<artifactId>ant-contrib</artifactId>
<version>20020829</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.sourceforge.cobertura</groupId>
<artifactId>cobertura</artifactId>
<version>1.9.4.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
它有什么作用:
1. @process-classes
- 检测模块的编译类。
2. @generate-test-sources
- 将先前模块中的.ser
文件与创建的该模块之一合并,以获得完整的代码覆盖率。
3. @test
- 创建代码覆盖率报告。应该在最后一个模块中调用,但由于最后一个模块可以更改,我总是调用它并且以前的报告将被覆盖。如果您使用xml
格式的报告(对于 Jenkins),它会很快,所以没关系。