Can't get sonarcloud to work with maven and java and bitbucket pipelines

We have a java 8 spring boot 2.7 maven 3.1 multi-module project which we are attempting to integrate with sonar using the supplied bitbucket pipeline instructions.

Using this image:

image: maven:3.3.9-jdk-8

we get this error:

ComponentLookupException: org/sonarsource/scanner/maven/SonarQubeMojo has been compiled by a more recent version of the Java Runtime (class file version 55.0)

Using this image:

 image: maven:3-openjdk-17

or this image:

 maven:3-amazoncorretto-21

Gives this error:

java.lang.ExceptionInInitializerError: Unable to make field private com.sun.tools.javac.processing.JavacProcessingEnvironment$DiscoveredProcessors com.sun.tools.javac.processing.JavacProcessingEnvironment.discoveredProcs accessible: module jdk.compiler does not "opens com.sun.tools.javac.processing" to unnamed module 

Any ideas how to get java/maven working with sonar and bitbucket pipelines?

Our parent pom has this:

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<maven.compiler.source>1.8</maven.compiler.source>
	<maven.compiler.target>1.8</maven.compiler.target>
	<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
	<sonar.organization>xxxx</sonar.organization>
	<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>

pipeline file is this:

image: maven:3-openjdk-17

clone:
  depth: full              # SonarQube Cloud scanner needs the full history to assign issues properly

definitions:
  caches:
    sonar: ~/.sonar/cache  # Caching SonarQube Cloud artifacts will speed up your build
  steps:
    - step: &build-test-sonarcloud
        name: Build, test and analyze on SonarQube Cloud
        caches:
          - maven
          - sonar
        script:
          - mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
        artifacts:
          - target/**

pipelines:             
  branches:
    master:
      - step: *build-test-sonarcloud
  pull-requests:
    '**':
      - step: *build-test-sonarcloud

I found a solution. Upgrading lombok to the latest and using maven:3-openjdk-17 got rid of the “processing” error.

However, even though we specify java 1.8 in the pom, if we use the java 17 image we get this error:

package jdk.nashorn.internal.ir.annotations does not exist

Because this was removed sometime after java 8.

However, java 11 seems to be a middle ground which works for sonar and our project, i.e. using:

 maven:3-jdk-11

Hi,

You can use sonar.java.jdkHome to point analysis to the set of JDK classes it should use as a reference in your analysis. By default it uses the classes of the JDK its running with, but as you’ve discovered, that’s not always appropriate.

By setting sonar.java.jdkHome, you can run analysis with the latest Java version if you like and still have all correct version’s classes used.

 
HTH,
Ann