Full documentation for SonarQube has moved to a new location: http://docs.sonarqube.org/display/SONAR

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

Configuration

Sonar can use a Maven plugin to perform project analysis : this is the easiest and most comprehensive way. To do so, you should edit settings.xml file, located into $MAVEN_HOME/conf or ~/.m2, and the database parameters to be used as well as the Sonar server host.

Example :

Icon

Warning : With Sonar 2.2, the property sonar.host.url MUST NOT end with a slash character. Otherwise, JDBC driver is not found by the Sonar Maven plugin. See SONAR-1685.

It is recommended to increase the Java heap memory. It avoids getting OutOfMemoryError failures. Set the MAVEN_OPTS environment variable :

Analyze Maven projects

Analyzing a maven project consists of running a maven goal in the directory where the pom.xml sits. If possible, an install goal should be performed prior to the sonar one.

Recommended way


Using Eclipse

Icon

Make sure you're not using the eclipse plugin maven embedder. Define a new maven runtime pointing to your local maven install, use the latest maven eclipse plugin and uncheck "resolve workspace artifacts" in the maven project launch window.
Have a look at the first comment of this ticket : http://jira.codehaus.org/browse/SONAR-929

Alternative way

When the above configuration is not possible, you can run an alysis in one command, but unit tests will run twice : once in the install goal and once in Sonar. Do not use the -Dtest=false parameter, else Sonar will not execute unit tests and therefore not report on them.

The -Dmaven.test.failure.ignore=true is there to make sure that event if unit tests fail, the analysis will happen

For advanced features such as reusing existing unit test reports, you can use advanced parameters

Analyze projects which are not built with Maven

Sonar can analyze any kinds of Java projects, even if they're not built with Maven2. In such a case, measures that need a code execution to be calculated (unit tests, code coverage) are not available unless the XML reports are available and Sonar is configured to collect them.

  • Install Maven2 (just unzip, define the $MAVEN_HOME property and add $MAVEN_HOME/bin to $PATH)
  • Create a file named 'pom.xml' into the root directory of your project :
  • 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

  • Execute the Maven plugin as explained in the previous section :

Findbugs and Sonar light mode

Icon

Findbugs rules can be activated in Sonar light mode but as Findbugs needs compiled classes to find violations, you need to specify where those classes are stored. By default Findbugs tries to find classes in the "target/classes" directory (default Maven directory). If your Ant build generates classes for instance in the "build" directory you need to update your pom.xml file by adding "<outputDirectory>[YOUR CLASSES/BIN DIRECTORY]</outputDirectory>" below "<sourceDirectory>" node.
Additionally you need to define the aux classpath for findbugs in the "<dependencies>" node for instance as system dependency via :

If you forget a jar, you'll certainly get a warning message like this one :

Reuse existing unit test reports

If you generates JUnit and Cobertura/Clover/Emma reports with your build mechanism you can configure Sonar in order to collect them. To do that, you must add he following lines in your pom.xml file :

The property sonar.cobertura.reportPath can be replaced by sonar.clover.reportPath if you use Clover instead of Cobertura.

Project with multiple sources directories

If your non-maven project contains more than one sources directory, you can specify which sources directories to analyse by adding a new section about the Build Helper Maven Plugin into your pom.xml file :

  • 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 first sources directory

    src/module1/java

    [YOUR CLASSES/BIN DIRECTORY]

    the relative path to the compiled java classes directory

    bin

    [YOUR SOURCE DIRECTORY 2]

    the relative path to the second sources directory 

    ${basedir}/src/main/java2

    [YOUR SOURCE DIRECTORY 3]

    the relative path to the third sources directory 

    ${basedir}/src/main/java3

  • And execute the maven2 plugin as explained in the installation guide :
  • No labels