我已按照以下链接获取多模块项目的代码覆盖率。 https://www.petrikainulainen.net/programming/maven/creating-code-coverage-reports-for-unit-and-integration-tests-with-the-jacoco-maven-plugin/
Jenkins ver。 1.631 声纳版。 5.1.1 Jacoco ver。 0.7.9 Java版本1.8 Maven ver。 3.3.9
主Pom文件 -
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>test</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
<commons.lang.version>2.5</commons.lang.version>
<log4j.version>1.2.15</log4j.version>
<org.jboss.hibernate.version>5.2.5.Final</org.jboss.hibernate.version>
<jaxb.version>2.1</jaxb.version>
<guava.version>19.0</guava.version>
<jersey.version>2.24.1</jersey.version>
<jackson.version>2.8.5</jackson.version>
<jackson.databind.version>2.8.1</jackson.databind.version>
<cxf.version>3.1.4</cxf.version>
<tomcat.version>7.0.47</tomcat.version>
<tomcat.port>18080</tomcat.port>
<tomcat.client.port>28080</tomcat.client.port>
<server.version>1.0.0</server.version>
<maven.build.timestamp.format>yyyy-MM-dd'T'HH:mm:ss.SSS z</maven.build.timestamp.format>
<olingo.version>2.0.6</olingo.version>
<felix.version>5.4.0</felix.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version> <type>jar</type> <scope>compile</scope> </dependency> -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3-1102-jdbc41</version>
<!-- 9.2-1002-jdbc4 -->
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${org.springframework.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${org.springframework.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${org.jboss.hibernate.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<exclusions>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>opensymphony</groupId>
<artifactId>quartz</artifactId>
<version>1.6.3</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind.version}</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.felix/org.apache.felix.framework -->
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
<version>${felix.version}</version>
</dependency>
</dependencies>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>module4</module>
<module>module5</module>
<module>module6</module>
<module>module7</module>
<module>module8</module>
</modules>
<profiles>
<!-- Default profile excludes connectors netsuite and salesforce in module1
and module2 and skips all module3 tests. -->
<profile>
<id>no-connector</id>
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
<module>module4</module>
<module>module5</module>
<module>module6</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<argLine>${failsafeArgLine}</argLine>
<excludedGroups>salesforce, netsuite, netsuitesso, GoogleDirectoryProvider,hybris4,hybris,hybris5, ftp</excludedGroups>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${surefireArgLine}</argLine>
<excludedGroups>salesforce, netsuite, netsuitesso, netsuite_min, GoogleDirectoryProvider, hybris4, hybris, hybris5, ftp</excludedGroups>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.17</version>
<reportSets>
<reportSet>
<reports>
<report>report-only</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-pmd-plugin</artifactId>
<version>2.4</version> <configuration> <linkXref>true</linkXref> <sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>100</minimumTokens> <targetJdk>1.7</targetJdk> <rulesets>
<ruleset>/rulesets/basic.xml</ruleset> <ruleset>/rulesets/imports.xml</ruleset>
<ruleset>/rulesets/unusedcode.xml</ruleset> <ruleset>/rulesets/controversial.xml</ruleset>
<ruleset>/rulesets/coupling.xml</ruleset> <ruleset>/rulesets/optimizations.xml</ruleset>
<ruleset>/rulesets/design.xml</ruleset> <ruleset>/rulesets/migrating.xml</ruleset>
<ruleset>/rulesets/typeresolution.xml</ruleset> </rulesets> </configuration>
</plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>findbugs-maven-plugin</artifactId>
<version>2.3.2</version> <configuration> <findbugsXmlOutput>true</findbugsXmlOutput>
<findbugsXmlOutputDirectory>target/site</findbugsXmlOutputDirectory> <xmlOutput>true</xmlOutput>
</configuration> </plugin> -->
</plugins>
</reporting>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
我在Module1和Module2 Pom xml中添加了下面的插件配置,我在那里进行了集成测试。
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.9</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
</configuration>
</plugin>
Maven Build Command -
-DargLine="-DDB_SERVER=localhost -DDB_PORT=1234 -DDB_NAME=test -DDB_USER=tester -DDB_PASSWORD=testerpwd -DDB_MAX_POOL=10" -Dcom.tester.redis=false clean verify
我使用Jenkins执行此操作时遇到错误。
[Checking] $ /Users/test-test/.jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Sonar_Runner/bin/sonar-runner -Dsonar.jdbc.url=jdbc:mysql://123.45.65/sonar?autoReconnect=true&useUnicode=true&characterEncoding=utf8 -Dsonar.jdbc.username=abcd -Dsonar.jdbc.password=abcd@123 -e -Dsonar.host.url=http://123.45.65:9000 ******** ******** -Dsonar.projectName=test-Test -Dsonar.projectVersion=28-06-2017-23 -Dsonar.jacoco.reportPath=reports/jacoco/jacoco-ut.exec -Dsonar.projectKey=test-Test -Dsonar.jacoco.itReportPath=reports/jacoco/jacoco-it.exec -Dsonar.java.coveragePlugin=jacoco -Dsonar.sources=. -Dsonar.modules=module1,module2,module3,module4,module5 -Dsonar.projectBaseDir=/Users/test-test/.jenkins/test/test/Checking
SonarQube Runner 2.3
Java 1.8.0_60 Oracle Corporation (64-bit)
Mac OS X 10.11.6 x86_64
INFO: Error stacktraces are turned on.
INFO: Runner configuration file: /Users/test-test/.jenkins/tools/hudson.plugins.sonar.SonarRunnerInstallation/Sonar_Runner/conf/sonar-runner.properties
INFO: Project configuration file: NONE
INFO: Default locale: "en_US", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Work directory: /Users/test-test/.jenkins/test/test/Checking/.sonar
INFO: SonarQube Server 5.1.1
18:16:25.065 INFO - Load global repositories
18:16:25.337 INFO - Load global repositories (done) | time=283ms
18:16:25.339 INFO - Server id: 20170616093959
18:16:25.344 INFO - User cache: /Users/test-test/.sonar/cache
18:16:25.354 INFO - Install plugins
18:16:27.168 INFO - Install JDBC driver
18:16:27.187 INFO - Create JDBC datasource for jdbc:mysql://172.26.72.76/sonar?autoReconnect=true&useUnicode=true&characterEncoding=utf8
18:16:28.882 INFO - Initializing Hibernate
18:16:30.622 WARN - /!\ A multi-module project can't have source folders, so '/Users/test-test/.jenkins/test/test/Checking' won't be used for the analysis. If you want to analyse files of this folder, you should create another sub-module and move them inside it.
18:16:31.095 INFO - Load project repositories
18:16:31.323 INFO - Load project repositories (done) | time=228ms
18:16:31.323 INFO - Load project settings
18:16:31.801 INFO - Load technical debt model
18:16:31.852 INFO - Apply project exclusions
18:16:34.302 WARN - SCM provider autodetection failed. No SCM provider claims to support this project. Please use sonar.scm.provider to define SCM of your project.
18:16:34.304 INFO - ------------- Scan module1
18:16:34.314 INFO - Load module settings
18:16:34.707 INFO - Load rules
18:16:35.242 INFO - Base dir: /Users/test-test/.jenkins/test/test/Checking/module1
18:16:35.242 INFO - Working dir: /Users/test-test/.jenkins/test/test/Checking/.sonar/test-Test_admin
18:16:35.242 INFO - Source paths: .
18:16:35.243 INFO - Source encoding: UTF-8, default locale: en_US
18:16:35.243 INFO - Index files
18:16:35.948 INFO - 78 files indexed
18:16:39.945 INFO - Quality profile for java: QPtest
18:16:40.081 INFO - Trying to guess scm provider from project layout...
18:16:40.113 INFO - Didn't find which SCM provider is used. Fallback on configuration
18:16:40.113 WARN - SCM URL must not be blank. SCM Stats Plugin will not run.Please check the parameter SCM URL or the <scm> section of Maven pom.
18:16:40.149 INFO - Sensor JavaSquidSensor
18:16:40.456 INFO - Configured Java source version: none
18:16:40.841 INFO - Java Main Files AST scan...
18:16:40.844 INFO - 78 source files to be analyzed
18:16:50.845 INFO - 13/78 files analyzed, current file: /Users/test-test/.jenkins/test/test/Checking/module1/src/main/java/com/test/services/impl/ApplicationService.java
18:17:00.849 INFO - 25/78 files analyzed, current file: /Users/test-test/.jenkins/test/test/Checking/module1/src/main/java/com/test/services/impl/exampleServiceService.java
18:17:10.849 INFO - 37/78 files analyzed, current file: /Users/test-test/.jenkins/test/test/Checking/module1/src/main/java/com/test/services/impl/StarterProject.java
18:17:20.851 INFO - 74/78 files analyzed, current file: /Users/test-test/.jenkins/test/test/Checking/module1/src/main/java/com/test/test/client/ServiceProviderIT.java
18:17:21.904 INFO - 78/78 source files have been analyzed
18:17:21.904 INFO - Java Main Files AST scan done: 41063 ms
18:17:21.904 WARN - Java bytecode has not been made available to the analyzer. The org.sonar.java.bytecode.visitor.DependenciesVisitor@585fa2fb, org.sonar.java.checks.UnusedPrivateMethodCheck@2434f548, org.sonar.java.checks.UnusedProtectedMethodCheck@72bdb6d3, CycleBetweenPackages rule are disabled.
18:17:21.904 INFO - Java Test Files AST scan...
18:17:21.904 INFO - 0 source files to be analyzed
18:17:21.904 INFO - Java Test Files AST scan done: 0 ms
18:17:21.905 INFO - 0/0 source files have been analyzed
18:17:21.905 INFO - Sensor JavaSquidSensor (done) | time=41756ms
18:17:21.906 INFO - Sensor Lines Sensor
18:17:21.911 INFO - Sensor Lines Sensor (done) | time=5ms
18:17:21.911 INFO - Sensor QProfileSensor
18:17:21.915 INFO - Sensor QProfileSensor (done) | time=4ms
18:17:21.915 INFO - Sensor InitialOpenIssuesSensor
18:17:23.303 INFO - Sensor InitialOpenIssuesSensor (done) | time=1388ms
18:17:23.303 INFO - Sensor ProjectLinksSensor
18:17:23.339 INFO - Sensor ProjectLinksSensor (done) | time=36ms
18:17:23.339 INFO - Sensor VersionEventsSensor
18:17:23.420 INFO - Sensor VersionEventsSensor (done) | time=81ms
18:17:23.420 INFO - Sensor CoberturaSensor
18:17:23.420 WARN - Cobertura report not found at /Users/test-test/.jenkins/test/test/Checking/module1/target/site/cobertura/coverage.xml
18:17:23.420 INFO - Sensor CoberturaSensor (done) | time=0ms
18:17:23.420 INFO - Sensor org.sonar.plugins.findbugs.FindbugsSensor@5bebff4b
18:17:23.451 WARN - Findbugs needs sources to be compiled. Please build project before executing sonar or check the location of compiled classes to make it possible for Findbugs to analyse your project.
18:17:23.451 INFO - Sensor org.sonar.plugins.findbugs.FindbugsSensor@5bebff4b (done) | time=31ms
18:17:23.451 INFO - Sensor SurefireSensor
18:17:23.451 INFO - parsing /Users/test-test/.jenkins/test/test/Checking/module1/target/surefire-reports
18:17:23.451 ERROR - Reports path not found or is not a directory: /Users/test-test/.jenkins/test/test/Checking/module1/target/surefire-reports
我的声纳。属性如下 -
sonar.projectKey=test-Test
sonar.projectName=test-Test
sonar.projectVersion=$PipelineId
sonar.modules=module1,module2,module3,module4,module5
sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPath=reports/jacoco/jacoco-ut.exec
sonar.jacoco.itReportPath=reports/jacoco/jacoco-it.exec
sonar.sources=.
Sonar IMG将null显示为单位覆盖率。
如何包含UT和IT以获得良好的无代码覆盖率。