Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Install the Web plugin through the Update Center or download it into the SONAR_HOME/extensions/plugins directory
  2. Restart the Sonar server

Usage

You can analyze your project with the different analyzers

Sonar Runner

...

Run a Sonar Analysis with the Sonar Runner (Recommended Way)

To launch a Sonar analysis of your Web project, use the Sonar Runner.

A sample project is available on github that can be browsed or downloaded:

...

 /projects/languages/web/web-sonar-runner.

Maven

Create a maven pom for your project. Set the following properties:

  • sonar.language: web
  • sonar.web.sourceDirectory: [folder of the web files]
  • sonar.dynamicAnalysis: false

Sample pom file:

...

<?xml version="1.0" encoding="UTF-8"?>
<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>my.group.id</groupId>
  <artifactId>web</artifactId>
  <version>1.0</version>
  <name>MyProject :: Web</name>

  <properties>
    <sonar.language>web</sonar.language>
    <sonar.web.sourceDirectory>src/main/webapp</sonar.web.sourceDirectory>
    <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
   </properties>
</project>

Quick analysis
For an existing maven project, you might start an analysis by the following command:

Code Block
mvn sonar:sonar -Dsonar.language=web -Dsonar.dynamicAnalysis=false -Dsonar.web.sourceDirectory=src/main/webapp

You may set the property sonar.branch to make sure the web analysis will be reported in sonar under another project name, e.g. -Dsonar.branch=Web.

Ant

Create a build.xml for your project. In the sample below has two tasks for jsp(key:gNumber-JSP) and java(Key:Gnumber-Java) .

Sample build.xml (complete file)

...

<project name="myProject" basedir="../" default="deploy">

   <!-- Project settings -->
   <property name="project.distname" value="myProject"/>

   <!-- Define the Sonar task -->

   <taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
      <classpath path="D:\Program Files\sonar-2.10\lib" />
   </taskdef>

   Additional Sonar configuration (PMD need 1.5 when using annotations)-->
   <property name="sonar.java.source" value="1.5"/>
   <property name="sonar.java.target" value="1.5"/>
	 	 
   <!-- SERVER ON A REMOTE HOST - Here we use the default sonar setup -->
   <property name="sonar.host.url" value="http://localhost:9000" />
	
   <!-- Local system paths -->
   <property file="${basedir}/ant/build.properties"/>
   <property name="webroot.dir" value="${basedir}/WebContent"/>
   <property name="webinf.dir" value="${webroot.dir}/WEB-INF"/>
   <property name="build.dir" value="build"/>
   <property name="java.dir" value="${basedir}/src"/>
   <property name="bin.dir" value="${basedir}/bin"/>


    <!-- classpath for JSF 1.0 -->
    <path id="compile.classpath">
       <pathelement path ="${webinf.dir}/lib/javaee.jar"/>
       <pathelement path ="${webinf.dir}/lib/jsf-api.jar"/>
       <pathelement path ="${webinf.dir}/lib/jsf-impl.jar"/>
       <pathelement path ="${webinf.dir}/classes"/>
       <pathelement path ="${classpath.external}"/>
       <pathelement path ="${classpath}"/>
    </path>

    <!-- define your folder for deployment -->
    <property name="deploy.dir" value="deploy"/>

    <!-- Local system paths -->
    <property file="${basedir}/ant/build.properties"/>
    <property name="webroot.dir" value="${basedir}/WebContent"/>
    <property name="webinf.dir" value="${webroot.dir}/WEB-INF"/>
    <property name="build.dir" value="build"/>

    <!-- Check timestamp on files -->
    <target name="prepare">
        <tstamp/>
    </target>

    <!-- Copy any resource or configuration files -->
    <target name="resources">
        <copy todir="${webinf.dir}/classes" includeEmptyDirs="no">
            <fileset dir="src">
            <patternset>
                <include name="**/*.conf"/>
                <include name="**/*.properties"/>
                <include name="**/*.xml"/>
            </patternset>
            </fileset>
        </copy>
    </target>

    <!-- Normal build of application -->
    <target name="compile" depends="prepare,resources">
        <javac srcdir="src" destdir="${webinf.dir}/classes">
            <classpath refid="compile.classpath"/>
        </javac>
    </target>

    <!-- Remove classes directory for clean build -->
    <target name="clean"
      description="Prepare for clean build">
      <delete dir="${webinf.dir}/classes"/>
      <mkdir  dir="${webinf.dir}/classes"/>
    </target>

    <!-- Build entire project -->
    <target name="build" depends="prepare,compile"/>
    <target name="rebuild" depends="clean,prepare,compile"/>

    <!-- Create binary distribution -->
    <target name="war" depends="build">
      <mkdir dir="${build.dir}"/>
      <war
        basedir="${webroot.dir}"
        warfile="${build.dir}/${project.distname}.war"
        webxml="${webinf.dir}/web.xml">
        <exclude name="WEB-INF/${build.dir}/**"/>
        <exclude name="WEB-INF/src/**"/>
        <exclude name="WEB-INF/web.xml"/>
       </war>
    </target>

    <target name="deploy" depends="war">
       <delete file="${deploy.dir}/${project.distname}.war"/>
       <delete dir="${deploy.dir}/${project.distname}"/>
       <copy file="${build.dir}/${project.distname}.war" todir="${deploy.dir}"/>
    </target>
	
    <target name="sonar-jsp">
       <!-- The workDir directory is used by Sonar to store temporary files -->
          <sonar:sonar workDir="${temp.dir}" key="gNumber-JSP" version="1.0" xmlns:sonar="antlib:org.sonar.ant">
       
          <!-- source directories (required) -->
          <sources>
             <path location="${webroot.dir}" />
          </sources>
	 
          <!-- binaries directories, which contain for example the compiled Java bytecode (optional) -->
          <binaries>
             <path location="${webroot.dir}" />
          </binaries>
	 
          <!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
          <libraries>
	     <path refid="compile.classpath"/>
          </libraries>  	
        
          <!-- list of properties (optional) -->
          <property key="sonar.dynamicAnalysis" value="false" />
          <property key="sonar.language" value="web" />
          <property key="sonar.projectName" value="gNumber-JSP" />	
       </sonar:sonar>
    </target>
	
    <target name="sonar-java">
       <!-- The workDir directory is used by Sonar to store temporary files -->
       <sonar:sonar workDir="${temp.dir}" key="Gnumber-Java" version="1.0" xmlns:sonar="antlib:org.sonar.ant">
           <!-- source directories (required) -->
           <sources>
              <path location="${java.dir}" />
           </sources>
           <!-- binaries directories, which contain for example the compiled Java bytecode (optional) -->
           <binaries>
              <path location="${bin.dir}" />
           </binaries>		 
           <!-- path to libraries (optional). These libraries are for example used by the Java Findbugs plugin -->
           <libraries>
              <path refid="compile.classpath"/>
           </libraries>
           <!-- list of properties (optional) -->
           <property key="sonar.dynamicAnalysis" value="false" />
           <property key="sonar.language" value="java" />
           <property key="sonar.projectName" value="Gnumber-Java" />
       </sonar:sonar>
    </target>
</project>

Quick analysis
Run the following commands in the same folder from buld.xml:

Code Block
ant sonar-jsp --> for project jsp files
ant sonar-java --> for project java files

Run a Sonar Analysis with the other Analyzers

Maven and Ant can also be used to launch analysis on Web projects.

Advanced Parameters

The following properties of the plugin are configurable:

...