javamavensonarqubemaven-compiler-pluginsonar-maven-plugin

sonar-maven-plugin fails to recognise jdk version from "maven.compiler.release"


Behaviour of sonar-maven-plugin varies when maven-compiler-plugin is/isn't defined. I spent some time to find out the proper setting.

Create a simple Java maven project and keep the pom.xml simple.

<?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>org.example</groupId>
    <artifactId>testing</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.release>17</maven.compiler.release>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

Execution

mvn org.sonarsource.scanner.maven:sonar-maven-plugin:<version>:sonar -Dsonar.login=<token>

Case 1:

Result:

Configured Java source version (sonar.java.source): 5

Case 2:

Result:

(It means the plugin can only get the version from source instead of release)

Configured Java source version (sonar.java.source): 17

Case 3:

Result:

(No idea why the version changes when maven compiler plugin is defined.)

Configured Java source version (sonar.java.source): 6

Case 4:

Result:

(No idea why the version changes when different version of maven compiler plugin is defined.)

Configured Java source version (sonar.java.source): 7

Case 5:

Result:

(Upgrading sonar maven plugin doesn't help.)

Configured Java source version (sonar.java.source): 5

Solution

  • Proper setting

    Execution:

    mvn org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar -Dsonar.login=<token>
    

    pom.xml

    <?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>org.example</groupId>
        <artifactId>testing</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <maven.compiler.release>17</maven.compiler.release>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.10.1</version>
                </plugin>
            </plugins>
        </build>
    
    </project>