Setting up Maven, Git, Jenkins and Sonar on Ubuntu

This tutorial explains how to install Maven (build automation tool), Git (version control), Jenkins (continuous integration) and Sonar (code quality tool). I’ll keep the intro simple, because there is enough to do.

Requirements and assumptions

– Ubuntu (Server edition)
– Server in this example is called “”
– You have a Maven-project on your local system
– Everywhere ‘localhost’ is used, replace it with your server adddress (either by IP address or FQDN)

Step 1: Installing Git on the server

Run the following commands to install Git for version control on the server. This will allow you to push changes you made in your local Git-repository to the server.

#Install the Git binaries
sudo-apt get install git-core

#Create a Git-user
sudo adduser gituser --home /home/gituser

#Switch to the Git-user
su gituser

#Go home
cd ~

# Initialize your Git-identity
git config --global ""
git config --global "yourusername"

# Initialize a bare Git repository in the home directory
git init --bare myrepository.git

Step 2: Instelling Git on the client

Download a Git-client for your operating system via so you can create your local Git-repository and commit changes. Once you’ve installed your Git-client, make sure you’ve put your Maven-project under version control by initializing it (via the GUI or the CLI):

# Go to the directory holding your Maven-project
cd /myprojects/testproject

# Initialize your project (run this inside the root of your project)
git init

# Add all files under Git version control
git add .

# ‘Connect’ your local repository to your remote (origin) repository
git remote add origin

# Push our local project/changes to our remote (origin) repository
git push origin master

Step 3: Installing Sonar and MySQL on the server

Sonar will provide us with code coverage tools, for Ubuntu you need to add Sonar to your package source listing.
Edit the “/etc/apt/sources.list” file and add the following line to that to add the repository and to install Sonar:

deb binary/
sudo apt-get update
sudo apt-get install sonar

Install MySQL, login as root and create the necessary tables and permissions (change the password of the MySQL sonar user to something else):

sudo apt-get install mysql-server
mysql -u root -p

GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';

Edit the file at “/opt/sonar/conf/” and set the following (disable default driver):

# sonar.jdbc.url: jdbc:h2:tcp://localhost:9092/sonar
# sonar.jdbc.driverClassName: org.h2.Driver

Uncomment the Mysql settings section as follows:

#----- MySQL 5.x
# Comment the embedded database and uncomment the following line to use MySQL
sonar.jdbc.url: jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
# Optional properties
sonar.jdbc.driverClassName: com.mysql.jdbc.Driver

And make sure the user name and password to connect to the data base are set:

sonar.jdbc.username: sonar
sonar.jdbc.password: sonar

Start the Sonar server and visit the url (http://localhost:9000) to confirm if it works:

sudo service sonar start

Step 4: Installing Maven and Jenkins on the server

Jenkins is the tool to enable us to setup continuous integration and Maven is our build tool. To install Maven execute the following command on the server:

sudo apt-get install maven

To install Jenkins you first need to add it to the packages source list, so we can easily install it via apt-get.

wget -q -O - | sudo apt-key add -
sudo sh -c 'echo deb binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins

Check if Jenkins is running at http://localhost:8080/.
The next step is to install the plugins, this can be done via the Dashboard -> Manage Jenkins -> Manage Plugins and install the Git-client plugin, Git-plugin, SCM API Plugin and Sonar plugin.
Finally you have to configure your Jenkins via Dashboard -> Configure System and look for the Sonar header and you will see a list of fields to configure. The most important are database url, database username and password. Be sure the correct server url’s (where your instance of Sonar is running on) are configured here.

In the same configuration screen look for the Maven header and add Maven by giving it a name and the location of your installation (e.g. /usr/share/maven).

Step 5: Creating our first Jenkins continuous integration project

Jenkins should know which project it is going to serve for, so we start by creating a new project via Dashboard -> New item. Give your project a name and choose for Maven2/3 project. In the next screen select the Source code management header and choose for Git and add the following Git-url:

Here is a tricky part if you want to automate this, because when Jenkins is calling Git it wants to login over SSH and needs credentials for that. We generate a public key for our Jenkins account and put this key in our gituser account:

#Login as user jenkins
sudo su jenkins

#Go home to our SSH folder
cd ~/.ssh

# Generate a public key

# Print the contents of the generate public key and copy the output from your console and logout

# Login with account gituser and paste the public key in the authorized_keys file
sudo su gituser
cd ~/.ssh
nano authorized_keys
<paste the key you copied from the generated public key file (>

Now Jenkins has access to our repository. The next step is to install a Build Trigger so Jenkins automatically starts building as soon as new code is submitted. Open the Build Trigger header and enter SCM sampling textbox five stars (* * * * *) separated by a space. This way Jenkins will poll every minute at our Git-repository to see if new code is in there and if so, starts building.

Please leave a comment if parts are unclear, missing in this short tutorial or if it helped you 🙂