{"id":1798,"date":"2015-04-14T12:00:59","date_gmt":"2015-04-14T11:00:59","guid":{"rendered":"http:\/\/www.unicoda.com\/?p=1798"},"modified":"2015-04-09T12:15:39","modified_gmt":"2015-04-09T11:15:39","slug":"enabling-code-coverage-in-sonar-from-jenkins-with-maven-using-jacoco","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=1798","title":{"rendered":"Enabling code coverage : in Sonar, from Jenkins, with Maven, using Jacoco."},"content":{"rendered":"<p>Let&rsquo;s assume a few think before we begin.<\/p>\n<ul>\n<li>SonarQube is installed somewhere and works.<\/li>\n<li>The task \u00ab\u00a0Invoke Standalone Sonar Analysis\u00a0\u00bb is available in Jenkins.<\/li>\n<li>Your project is using Maven so it has a pom.xml.<\/li>\n<\/ul>\n<p>To begin, we&rsquo;ll add configuration in our pom.xml.<\/p>\n<pre>&lt;properties&gt;\r\n  &lt;sonar.core.codeCoveragePlugin&gt;jacoco&lt;\/sonar.core.codeCoveragePlugin&gt;\r\n  &lt;sonar.jacoco.reportPath&gt;${project.basedir}\/..\/target\/jacoco.exec&lt;\/sonar.jacoco.reportPath&gt;\r\n  &lt;sonar.language&gt;java&lt;\/sonar.language&gt;\r\n&lt;\/properties&gt;<\/pre>\n<p>We&rsquo;re using JUnit to run tests.<\/p>\n<pre>&lt;dependencies&gt;\r\n  &lt;dependency&gt;\r\n    &lt;groupId&gt;junit&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;junit&lt;\/artifactId&gt;\r\n    &lt;version&gt;4.11&lt;\/version&gt;\r\n    &lt;scope&gt;test&lt;\/scope&gt;\r\n  &lt;\/dependency&gt;\r\n&lt;\/dependencies&gt;<\/pre>\n<p><!--more-->Now, we configure our plugins.<\/p>\n<pre>&lt;build&gt;\r\n  &lt;plugins&gt;\r\n    &lt;plugin&gt;\r\n      &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;maven-surefire-plugin&lt;\/artifactId&gt;\r\n      &lt;version&gt;2.18.1&lt;\/version&gt;\r\n    &lt;\/plugin&gt;\r\n    &lt;plugin&gt;\r\n      &lt;groupId&gt;org.jacoco&lt;\/groupId&gt;\r\n      &lt;artifactId&gt;jacoco-maven-plugin&lt;\/artifactId&gt;\r\n      &lt;version&gt;0.7.2.201409121644&lt;\/version&gt;\r\n      &lt;configuration&gt;\r\n        &lt;append&gt;true&lt;\/append&gt;\r\n      &lt;\/configuration&gt;\r\n      &lt;executions&gt;\r\n        &lt;execution&gt;\r\n          &lt;goals&gt;\r\n            &lt;goal&gt;prepare-agent&lt;\/goal&gt;\r\n          &lt;\/goals&gt;\r\n        &lt;\/execution&gt;\r\n        &lt;execution&gt;\r\n          &lt;id&gt;post-unit-test&lt;\/id&gt;\r\n          &lt;phase&gt;test&lt;\/phase&gt;\r\n          &lt;goals&gt;\r\n            &lt;goal&gt;report&lt;\/goal&gt;\r\n          &lt;\/goals&gt;\r\n        &lt;\/execution&gt;\r\n      &lt;\/executions&gt;\r\n    &lt;\/plugin&gt;\r\n  &lt;\/plugins&gt;\r\n&lt;\/build&gt;<\/pre>\n<p>Everything should be good regarding Maven configuration, so we&rsquo;ll switch to Jenkins configuration. In your job, you should add the task \u00ab\u00a0Invoke Standalone Sonar Analysis\u00a0\u00bb. The interesting part is the \u00ab\u00a0project properties\u00a0\u00bb field.<\/p>\n<pre># Metadata\r\nsonar.projectName=${JOB_NAME}\r\nsonar.projectVersion=1.0.0\r\n\r\n# Source information\r\nsonar.sources=src\/main\r\nsonar.sourceEncoding=UTF-8\r\nsonar.language=java\r\n\r\n# Tests\r\nsonar.tests=src\/test\r\nsonar.junit.reportsPath=target\/surefire-reports\r\nsonar.surefire.reportsPath=target\/surefire-reports\r\nsonar.jacoco.reportPath=target\/jacoco.exec\r\nsonar.binaries=target\/classes\r\nsonar.java.coveragePlugin=jacoco\r\n\r\n# Debug\r\nsonar.verbose=true\r\n<\/pre>\n<p>Don&rsquo;t forget <em>sonar.binaries<\/em>, otherwise, you might get something like \u00ab\u00a0Project coverage is set to 0% since there is no directories with classes.\u00a0\u00bb in your logs.<\/p>\n<p>In addition, if I&rsquo;m doing a <em>mvn clean test<\/em>, I get the following files in directory <em>target<\/em> (among others):<\/p>\n<ul>\n<li>classes\/<\/li>\n<li>surefire-reports\/<\/li>\n<li>jacoco.exec<\/li>\n<\/ul>\n<p>Using this setup, I manage to display information regarding test coverage and test execution. Besides, there might be some duplicate as for <em>sonar.surefire.reportsPath<\/em> and <em>sonar.junit.reportsPath<\/em> in sonar properties.<\/p>\n<p><a href=\"http:\/\/www.unicoda.com\/wp-content\/uploads\/2015\/04\/sonarTestCoverage.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1800 size-full\" src=\"http:\/\/www.unicoda.com\/wp-content\/uploads\/2015\/04\/sonarTestCoverage.png\" alt=\"Test Coverage in SonarQube\" width=\"701\" height=\"192\" srcset=\"https:\/\/www.unicoda.com\/wp-content\/uploads\/2015\/04\/sonarTestCoverage.png 701w, https:\/\/www.unicoda.com\/wp-content\/uploads\/2015\/04\/sonarTestCoverage-300x82.png 300w, https:\/\/www.unicoda.com\/wp-content\/uploads\/2015\/04\/sonarTestCoverage-500x137.png 500w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&rsquo;s assume a few think before we begin. SonarQube is installed somewhere and works. The task \u00ab\u00a0Invoke Standalone Sonar Analysis\u00a0\u00bb is available in Jenkins. Your project is using Maven so it has a pom.xml. To begin, we&rsquo;ll add configuration in our pom.xml. &lt;properties&gt; &lt;sonar.core.codeCoveragePlugin&gt;jacoco&lt;\/sonar.core.codeCoveragePlugin&gt; &lt;sonar.jacoco.reportPath&gt;${project.basedir}\/..\/target\/jacoco.exec&lt;\/sonar.jacoco.reportPath&gt; &lt;sonar.language&gt;java&lt;\/sonar.language&gt; &lt;\/properties&gt; We&rsquo;re using JUnit to run tests. &lt;dependencies&gt; &lt;dependency&gt; &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=1798\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;Enabling code coverage : in Sonar, from Jenkins, with Maven, using Jacoco.&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,126],"tags":[230,62,227,229,226,225,231,228],"class_list":["post-1798","post","type-post","status-publish","format-standard","hentry","category-code","category-logiciellibre","tag-jacoco","tag-java","tag-jenkins","tag-junit","tag-maven","tag-sonar","tag-sonarqube","tag-test-coverage"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1798"}],"version-history":[{"count":5,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1798\/revisions"}],"predecessor-version":[{"id":1804,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1798\/revisions\/1804"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}