Monday, April 30, 2012

How to run sonar for non-Maven based java project


 
To run sonar report, first we have to install maven & sonar in our local machine.For Non maven based project also we require maven because sonar require maven.
  1. Download and extract Maven in your C drive from http://www.apache.org/dyn/closer.cgi/maven/binaries/apache-maven-3.0.4-bin.zip
  2. Install Jdk 
  3. Download and extract Sonar in your D drive from http://dist.sonar.codehaus.org/sonar-3.0.zip
  4. Now go to D:\sonar-3.0\bin\windows-x86-32 and double click on StartSonar.batNow sonar server will start locally on http://localhost:9000
  5. Here maven require pom.xml for the project for which sonar report is to be generated, but non maven based project will not have any pom.xml.
    but we have to create a pom.xml in its project's parent folder as below.
  6.        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>[YOUR.ORGANIZATION]</groupId>
      <artifactId>[YOUR.PROJECT]</artifactId>
      <name>[YOUR PROJECT NAME]</name>
      <version>[YOUR PROJECT VERSION]</version>
      <build>
            <sourceDirectory>[YOUR SOURCE DIRECTORY]</sourceDirectory>
            <outputDirectory>[YOUR CLASSES/BIN DIRECTORY</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>
      </properties>
    </project>
  7. Replace the parameters :

    Description Example
    [YOUR.ORGANIZATION] the id of your organization (no space) com.myorganization
    [YOUR.PROJECT] the id of your project (no space) my.project
    [YOUR PROJECT NAME] the name displayed into sonar (spaces allowed) My Project
    [YOUR PROJECT VERSION] the version. Set 1.0 if no specific version. 1.0
    [YOUR SOURCE DIRECTORY] the relative path to the sources directory src/java
    [YOUR CLASSES/BIN DIRECTORY] the relative path to the compiled java classes directory bin
  8.   

    Go to parent folder of your project, and run following command
    mvn sonar:sonar
  9. After its completion, browse http://localhost:9000/ where you will see the project list with code quality report as below

3 comments:

  1. Following the above I created the pom.xml and it worked very well. Thanks. However I get these warnings:
    =====================
    [WARNING]
    [WARNING] Some problems were encountered while building the effective model for indience:unvired_kernel_android:jar:2
    [WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 13, column 20
    [WARNING]
    [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
    [WARNING]
    [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
    [WARNING]
    ---------------------------

    How can these be fixed?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Add the tag < version >2.5.1< / version > below the tag artifactId.

    ReplyDelete