Sonar light: the low-calorie mode for Sonar

When I initially wrote this blog entry, I chose a much more original title : “What is the analogy between a Coke light and Sonar light”. But then I realized that “Coca Light” (the French for Diet Coke) does not translate to “Coke light”… The title was not as attractive anymore : I had to back up ! ;-)



As any good code analysis tool, Sonar performs static as well as dynamic analysis (code coverage by unit tests). The dynamic analysis requires a lot of processing power (calories burning) since the unit components of the program must be compiled before they can be executed. On the contrary, static analysis just needs to analyze source code in order to calculate metrics like cyclomatic complexity or to detect bad coding practices.



Further more, the dynamic analysis in Sonar requires that the project is “mavenized” in order to have a list of runtime dependencies. Since there are still a few projects around that are not mavenized yet, the demand was growing for a way of using Sonar on those projects.



Those two reasons were good enough to create Sonar light, a mode without dynamic analysis and that does not require your project runs under maven.
A live example of Sonar light is the Tomcat project in Nemo.



Practically, in order to run analysis on a project in the light mode, you only need the simplest possible pom.xml placed in the root of your project :



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>[GROUPID]</groupId>

  <artifactId>[ARTIFACTID]</artifactId>

  <name>[PROJECT NAME]</name>

  <version>[PROJECT VERSION]</version>

  <build>

        <sourceDirectory>[SOURCE DIRECTORY]</sourceDirectory>

        <plugins>

           <plugin>

              <groupId>org.apache.maven.plugins</groupId>

              <artifactId>maven-compiler-plugin</artifactId>

              <configuration>

                  <source>1.5</source>

                  <target>1.5</target>

              </configuration>

           </plugin>

        </plugins>

  </build>

  <properties>

    <sonar.light>true</sonar.light>

  </properties>

</project>



Launch Sonar Maven goal mvn org.codehaus.sonar:sonar-maven-goal:1.X:sonar and go to the Sonar web interface to see the results.



Coming back to the initial title : in both cases you are using a low-calorie product.

  • I needed to use this command to get this to work ( with version 1.5.1)

    mvn org.codehaus.sonar:sonar-maven-plugin:1.5.1:sonar

  • My project base in SVN,I want add my project in Sonar.
    I don’t think install Maven.Sonar don’t add my SVN of projects.
    I hope Sonar will add this module. thanks.

  • Thanks for the tips! Worked almost perfectly.

    The mvn command didn’t work for me, but a straight mvn sonar:sonar was fine.

    Exteningd the example to a multimodule example works as well (by replicating that pom specified per subproject/module and then having a parent pom which refers to the modules)

  • the command mvn sonar:sonar, works due to a new maven plugin.

    however, for me, when I try to run sonar with findbug, there is always a class missing exception.

    it’s a multi-module project, not maven project. the pom.xml is similar as suggested. I have specified it as light mode, and specify the dependency system path. But it still doesn’t work. Any one can enlighten me on what’s wrong?

    4.0.0
    com.hcl
    CFC_JMS_Listener
    CFC_JMS_Listener
    1.0

    dep1
    dep1
    1.0
    system
    ${basedir}\sonar_lib\spring.jar

    dep2
    dep2
    1.0
    system
    ${basedir}\sonar_lib\Shell_Utility.jar

    src
    build-to/classes

    org.apache.maven.plugins
    maven-compiler-plugin

    1.5
    1.5

    **/*.*

    false
    true

  • <project xmlns=”http://maven.apache.org/POM/4.0.0″
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
    xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd“>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.hcl</groupId>
    <artifactId>CFC_JMS_Listener</artifactId>
    <name>CFC_JMS_Listener</name>
    <version>1.0</version>
    <dependencies>
    <dependency>
    <groupId>dep1</groupId>
    <artifactId>dep1</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}\sonar_lib\spring.jar</systemPath>
    </dependency>
    <dependency>
    <groupId>dep2</groupId>
    <artifactId>dep2</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${basedir}\sonar_lib\Shell_Utility.jar</systemPath>
    </dependency>
    </dependencies>
    <build>
    <sourceDirectory>src</sourceDirectory>
    <outputDirectory>build-to/classes</outputDirectory>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.5</source>
    <target>1.5</target>
    <excludes>
    <exclude>**/*.*</exclude>
    </excludes>
    </configuration>
    </plugin>
    </plugins>
    </build>
    <properties>
    <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
    <sonar.light>true</sonar.light>
    </properties>
    </project>

  • Please switch to the Sonar user mailing list to get support.
    Thanks
    Freddy

  • Hi,

    Can you please provide a downloadable link of SonarLightpomTemplate.xml.

    I am not getting what should be the correct format of pom.xml in Sonar light mode.

    Thanks in advance.

    Regards,
    Shubham

  • how?

Leave a reply


6 × six =