Tim
Tim

Reputation: 13258

Sonar: non-maven project -> exclude a directory in src

I have a non-maven project, but I have to use Sonar to make a code analysis once. So I created a pom.xml which works great. I see the analysis of all files and folders below my src directoy using this pom file:

<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>arifactId</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <name>MyApplication</name>
    <build>
        <sourceDirectory>src</sourceDirectory>
    </build>

    <properties>
        <sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
    </properties>
</project>

But I want to exclude one directory and all subdirectories of it. I have the two directories src/fr/ and src/com/. I only want to have src/fr/ and exclude src/com/. Changing

<sourceDirectory>src</sourceDirectory>

to

<sourceDirectory>src/fr</sourceDirectory>

I get this error:

Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0:sonar 
(default-cli) on project arifactId: Can not execute Sonar: Sonar is 
unable to analyze file : '/Users/tim/workspace/src/fr/xxx/dao/TestClass.java': 
The source directory does not correspond to the package declaration fr.xxx.dao

Upvotes: 1

Views: 1877

Answers (1)

Charlee Chitsuk
Charlee Chitsuk

Reputation: 9059

Normally, the The source directory does not correspond to the package declaration, as it told us, is an error when the package declared at the java file does not correspond with the directory structure e.g.

package my.test.java;
public class MyTest {}

The directory should be my/test/java/MyTest.java, 
please note the src is treated as a sourceDirectory.

In your case you have changed the sourceDirectory from src to src/fr that means

package fr.xxx.dao;
public class TestClass{}

The directory is xxx/dao/TestClass.java, 
please note the src/fr is treated as a sourceDirectory. Then the fr is ignored.

Normally when we would like to exclude somes package from the Sonar analysis, it can be simply done by setting them at each quality maven plugin e.g. findbugs,PMD, cobertura, etc.

Anyhow the Sonar also provides the configuration for each project as well. We can set by using the following steps: -

Please note: I'm using Sonar version 3.5, the menu may be different if you're using the different version.

  1. Go to our sonar web site, e.g. https://myhost/sonar
  2. Go to our project by selecting it from the dashboard.
  3. At the top right you will see the Configuration menu. Click it and select Settings.
  4. At the Settings page, select Exclusions menu.
  5. At Exclusions you can simply set the excluded modules which is able to use the wildcard as well. (You may see the example at the bottom of the page.)
  6. The example from your case, the exclusion should be fr/**. (Exclude all under the folder fr).

Please refer to the following link: -

  1. Project Administration
  2. Excluding Files and
  3. Analysis Parameters

I hope this may help.

Upvotes: 2

Related Questions