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 3 Next »

Analyse with Maven

Sonar uses a Maven plugin to analyze sources and to inject measures into the database. That's why database properties must be defined into the Maven configuration. Edit settings.xml file, located into $MAVEN_HOME/conf or ~/.m2, and add the parameters from step 3.

If data collection and server are on different boxes, set the server host with the property 'sonar.host.url'.

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 :

Analyzing Java Projects

The component that does analysis in Sonar is a maven plugin and is therefore perfectly integrated in a maven environment, where it will deploy its full capability.
However, in order to cover non-maven projects, a "Sonar light" mode was developed.

Maven projects

Analyzing a maven project consists of running a maven goal in the directory where the pom.xml sits. However, this requires first to have run an install goal.

(warning)  The version must be the same than the installed server. For example the 1.4 Sonar maven plugin does not work with 1.5 Sonar web server.

It is to be noted that there was a major change from Sonar 1.5: Sonar maven plugin used to do an install goal by default. From Sonar 1.5, the install goal is not run by default anymore. For more details, you can view Advanced parameters.

Recommended way

When possible, this is the recommended way of running an analysis on a project.

First, run an installation without running unit tests :


Then run the sonar goal : 

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

Other 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

Reuse existing unit test reports

If you already generates Surefire and Cobertura/Clover reports, for instance during your Maven Site generation, you might want Sonar to reuse those reports instead of consuming twice the CPU time to execute unit tests. To do that, you must use the property "sonar.dynamicAnalysis" :

Running analysis from a CI engine

For information on Sonar in Continuous Integration engines, see Continuous integration engines.

Non-Maven projects (sonar light mode)

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 maven2 plugin as explained in the installation guide :

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