NerdGerl

Sonar and Hudson

Posted by: nerdgerl on: September 17, 2009

I recently found out about Sonar after finding issues with running Cobertura on multi-module maven projects (see nabble post). Sonar looked like it solved this issue and added a whole stack more insight into the code quality of a project.
I got a Sonar demo up and running fine locally and convinced my team it would be a benefical tool for us to use so the next step for me was to install Sonar on our development box and integration it with our CI server, Hudson.
There seems to be two ways you can achieve Sonar and Hudson integration:

  1. Use the Sonar Hudson plugin and run Sonar as an ‘after build’ task
  2. Run mvn sonar:sonar in a within Hudson

I had a lot of issues trying to get the plugin to work so I ended up going with option 2. Below are the steps I took to get it to work.

  1. Firstly you need to download and install Sonar. You can do this buy executing the run script or by building and deploying the war file into an applicaiton server. I had issues with Glassfish so I just opted for running it from the run script and for now, I just stuck with the default Derby database.
  2. Starup Sonar and browse to http://hostname:9000 to make sure it’s up and running.
  3. Edit the maven settings.xml that is located on the same machine as your Hudson server and add in the following
  4. Add in a new repository under your exising one.

    <repository>
              <id>sonar</id>
              <name>Sonar Repository</name>
              <snapshots>
                <updatePolicy>daily</updatePolicy>
                <checksumPolicy>ignore</checksumPolicy>
              </snapshots>
              <url>http://hostname:9000/deploy/maven</url>
            </repository>
    

    Add in a new sonar profile

    <profile>
    <id>sonar</id>
    <activation>
    <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
    <sonar.jdbc.url>
                      jdbc:derby://localhost:1527/sonar;create=true
                    </sonar.jdbc.url>
                    <sonar.jdbc.driver>org.apache.derby.jdbc.ClientDriver</sonar.jdbc.driver>
                    <sonar.jdbc.username>sonar</sonar.jdbc.username>
                    <sonar.jdbc.password>sonar</sonar.jdbc.password>
    <sonar.host.url>http://hostname:9000</sonar.host.url>
    </properties>
    </profile>
    

    update your nexus mirror

        <mirror>
          <id>hsl-repository-mirror</id>
          <mirrorOf>*,!sonar</mirrorOf>
          <url>http://maven-repository:8181</url>
        </mirror>
    

  5. Now you can create a new job in Hudson and run it with the following maven command:
    clean install sonar:sonar

    That’s it! You should be able to browse to your sonar page and see your project there with coverage results. I had my job execute ever day at 11pm rather than on SVN commit.

    It’s worth pointing out that the above mvn command will run the tests twice, firstly during install and secondly during sonar:sonar. Take a look here for more info. In order to speed this up it might be worth adding in the following paramater at the end of the command:

    -Dsonar.dynamicAnalysis=reuseReports

    If you want to generate sonar results even if the build fails add this paramater

    -Dmaven.test.failure.ignore=true

    All together that would be:

    clean install sonar:sonar -Dsonar.dynamicAnalysis=reuseReports -Dmaven.test.failure.ignore=true

If you you are getting out of memory exceptions, consider editing the MAVEN_OPTS under Advanced in your job configuration to have the following value:

-Xmx256m -XX:MaxPermSize=256m

Tags: , , ,

7 Responses to "Sonar and Hudson"

Hello,

I have one question: what do you mean with “Edit your maven settings.xml”? Do you mean the settings.xml on the Hudson server or the settings.xml on the machine of a developer?

Thank you

Hi,
Thanks for the question. I mean the setting.xml file that is located on the same machine as the Hudson server. This will allow you to run a successful build from Hudson.

Ive updated the blog to explain this more clearly.

Cheers
Georgi

OK, thank you.

You stated that Sonar solved the issue about multi module projects in maven. What exactly do you mean?

Because I have a maven project with multi modules; one of them is just responsible for testing the other modules. The problem is that I won’t get the correct code coverage for the modules, which are tested by the seperate test module, when using Sonar and Cobertura. Did you solve this problem or do you mean that Sonar is aware of multi module code coverage; but Sonar can’t handle the case when a module is testing other modules?

Thanks in advance

Hey again,
I have the same project setup as you eg:
Project1 (contains src code and unit tests)
Project2 (contains src code and has the parent pom)
Project3 (no src code ONLY integration tests)

I have a profile wich is needed to run the integration tests: “-DintegrationTests=true”

So I run my build/tests like: clean install -DenableIntegrationTest=true sonar:sonar

In the cobertura results you should see your unit tests and integration test coverage.

Let me know if this doesn’t work for you as I had different coverage percentages with Clover than with Cobertura so perhaps im missing out on something.

Cheers

How do you run the hudson job with clean install sonar:sonar

i tried the sonar plugin for hudson but it was impossible for me to make it work with a large project. it always said the same out of memory error on cobertura plugin execution. definitely i used my own maven goal (sonar:sonar) in my task and configured settings.xml and mvn script to run with more memory usage. i had to increase the memory used by cobertura plugin in sonar configuration.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.