CD part XIV: Creating the Sonar job in Jenkins

Johan Tuitel

  • 10/02/2017
  • 5 minuten leestijd
Johan Tuitel

CD part XIV: Creating the Sonar job in Jenkins

After installing jenkins, we need to configure the GIT plugin and the SonarQube plugin. When this is done we need to create the jenkins job and adding JaCoCo-plugin for unittest-coverage

install git plugin in jenkins

  1. we start the tomcat instance for jenkins by executing the tomcat jenkins - start.cmd script
  2. open a browser and navigate to: http://localhost:8075/jenkins/
  3. click in the menu on Manage Jenkins
  4. click on Manage Plugins
  5. select the Available tab
  6. search for SonarQube plugin
  7. install this plugin and restart jenkins

git plugin installed

integrate sonarqube and jenkins

  1. we start the tomcat instance for jenkins by executing the tomcat jenkins - start.cmd script
  2. open a browser and navigate to: http://localhost:8075/jenkins/
  3. click in the menu on Manage Jenkins
  4. click on the Configure link
  5. navigate to the SonarQube servers section
  6. click on the Add SonarQube
    1. Name: SonarQube 5.4.
    2. Server URL: http://localhost:9000/
    3. Server Version: 5.3 or higher

create continuous integration job

  1. we start the tomcat instance for jenkins by executing the tomcat jenkins - start.cmd script
  2. open a browser and navigate to: http://localhost:8075/jenkins/
  3. click in the sidemenu on New Job
  4. for this exercise, we are using the account status webservice
  5. call this item: wsaccountstatus_sonar
  6. select the first radiobutton create a freestyle project
  7. in the Source Code Management section select the Git radiobutton
  8. enter the url: https://bitbucket.org/johantuitel/easywebservices.git
  9. select the credentials you have made for your bitbucket account
  10. in my case it was j.tuitel@developers.nl/*(bitbucket account)
  11. after that we need to set the correct branch in the field Branches to build
  12. enter: develop
  13. add a build step and select Invoke Top-level Maven
  14. target: clean install
  15. POM: AccountStatusWebservice\pom.xml
  16. navigate to the Actions after build
  17. select the SonarQube analysis with Maven
    1. branch: develop
    2. JDK: j.d.k. 1.8
    3. Maven Version: Apache-Maven-3.3.9
  18. click on Save and we are done
  19. build the project and when its succesfull you will see this(IMG)
  20. click on the SonarQube icon to start SonarQube
  21. when we navigate to the Coverage section and we don't see any results. In the next paragraph

adding testcoverage with the JaCoCo maven plugin

  1. we need to add dependency in the pom.xml
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <version>2.10.3</version>
    </dependency>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.6.201602180812</version>
    </dependency>
  1. next we need to add a profile to build the specific sonar reports
    <profile>
    <id>sonar</id>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.6.201602180812</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    </profile>
  1. we need to configure the job so the sonar profile is executed
  2. navigate to http://localhost:8075/jenkins/job/ws_account_status_sonar/configure
  3. go the the Invoke top-level Maven targets section
  4. adjust the Goal clean install > clean install -Psonar
  5. build the project
  6. navigate to sonar by clicking on the icon
  7. when you click on the Coverage tab, you won't see anything. This is because there is not any test present, so make a unittest and you wil see some coverage