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 “myserver.net”
- 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 user.email "yourusername@myserver.net"
git config --global user.name "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 http://git-scm.com/downloads 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 gituser@myserver.net:myrepository.git

# 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 http://downloads.sourceforge.net/project/sonar-pkg/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

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'sonar' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';
FLUSH PRIVILEGES;

Edit the file at “/opt/sonar/conf/sonar.properties” 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 - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian 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:

gituser@myserver.net:myrepository.git

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
ssh-keygen

# Print the contents of the generate public key and copy the output from your console and logout
cat id_rsa.pub
exit

# 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 (id_rsa.pub)>

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 :)

No Comments Posted in /dev
Spring WebFlow Wizard

Inspired by the great Ajax based webflow for Grails a former colleague made and the fact I am currently developing in the Spring MVC framework, I needed something similar for Spring Webflow. The documentation for Spring WebFlow is there, but the examples are very scarce and/or sometimes lacking good documentation. Because I now have an account on Github I decided to put a sort of Spring Webflow Wizard ‘framework’ in the repository there. It contains the minimal skeleton (breadcrumbs, navigation, validation et cetera) required to get people started building a wizard.

The project can be found at https://github.com/tjeerdnet/SpringWebFlowWizard.

It is an initial version, so feedback for improvement and/or bug reports are very welcome.

No Comments Posted in /dev
Tagged , , ,
World Statues Festival Arnhem 2011

Last month I visited the World Statues Festival in Arnhem. More than hundred people from all over the world presenting themselves as statues. The weather was great and it was very crowded. Ester and I took some pictures which you can find here.

Posted in /home, /media
Guruplug boot and kernel upgrade for stable Wi-Fi

Recently I got my Guruplug Server Plus device and I am happy with the toy, but soon I discovered that the Wi-Fi drivers are not stable if you want to run the device in “client mode”, I found out that recently there has been put up a page with instructions to make the device Wi-Fi stable. Although the instructions are there, it was difficult for me to find the right way to upgrade the U-boot and kernel. So here the way I did it. In a next posting I will explain how to setup Wi-Fi correctly, still figuring that out.

Prerequisites:
- Guruplug connected to your network via ethernet and a JTAG module to allow serial console access to the device
- Computer connected to your network (probably also the computer you want to use to access your Guruplug via the serial console)

Part 1: setting up the TFTP-server and putting the images in the right place

- Download pre-compiled U-boot binary here (direct link) into a directory (e.g. /root/guruplug)
- Download pre-compiled binary kernel here (direct link) into a directory (e.g. also /root/guruplug)
- Install a tftp-server (under Ubuntu Linux) and check whether it is running:

sudo apt-get install tftpd-hpa
service tftpd-hpa status

- Go to your work directory (/root/guruplug) and extract the u-boot and the kernel archive:

tar xvzf guruplug-u-boot-flipflip-20101111.tar.gz
tar xjvf UImage_linux_kernel_2_6_33_7.tar.bz2

- Copy of the extracted contents the following two files to the folder where tftpboot resides, in my case /var/lib/tftpboot:

mv /root/guruplug/u-boot.kwb /var/lib/tftpboot
mv /root/guruplug/uImage /var/lib/tftpboot

Part 2: The serial console / JTAG setup and ‘burning’ the images

- Connect via the serial console to the Guruplug, see this page. This
allows you to see what is going on on the device, including the boot process (U-boot).
- Turn on the Guruplug and see the device booting, when you see a message which allows you to interrupt the boot process by pressing a key, do so, this will show you the U-boot prompt
- Set some environment variables to prepare for ‘downloading’ the images from your TFTP-server (= serverip variable) to your Guruplug (= ipaddr variable). The IP addresses used here might be different for your network. The serverip is the computer running the TFTP-server. The ipaddr is the IP address you give to your Guruplug. It might be handy to save the environment variables so they are there everytime you boot.

setenv serverip=192.168.0.100
setenv ipaddr=192.168.0.110
saveenv

- Download the U-boot image from the TFTP-server into the memory of your Guruplug, erase the old U-boot image and write the new U-boot image into the memory of the plug. Finally reset the device. If everything went fine, you will see your updated U-boot prompt.

tftp 0x6400000 u-boot.kwb
nand erase 0x00000000 0x0100000
nand write 0x6400000 0x0000000 0x80000
reset

- Now install the new kernel image from the TFTP-server into the memory of the Guruplug and set the environment variables and save them.

tftp 0x6400000 uImage
nand erase 0x100000 0x400000
nand write.e 0x6400000 0x100000 0x400000

setenv x_bootcmd_kernel 'nand read.e 0x6400000 0x100000 0x400000'
setenv x_bootargs_root 'ubi.mtd=2 root=ubi0:rootfs rootfstype=ubifs'
setenv x_bootargs 'console=ttyS0,115200'
setenv x_bootcmd '$(x_bootcmd_kernel); setenv bootargs $(x_bootargs) $(x_bootargs_root) ;bootm 0x6400000;'
setenv bootcmd 'run x_bootcmd'
saveenv

Now you should be able to boot up your Guruplug with a new U-boot and kernel installed by entering the following command:

reset

Useful links:
http://www.denx.de/wiki/bin/view/DULG/Manual
http://www.plugcomputer.org/plugwiki/index.php/U-Boot_Quick_Reference
http://www.plugcomputer.org/plugwiki/index.php/Re-building_the_kernel_and_U-Boot

Posted in /dev