A new Hudson plugin for a closer integration with Sonar

By Simon Brandhof on September 30, 2008 » tags ,

Continuous integration (CI) has become a centerpiece of software development lifecycle. Since Sonar is implemented as a maven plugin, it can be easily integrated with any CI engine : the engine acts as the scheduler for Sonar’s daily runs. You can even imagine to use the well known but a bit outdated cron scheduler to launch the maven goal “mvn org.codehaus.sonar:sonar-maven-plugin:1.4.2:sonar” on each project you want to analyse.



At this point in time, you are probably wondering : ok, but why would I need a plugin to integrate Sonar with Hudson, the very popular, open source and easy to use CI engine ? Is it possible to get an even more seamless integration ?




In fact, when you face a real enterprise context, you need to configure your setting.xml file by hand on your integration machine and copy/paste the Sonar maven goal for each project under control : 10, 50, 100, 200 times … Life will be easier if you can :

  • Prevent edition of the setting.xml file and directly edit and view the Sonar configuration from Hudson
  • Define the Sonar maven goal only once and use it as often as often as you want




We’ve developed a Hudson plugin to provide those two missing features. All configuration is done online and is shared across projects, you can enable/disable Sonar analysis in one click for all your projects. Obviously, you still can override Sonar parameters at the project level. Imagine for instance, on a given project you don’t want to import and display source code through Sonar interface for security reason (SONAR-306).



That was the first step on planet Hudson and we wanted to go beyond. There is an unknown but yet useful Sonar functionality which allows you to analyze any java projects even if they don’t use Maven (perhaps you’re still using Ant or don’t use a build framework at all). Nevertheless, in that case you need to write by hands a short pom.xml file to provide to Sonar several mandatory parameters. This last tedious step has gone with version 0.2 of the plugin.You just need to fill those basic parameters in the Hudson job configuration and quality analysis can start. If you’re a code audit consultant and you need to quickly and efficiency analyze your customer’s sources : just tell Sonar/Hudson couple where is the source director.
hudson-job.png
Want to give it a try : just install the sonar plugin from the Hudson management console. And… don’t forget to install Hudson and Sonar before :-)


  1. Hi, on http://hudson.gotdns.com/wiki/display/HUDSON/Sonar+plugin it says: “We recommend to execute Sonar at the most on nightly builds. It’s useless to activate it on continuous builds.” If I understand this correctly, it means I shouldn’t execute Sonar’s maven plugin on every commit a developer makes in Subversion, but I should do it every 24 hours at most?

    Please, can you specify the reason why this is.

    Thanks a million
    Bye

  2. Hello,

    You can run as many analysis as you wish. However, although Sonar will keep all the analysis done during the day, the reporting interface will only show one analysis per day. Therefore, doing more that one analysis will not be of any use within Sonar. Further more, I don’t think it really makes sens to look at the code quality after every commit, especially if you are committing several times a day.

    Going a little bit further, when you have installed and configured Sonar, what you are generally interested in (there might be exceptions) is looking at evolutions and trends. Therefore, to monitor this, a weekly analysis is generally the most appropriate on a fairly active project.

    Hope this clarifies.

    Olivier

  3. A really useful feature IMHO would be to perform an analysis after each commit and use the comparison against the previous commit to increment/decrement a tally of stats for the committer.

    This would enable a team to quickly identify those developers that need mentoring and in which areas the mentoring should focus.

  4. Good idea William !
    What would be great is to :
    - retrieve which files and which lines in those files have been added or modified
    - launch static analysis (checkstyle, pmd and findbugs) and dynamic analysis (see last clover feature : Test optimization) only on those files to drastically reduce the quality analysis time
    - push all the collected data into Sonar and report what you want

    I don’t know if Hudson API provides a way to easily get which lines have been changed in a file ?

  5. So close, but sonar can’t seem to find artifacts from the sonar repo

    [INFO] Failed to resolve artifact.

    Missing:
    ———-
    1) org.codehaus.sonar.runtime.plugins:parent:pom:20090109135100
    Path to dependency:
    1) org.codehaus.sonar:sonar-core-maven-plugin:maven-plugin:1.5.1
    2) org.codehaus.sonar.runtime.plugins:parent:pom:20090109135100

    2) org.codehaus.sonar.runtime.jdbc-driver:parent:pom:20090109135100
    Path to dependency:
    1) org.codehaus.sonar:sonar-core-maven-plugin:maven-plugin:1.5.1
    2) org.codehaus.sonar.runtime.jdbc-driver:parent:pom:20090109135100

    ———-
    2 required artifacts are missing.

    for artifact:
    org.codehaus.sonar:sonar-core-maven-plugin:maven-plugin:1.5.1

    from the specified remote repositories:
    central (http://repository.codehaus.org),
    sonar (http://localhost:9000/deploy/maven)

  6. Hi Morgan, can you switch to the Sonar mailing list to discuss your issue ? Seems like you use a maven mirror configuration or something like that.

  7. Is sonar usable for C++ projects. All the users listed at http://nemo.sonar.codehaus.org seem to be java projects. The tools bundled by sonar also seem to be for java.

  8. Sonar isn’t yet bundled with C++ code analyzers.

  9. You say :“We recommend to execute Sonar at the most on nightly builds. It’s useless to activate it on continuous builds.”

    but how to do that with your plugin? In Hudson, what can I configure is the scheduling to track scm changes or a personal scheduling but this is always the same build goal that is executed, but I don’t want to execute sonar when scm change!
    An idea?

  10. You need to setup 2 jobs in Hudson. One for your continuous integration that is gonna poll the scm. The other one with a personal scheduling for Sonar execution.

  11. Hudson plugin 1.0 is out with many improvements : master/slave mode, i18n (currently English/French), easier configuration and some advanced parameters. It needs Hudson 1.306+ and Sonar 1.8+.

  12. [...] objective was reach last September with the release of Hudson Sonar plugin version 0.2. Since then, we have used the plugin and have been able to increase without big effort the number [...]

  13. what should I do if I have more than one source directory?

  14. If you use latest version 1.1. of the Hudson plugin, this feature is available :

    http://jira.codehaus.org/browse/SONARPLUGINS-15

  15. You write: “There is an unknown but yet useful Sonar functionality which allows you to analyze any java projects even if they don’t use Maven (perhaps you’re still using Ant or don’t use a build framework at all). Nevertheless, in that case you need to write by hands a short pom.xml file to provide to Sonar several mandatory parameters.This last tedious step has gone with version 0.2 of the plugin.” Does that mean that I can start sonar without any maven installation?

  16. It means that when you are using Hudson, you can start Sonar analysis without touching Maven at all. Hudson is going to handle it for you.

  17. While this is great (thanks!) it doesn’t allow to specify that the coverage reports should be coming from emma. I work around this by setting system properties (sonar.core.codeCoveragePlugin and sonar.emma.reportPath) in the main hudson config, but it ain’t so nice.

  18. This is correct that it does not allow you to specify that the coverage reports should be coming from emma. Two comments though :
    1. Emma coverage is not part of Sonar core which is why the sonar.emma.reportPath was not included originally in the Hudson plugin. Could you create a Jira issue to include it ?
    2. Instead of using the sonar.core.codeCoveragePlugin, wouldn’t it make more sense to set it up in the Sonar configuration section, either globally or per project ?

  19. Has anyone successfully used the Hudson plugin with Sonar and the Sonar MetriC++ plugin?
    I’m able to trigger the Sonar analysis from Hudson, but the Sonar results never show any violations, duplicate code, etc.

  20. Hi Craig, feel free to send an email to the Sonar mailing list (http://sonar.codehaus.org/support/) and attach to this email your Maven log.
    Thanks

  21. I’m using the 1.1 version of the plugin with Hudson 1.340, I’m not sure how it’s supposed to be set up.

    For “Sonar Installation”, the only choice I have is “default”, should there be something else to choose from?

    For “Sonar maven runtime”, again the only choice is default. Since we using ANT and the “NOT built with maven” box is checked, I’m not sure what this thing is doing here at all.

    Source directory. This is relative to what? Is it the workspace?

    BTW, clicking on the question mark brings up a 404 error.

    Thanks in advance.

  22. Andy,

    Might be easier to switch to the mailing list to discuss all this.

    For “Sonar Installation”, you could have several instances of Sonar and in that case and in that case more entries would appear. Same thing for Maven. Sonar analyzer being a maven plugin you need a maven installation in all cases.

    Source directory is the directory where the root of the sources is located.

    Which of the question mark is showing a 404 ?

    Olivier

Leave a reply