Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Import your project's history in Sonar

When you do your first Sonar run on your project, you get a lot of new quality numbers to play with, but no trends. You only have one data set for comparison, the now picture.

Wouldn't it be nice if you could see the current trend of the project without waiting a couple of month for the 'daily/weekly' Sonar runs to fill up the data? Well, you're in luck! And if you're using git as a version system as well, this is your day. :)

In the Sonar Advanced Parameter documentation you will find a System Property called sonar.projectDate. The property let you tell Sonar when in time the running analysis was ran.

By combining this property and what your version system does best, track changes to source, we can now play back the history of the project as far as Sonar is concerned.

This little Bash script illustrates the concept. To spell out what it does in human readable form:

for each tag in the given git repository, checkout the source and run sonar, using the tag date as projectDate


MVN_COMMAND="mvn clean install"
SONAR_COMMAND="mvn org.codehaus.sonar:sonar-maven3-plugin:3.0:sonar"

if [ -z "$GIT_REPO" ]; then
    echo "Missing program argument: repository"
    echo "Usage: ./ git_repository_path [start-tag]"

pushd $GIT_REPO
for tag in `git tag`
    if [[ -n "$START_TAG" && "$START_TAG" > "$tag" ]] ; then
        echo "Skipping $tag (start tag $START_TAG)"

    TAG_DATE=`git show $tag --date=iso | grep Date: -m1 | cut -d' ' -f 4`
    echo "Checking out source from $TAG_DATE tagged as $tag"
    git checkout $tag  > /dev/null 2>&1
    git clean -df > /dev/null 2>&1


    echo "Executing Maven: $MVN_COMMAND"
    $MVN_COMMAND > /dev/null 2>&1
    echo "Executing Sonar: $SONAR_PROJECT_COMMAND"
    $SONAR_PROJECT_COMMAND > /dev/null 2>&1

You can of course modify this script to fit your own need. Maybe you want to checkout the history pr week or pr month instead of using tags. It's up to you.

Have fun! :)

Copy link

arouel commented Jul 15, 2013

Thanks for sharing this valuable example.

Copy link

christopinka commented Feb 26, 2015

I'm trying to do the same with CVS. Not a bash or git expert. How does pushd $GIT_REPO
for tag in git tag work? Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment