In SonarQube 7.6 I am using the the sonar-maven-plugin to analyze a multi-module maven project. Due to the project directory structure being side-by-side instead of a subdirectory layout this does not set the base project directory correctly.
Example layout:
~/workspace/modules/pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>modules</name>
<modules>
<module>../module1</module>
</modules>
<plugins>
<plugin>
<groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<version>3.6.0.1398</version>
</plugin>
</plugins>
</project>
~/workspace/module1/pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../modules</relativePath>
</parent>
<artifactId>module1</artifactId>
<packaging>jar</packaging>
<name>module1</name>
</project>
Then running:
cd /home/ds/workspace/modules
mvn clean install sonar:sonar
Results in an error
Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project module1: SCM provider autodetection failed. Both svn and git claim to support this project. Please use “sonar.scm.provider” to define SCM of your project.
So re-running with mvn clean install sonar:sonar -Dsonar.scm.provider=git
produces this error caused from jgit:
...
[INFO] Project key: com.example:modules
[INFO] Base dir: /home/ds/workspace
[INFO] Working dir: /home/ds/workspace/modules/target/sonar
...
[INFO] Indexing files...
[INFO] Project configuration:
[INFO] Indexing files of module 'module1'
[INFO] Base dir: /home/ds/workspace/module1
[INFO] Source paths: pom.xml, src/main/java
[INFO] Test paths: src/test/java
[INFO] Indexing files of module 'modules'
[INFO] Base dir: /home/ds/workspace
[INFO] Source paths: modules/pom.xml
[INFO] 162 files indexed
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
...
[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.6.0.1398:sonar (default-cli) on project modules: Not inside a Git work tree: /home/ds/workspace -> [Help 1]
This is because there is no git project in the /home/ds/workspace directory because the base directory should really be /home/ds/workspace/modules. As a work around I cloned the modules project also in to /home/ds/workspace so that there is a .git work tree. This makes the error go away but then causes jgit issues when it attempts to read blame information due to the files not being found from the directory it’s trying to run blame from (/home/ds/workspace):
[WARNING] Missing blame information for the following files:
[WARNING] * module1/src/main/java/com/example/SomeClass1.java
[WARNING] * module1/src/main/java/com/example/SomeClass2.java
To me this is a bug as you can run the maven plugin and it reads the modules just not in to the correct directory structure that is fit for SonarQube. There should at least be an error raised like “Maven multi-module must be in a sub-directory layout.”