Sonar analysis using multiple git branches in Jenkins

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

This should work

mvn sonar:sonar -U -Dsonar.branch.name=$BRANCH_NAME

You can try adding -Dsonar.branch.name=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.

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

In a Jenkins multi-branch pipeline, the variable is BRANCH_NAME.

P.S. 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.