Issues with Maven and SonarQube

We upgraded the SonarQube server from 7.5.4 to the next LTE version (8.9) then upgraded from there to v9.4. Our CICD is built on Jenkins. All scanning from Jenkins to SonarQube had been working prior to the upgrade. .Net jobs used Sonar Scanner for MSBuild. Java jobs use Maven.

We ran into a couple of issues after the upgrade. First, we resolved .Net Sonar Scanner issues by upgrading Sonar Scanner to v4.7 and Sonar Scanner for MSBuild to Sonar Scanner for .Net and modifying the sonar-scanner.bat file to use the embedded Java. That allows it to use Java 11 even though the Java_Home variable on the server points to Java 8.

However, we still have issues running Maven as it still has Java version issues. Here’s the error:
ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar (default-cli) on project : Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar failed: An API incompatibility was encountered while executing org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar: java.lang.UnsupportedClassVersionError: org/sonar/batch/bootstrapper/EnvironmentInformation has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

This points to a Java mismatch. We need a way when using the Maven command to use Java 11 while the server Java_Home variable remains pointing to Java 8 which leads to the question:

How we can make a command line change so that the command for SonarQube scanning can point to Java 11 (either by using an alternate config file or some other way, while all other Maven commands aren’t effected (In other words, not changing the config making all Maven commands use Java 11.)

ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar (default-cli) on project : Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar failed: An API incompatibility was encountered while executing org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar: java.lang.UnsupportedClassVersionError: org/sonar/batch/bootstrapper/EnvironmentInformation has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

This points to a Java mismatch. We need a way when using the Maven command to use Java 11 while the server Java_Home variable remains pointing to Java 8 which leads to the question:

How we can make a command line change so that the command for SonarQube scanning can point to Java 11 (either by using an alternate config file or some other way, while all other Maven commands aren’t effected (In other words, not changing the config making all Maven commands use Java 11.)

The command is (JOB is replacement for the actual job name):

“mvn sonar:sonar -Dsonar.projectKey=JOB -Dsonar.projectName=JOB -Dsonar.branch.name=Ola-sonar-sdds-testing --batch-mode”

Since the Jenkins server runs Java 8, we need to override that in the Maven command to point to Java 11 which is on the server in another directory. As I mentioned, Java_Home points to Java 8.

Any help is appreciated!

Hey there.

Congrats on your ugprade!

I think you’ll find the Scanner Environment documentation helpful. There is specifically a section on Jenkins. I suggest looking at the various options (based on what kind of Jenkins job you use).

Hi,

in our generic Maven pipeline - wrapped in a shared library - we split the mvn build and
mvn sonar:sonar, so the mvn build might run with another Java version, i.e. Java 8

The Sonarqube stage of the pipeline has

nodejs(configId: xxx, nodeJSInstallationName: 'nodejs-16') {
  withSonarQubeEnv(sonarenv) {
    withMaven(jdk: 'java-11-openjdk', maven: "xxx", globalMavenSettingsConfig: 'xxx') {
      genericSh("mvn ${sonarDebug ? '-X' : ''} ${p.mvnPomPath ? "-f \"$p.mvnPomPath\"" : ''} ${isDMZ ? mvnDMZparams : ''} sonar:sonar ${sqProjectVersion ? "-Dsonar.projectVersion=$sqProjectVersion" : ''} -Dsonar.branch.name=$BRANCH_NAME")
    }
  }
}

genericSh is a shared library

// vars/genericSh.groovy

def call(cmd) {
  if (isUnix()) {
    sh cmd
  }
  else {
    bat cmd
  }
}

and p is the name of a map holding the values of a yaml config file with specific values
for the pipeline (release branch, email recipients …)
The Java version for the Sonarqube stage is fixed to openjdk 11 and nodejs 16 - nodejs is needed for CSS i.e.

Gilbert

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.