A new Hudson plugin for a closer integration with Sonar

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

  • 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

  • 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

  • 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.

  • 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 ?

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

  • 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.

  • 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.

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

  • 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?

  • 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.

  • 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+.

  • [...] 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 [...]

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

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

    http://jira.codehaus.org/browse/SONARPLUGINS-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?

  • 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.

  • 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.

  • 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 ?

  • 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.

  • 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

  • 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.

  • 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

  • I am using Hudson to do a p2 build of an eclipse RCP (basically a horde of Eclipse plugins). The project is not built with maven2.
    These eclipse plugins are compiled using a specific java builder provided by eclipse (it’s a wrapper around the java compiler).
    The result of the build is a set of jars found in specific folders (like plugins/ or features/).
    No statistics are generated what-so-ever.

    Where exactly should the sources and target be pointing to?

    This is how the sources structure looks like:
    {sources}/com.name.p1/src/com/name/p1/… all the java files
    {sources}/com.name.p2/src/com/name/p2/… all the java files

    This is how the destination structure looks like:
    plugins/com.name.p1_1.0.0.201002121150.jar
    plugins/com.name.p2_1.0.0.201002121150.jar

    Additional questions:
    Are the .class files mandatory to generate any report?

  • Fabian, please switch to the mailing list to ask such question.

  • I want to integrate Fortify analysis results into Sonar but seems like there are no plugins available.
    I was looking at the hudson and found that they have Fortify plugin. Is their a way to leverage Hudson to port my Fortify results into Sonar.

  • Why was isometriC++ Sonar plugin deprecated? Is there any viable Sonar plugins for C++ projects?

    Thanks
    Yogish

  • Hi Yogish, the C++ plugin was supported by Isotrol but there is no more activity on this plugin.

  • We are integrating the Emma to Sonar. Provided the details of Emma in POM.xml file.( reuseReports
    emma coverage ). But while running Sonar with maven(mvn sonar:sonar), we are getting below exception:

    [ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0-beta-2:s
    onar (default-cli) on project BbAssist: Can not execute Sonar: null input: mdata
    -> [Help 1]

    Anyone please help us on this issue.

    Thanks in Advance,
    Satish Kumar

  • Hi Satish, please switch to the Sonar mailing list for such question. Thanks.

  • Hi all,

    My war file uses an ANT build script. I have added sonar-ant_task1.0.jar and followed the instructions here

    http://docs.codehaus.org/display/SONAR/Analyse+with+Ant+Task+1.0

    Then In my configure in Hudson(now Jenkins) I have checked the checkbox that says “Check if this project is NOT built with maven 2″ and it still seems to try to be using Maven:

    [workspace] $ mvn -f /root/.jenkins/jobs/HRDA/workspace/pom.xml -e -B sonar:sonar -Dsonar.jdbc.driver=com.mysql.jdbc.Driver -Dsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8 ******** ******** -Dsonar.host.url=http://localhost:9000/
    FATAL: command execution failed
    java.io.IOException: Cannot run program “mvn” (in directory “/root/.jenkins/jobs/HRDA/workspace”): java.io.IOException: error=2, No such file or directory

  • Please switch to the Sonar mailing list for such question

  • Hi,

    I don’t know where should I write my query(issue) since I got completely stressed.
    The documentation on Sonar is very poor, It can’nt help who are just starting exploration on sonar, I’m trying to run sonar from last 3 days by using Javarunner as well as maven.
    It is not like I don’t know anything about java, I could write whole web project in notepad but this is really going to happen nightmare for me. my project is in .Net to which I want to analyze using sonar but after reading almost complete documentation on sonar I am not able to figure out this error:

    [ERROR] The project com.mmm.blog:SampleApplication:1.0 (C:\Documents and Setti
    ngs\A039009\My Documents\Visual Studio 2010\Projects\SampleApplication\pom.xml)
    has 1 error
    [ERROR] Unresolveable build extension: Plugin org.codehaus.sonar-plugins.dot
    net:maven-dotnet-plugin:0.6 or one of its dependencies could not be resolved: Fa
    iled to collect dependencies for org.codehaus.sonar-plugins.dotnet:maven-dotnet-
    plugin:jar:0.6 (): Failed to read artifact descriptor for org.codehaus.sonar-plu
    gins.dotnet:maven-dotnet-plugin:jar:0.6: Could not transfer artifact org.codehau
    s.sonar-plugins.dotnet:maven-dotnet-plugin:pom:0.6 from/to central (http://repo1
    .maven.org/maven2): Error transferring file: Connection timed out: connect -> [Help 2]

    On top of that on one replying my query also.
    hope some one will be having some workaround this.

    Many Thanks
    Manmohan

  • As said just above your message: “please switch to the Sonar mailing list for such question”.

  • Hi all,
    I installed every thing but i’m having this error in the build of my maven 3 project :

    I used sonar 2.9

    [ERROR] Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0-beta-2:sonar (default-cli) on project e-library-project: Can not execute Sonar: PicoLifecycleException: method ‘public void org.sonar.jpa.session.AbstractDatabaseConnector.start()’, instance ‘org.sonar.jpa.session.DriverDatabaseConnector@8bf290b, java.lang.RuntimeException: wrapper: Cannot open connection to database: SQL driver not found org.postgresql.Driver -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0-beta-2:sonar (default-cli) on project e-library-project: Can not execute Sonar at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:217) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:319) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352) Caused by: org.apache.maven.plugin.MojoExecutionException: Can not execute Sonar at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:118) at org.codehaus.mojo.sonar.Bootstraper.start(Bootstraper.java:65) at org.codehaus.mojo.sonar.SonarMojo.execute(SonarMojo.java:90) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209) … 19 more Caused by: org.picocontainer.PicoLifecycleException: PicoLifecycleException: method ‘public void org.sonar.jpa.session.AbstractDatabaseConnector.start()’, instance ‘org.sonar.jpa.session.DriverDatabaseConnector@8bf290b, java.lang.RuntimeException: wrapper at org.picocontainer.monitors.NullComponentMonitor.lifecycleInvocationFailed(NullComponentMonitor.java:77) at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.monitorAndThrowReflectionLifecycleException(ReflectionLifecycleStrategy.java:132) at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.invokeMethod(ReflectionLifecycleStrategy.java:115) at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.start(ReflectionLifecycleStrategy.java:89) at org.picocontainer.adapters.InstanceAdapter.start(InstanceAdapter.java:108) at org.picocontainer.behaviors.AbstractBehavior.start(AbstractBehavior.java:169) at org.picocontainer.behaviors.Stored$RealComponentLifecycle.start(Stored.java:132) at org.picocontainer.behaviors.Stored.start(Stored.java:110) at org.picocontainer.DefaultPicoContainer.potentiallyStartAdapter(DefaultPicoContainer.java:996) at org.picocontainer.DefaultPicoContainer.startAdapters(DefaultPicoContainer.java:989) at org.picocontainer.DefaultPicoContainer.start(DefaultPicoContainer.java:746) at org.sonar.batch.bootstrap.Module.start(Module.java:88) at org.sonar.batch.Batch.execute(Batch.java:74) at org.sonar.maven3.SonarMojo.executeBatch(SonarMojo.java:147) at org.sonar.maven3.SonarMojo.execute(SonarMojo.java:136) at org.codehaus.mojo.sonar.Bootstraper.executeMojo(Bootstraper.java:113) … 23 more Caused by: java.lang.RuntimeException: wrapper at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.monitorAndThrowReflectionLifecycleException(ReflectionLifecycleStrategy.java:130) … 37 more Caused by: org.sonar.jpa.session.DatabaseException: Cannot open connection to database: SQL driver not found org.postgresql.Driver at org.sonar.jpa.session.AbstractDatabaseConnector.testConnection(AbstractDatabaseConnector.java:189) at org.sonar.jpa.session.AbstractDatabaseConnector.start(AbstractDatabaseConnector.java:101) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.picocontainer.lifecycle.ReflectionLifecycleStrategy.invokeMethod(ReflectionLifecycleStrategy.java:110) … 36 more Caused by: java.sql.SQLException: SQL driver not found org.postgresql.Driver at org.sonar.jpa.session.DriverDatabaseConnector.getConnection(DriverDatabaseConnector.java:91) at org.sonar.jpa.session.AbstractDatabaseConnector.testConnection(AbstractDatabaseConnector.java:185) … 42 more Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at org.sonar.jpa.session.DriverDatabaseConnector.getConnection(DriverDatabaseConnector.java:88) … 43 more [ERROR] [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException [DEBUG] Skipping watched dependency update for build: e-library_Job #23 due to result: FAILURE Finished: FAILURE
    Any one have idea ??
    Thank you in advance

  • Hi comunity,
    I resolved the problem thank you for this plugin
    Amira

  • Hi Amira how to resolved, could you share us, i m having same exception

  • Hi
    I’m trying to integrate sonar with hudson.
    my query is, if i build my java project using ant task where in I have defined the sonar task then do I need a plugin in hudson to configure my project?

  • Please join the Sonar User mailing to get some community support. Thanks.

  • I wonder how many times Fabrice has to say “please switch to the Sonar mailing list for such question”, before these idiots get the message…how did these fools ever get a job in IT.

Leave a reply


6 × = forty eight