What's the correct way to configure Sonarqube to compile with Java 8 and run analysis with Java 11

Template for a good new topic, formatted with Markdown:

  • ALM used (Azure DevOps)
  • CI system used ( Azure DevOps)
  • Scanner command used:
  name: DEVBUILD
  demands: maven

- task: Maven@4
    mavenPomFile: 'pom.xml'
    goals: 'clean compile package install -P=DEV -DskipTests=true'
    publishJUnitResults: false
    javaHomeOption: 'Path'
    jdkDirectory: 'C:\Program Files\Amazon Corretto\jdk1.8.0_332'
    mavenVersionOption: 'Path'
    mavenDirectory: 'D:\build_resources\Maven'
    mavenSetM2Home: true
    mavenAuthenticateFeed: false
    effectivePomSkip: false
    sonarQubeRunAnalysis: false
- task: PowerShell@2
  displayName: 'Run sonar:sonar'
    targetType: 'inline'
    script: |
      mvn sonar:sonar```

Error: ```[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin: (default-cli) on project TechPubViewer: Execution default-cli of goal org.sonarsource.scanner.maven:sonar-maven-plugin: failed: An API incompatibility was encountered while executing org.sonarsource.scanner.maven:sonar-maven-plugin: 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
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.codehaus.mojo:sonar-maven-plugin:
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/C:/Windows/ServiceProfiles/NetworkService/.m2/repository/org/sonarsource/scanner/maven/sonar-maven-plugin/
[ERROR] urls[1] = file:/C:/Windows/ServiceProfiles/NetworkService/.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.4/plexus-sec-dispatcher-1.4.jar
[ERROR] urls[2] = file:/C:/Windows/ServiceProfiles/NetworkService/.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
[ERROR] urls[3] = file:/C:/Windows/ServiceProfiles/NetworkService/.m2/repository/org/codehaus/plexus/plexus-utils/3.2.1/plexus-utils-3.2.1.jar
[ERROR] urls[4] = file:/C:/Windows/ServiceProfiles/NetworkService/.m2/repository/org/sonarsource/scanner/api/sonar-scanner-api/
[ERROR] urls[5] = file:/C:/Windows/ServiceProfiles/NetworkService/.m2/repository/commons-lang/commons-lang/2.6/commons-lang-2.6.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
[ERROR] -----------------------------------------------------
[ERROR] -> [Help 1]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginContainerException
##[error]PowerShell exited with code '1'.

I’ve also tried using the azure devops plugins ‘sonarqube prepare’. I’ve searched through forum and I see few solutions like separating it into 2 tasks and changing the java version right after the build task. The issue I’m having is, I build the project with Java 8 and then I run the mvn sonar:sonar which should run using the Java 11 (because that’s the default JAVA_HOME) for my agent but it does not. I’m also very confused as to why it’s compiling the code again as it should only be doing analysis. When I ran the command on this agent itself, after compiling the project, the command mvn sonar:sonar tries to compile the project again in Java 11 and fails. Any help here would be greatly appreciated.

Hey there.

You’ll need to add a step to override the JAVA_HOME environment variable pointing to JDK 11/17 before running the analysis. If you’re just running mvn sonar:sonar via a Powershell script, you could do this in the same inline script.

Hi Colin, thank you for your reply, but if you could explain few things to me as I’m still very new to sonarqube and also first time for our organization, my JAVA_HOME is set to JAVA 11 in the agent. I am specifying in my first task (maven) that I want to specifically use the JAVA 8 (which is also installed in the agent). Shouldn’t the second task (powershell) already use the JAVA 11 as it’s set to be JAVA_HOME. Also, if there is a proper way to run sonarscan using the plugins, could you please route me or give me the instructions. I tried the sonar plugins in Azure Dev, first sonar prepare and then maven task to build the project, and checked the run sonar analysis box in the maven task. That fails because of the version mismatch. I also tried to leave that unchecked and add a sonar analysis as a task after the maven task, and that failed because it tells me to use the checkbox in the maven task to do it instead. I would really appreciate it if you could guide me here a bit. I will go on and try to set the java_home to 11 (although it’s already set to 11 by default) and see if that resolves issue. Thank you again!

If I had to guess, the Maven@V4 task is using the jdkDirectory (alis for jdkUserInputPath) to set JAVA_HOME and it doesn’t revert to the default Java installation afterwards.

Here’s the code for that Pipeline task

The proper way to run SonarQube analysis, if you need different versions of Java for the build vs. analysis, is to use the Prepare task, leave the Run SonarCloud Analysis checkbox clear from the Maven task, and then add the mvn sonar:sonar, much like you’re already doing.

Thanks for guiding me through. You were right, it apparently did reset the JAVA_HOME to JAVA 8 after the maven build task (weird, but maybe I just don’t understand the logics of it). Used Powershell to change the JAVA_HOME to JAVA 11 and I was able to run do analysis. I wasn’t able to run the mvn sonar:sonar as it said the sonar plugin was missing which is weird, but I was able to bypass by using specifying the sonar source. I have posted a snapshot below to help anyone who might face same issue in future.

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