POM中配置的如下:
    在通过mvn test执行了单元测试后,将会在工程的父级目录target/中生成surefire-reports子文件夹(包含.txt和.xml两种形式)
    注意:如果不以上方的方式命名,不会生成报告;若引用的为Testng框架则不受此约束

    其实以上都是为了在已配置的sonarqube平台中’代码覆盖率’作准备工作的,那么接下来最后执行如此即可:

    1. mvn clean test cobertura:cobertura -Dcobertura.report.format=xml sonar:sonar

    mvn clean test cobertura:cobertura -Dcobertura.report.format=xml sonar:sonar

    附上一个Demo示例图
    Maven插件之maven-surefire-plugin,junit单元测试报告和sonar测试覆盖率的整合说明 - 图1
    Maven插件之maven-surefire-plugin,junit单元测试报告和sonar测试覆盖率的整合说明 - 图2

    以上是本人花了两天时间总结而来的精华,翻阅了大量的网络资料。最后附上一个在实施完成之后的文章,个人推荐:http://seanzhou.iteye.com/blog/1393858

    另外,Demo的pom.xml写得并没有网上的那么复杂,如下:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0</modelVersion>
    5. <groupId>SelfAnyTest</groupId>
    6. <artifactId>SelfAnyTest</artifactId>
    7. <version>0.0.1-SNAPSHOT</version>
    8. <name>SelfAnyTest</name>
    9. <description>非项目用的测试与调试工程</description>
    10. <dependencies>
    11. <dependency>
    12. <groupId>junit</groupId>
    13. <artifactId>junit</artifactId>
    14. <version>4.12</version>
    15. <scope>test</scope>
    16. </dependency>
    17. </dependencies>
    18. <distributionManagement>
    19. <snapshotRepository>
    20. <id>ossrh</id>
    21. <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    22. </snapshotRepository>
    23. <repository>
    24. <id>ossrh</id>
    25. <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
    26. </repository>
    27. </distributionManagement>
    28. <build>
    29. <resources>
    30. <resource>
    31. <directory>src/main/resources</directory>
    32. </resource>
    33. </resources>
    34. <plugins>
    35. <plugin>
    36. <groupId>org.apache.maven.plugins</groupId>
    37. <artifactId>maven-source-plugin</artifactId>
    38. <version>2.2.1</version>
    39. </plugin>
    40. <plugin>
    41. <groupId>org.apache.maven.plugins</groupId>
    42. <artifactId>maven-javadoc-plugin</artifactId>
    43. <version>2.9.1</version>
    44. </plugin>
    45. <plugin>
    46. <groupId>org.apache.maven.plugins</groupId>
    47. <artifactId>maven-compiler-plugin</artifactId>
    48. <version>3.3</version>
    49. <!-- <configuration>
    50. <source>1.7</source>
    51. <target>1.7</target>
    52. <compilerId>eclipse</compilerId>
    53. </configuration> -->
    54. <dependencies>
    55. <dependency>
    56. <groupId>org.codehaus.plexus</groupId>
    57. <artifactId>plexus-compiler-eclipse</artifactId>
    58. <version>2.5</version>
    59. </dependency>
    60. </dependencies>
    61. </plugin>
    62. <plugin>
    63. <groupId>org.apache.maven.plugins</groupId>
    64. <artifactId>maven-surefire-plugin</artifactId>
    65. <version>2.19</version>
    66. </plugin>
    67. </plugins>
    68. </build>
    69. </project>

    因为maven有很多依赖可以非显示的写入,具体的受限于精力及非服务需要,未作探究。