I have setup Jenkins to automatically build several git branches of my project. The build also triggers Sonar analysis, as a post-build action.

The problem is that all branches point to the same Sonar project. I know that there is a sonar.branch property. Is it possible to have Jenkins automatically set sonar.branch property to the current git branch being built (without having to change project's pom.xml)?

up vote 18 down vote accepted

You can try adding -Dsonar.branch=something in the field MAVEN_OPTS in jenkins post-build action (Advanced configuration).

I don't know how to resolve something, since I don't know how you configure your jenkins job... but it will probably be something like $git.branch. Here is the doc

I didn't try, so i'm not sure it will work.

When having multiple branch, my approach is to always define one Jenkins job per branch (the copy job feature of jenkins may help)


While reading your question it's difficult to say if you are using maven or not to trigger sonar. If you are not using maven: there is a project properties field in the Jenkins config where you can define sonar.branch. Here is the doc

  • 1
    I ended up using the approach you suggested, using different jobs for each branch. I do use Jenkins to trigger sonar, so I set the sonar.branch property accordingly for each job. I think this has also the advantage that the build job is triggered only if a commit is made for this particular branch, and also you can create more fine-grained build schedule control/notifications for more important branches etc. – spyk Mar 25 '13 at 9:52
  • Not working for me :-( I added $GIT_BRANCH in MAVEN_OPTS. still when I am building from some other branch through some other job - the dashboard showing coverage is changing to the results from the latest branch run. I want it to remain same for my branch. – R11G Jul 5 '13 at 11:27

I am not sure if this helps you with the automated Jenkins execution, but to run a mvn sonar:sonar with the proper branch set, you can run the command (note the back-quotes), if you are in a Unix based environment:

mvn -Dsonar.branch=`git rev-parse --abbrev-ref HEAD` sonar:sonar

Did you use build parameters in order to specify the branch?

If yes, you can easily tell to Sonar which branch is it, juste add in config textarea:


Where branch is the name of the build parameter.

This should work

mvn sonar:sonar -U -Dsoanr.branch=<branch>

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.