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

How to run sonar for Maven based java project


To run sonar, either we can directly run using maven script or we can run sonar from eclipse plugins.
To run sonar report, first we have to install maven & sonar in our local machine.


  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.  Go to parent folder of your project, and run following command
    mvn clean compile install sonar:sonar as below
  6. After its completion, browse http://localhost:9000/ where you will see the project list with code quality report as below