Setting up Jenkins/SonarQube

In this post we will setup SonarQube and Jenkins to perform code quality check and continuous integration.



Install and configure SonarQube

There are many code quality checking tools like PMD, Firebug but SonarQube brings them all under one roof and gives better view of code quality.

Let us install and configure SonarQube for our JCart application.

Download SonarQube from
Extract it run sonarqube-5.2/bin/windows-x86-64/StartSonar.bat.

By default SonarQube uses in-memory H2 database to store all the metrics.
If we want to use MySQL we can configure MySQL jdbc parameters in sonarqube-5.2/conf/


Configure SonarQube Maven Plugin

Let us configure sonarqube maven plugin in jcart/pom.xml

<project xmlns="" 

Now you can run maven goal mvn sonar:sonar which performs all the code quality checks and insert metrics into database.
Now go to http://localhost:9000/ and click on jcart project. From there you can explore lot of metrics like coding issues, duplicate code etc. You can see code quality issue details and fix the problem.

There are lot of things we can configure like tweaking the Quality Profiles to meet our project needs.
But we are not covering all that in this post now. May be a future post!

Install and configure Jenkins

We will use Jenkins as continuous integration server for JCart.
Download Jenkins war from and run Jenkins as follows:

java -jar jenkins.war

Now you can go to http://localhost:8080/ and click on Manage Plugins.
Click on Available tab and search for Git in filter box then select “Git plugin” and “Github plugin“.
Click on Install without restart.

Once the installation is completed, go back to Dashboard and click on Manage Jenkins and go to Configure System.
In Configure System screen, configure JDK, Maven and Git path variables and click on Save.

Now let us configure a maven project build to build our JCart application.

First click on New Item menu, give jcart as Item name and select Maven project radio button.

Select Git for Source Code Management and give for Repository URL

In Build Triggers section, select Poll SCM with schedule H/15 * * * * (every fifteen minutes)

In Build section, enter pom.xml for Root POM and “clean install” for Goals and options.

We can configure Jenkins to perform Sonar code quality checks if the build is successful.
In Post Steps section, click on Add post-build step -> Invoke top-level Maven targets.
Select Run only if build succeeds and sonar:sonar as Goals.

Now click on Save to save our Jcart project build configuration. To test drive our build configuration click on Build Now and see the console output.

If we are hosting our code on Github we can configure Jenkins to trigger build whenever code change is pushed to repository instead of polling SCM periodically.

In jcart project build configuration screen, select Github project checkbox and enter as Project url.
In Build Triggers section, select Build when a change is pushed to GitHub checkbox.

In order to trigger Jenkins build when code is pushed to Github repository we need to configure Webhook.
Go to GitHub repository Settings tab, click on Webhooks & services -> Add service -> Choose “Jenkins (GitHub plugin)“.
Then fill in the Jenkins hook url with your jenkins url like this: http://your_jenkins_url/github-webhook/

But giving jenkins url like http://localhost:8080/github-webhook/ won’t work (obviously!!) as it tries to resolve “localhost” from Github server.

See for more details.

To simulate the behaviour of automatically triggering the build when code is pushed to Github, access the URL http://localhost:8080/git/notifyCommit?url= from your browser. It should trigger the build process in Jenkins.

You can run Jenkins on OpenShift to fully automate the build process so that you can give OpneShift Jenkins URL in Github webhooks which GitHub can resolve.

Now that we have completed Iteration-1. Let’s get ready for Iteration-2 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *