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:
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.
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>
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
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
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.
September 17, 2009 at 11:14 am
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
September 17, 2009 at 11:48 am
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
September 17, 2009 at 12:50 pm
OK, thank you.