Maven project and sonar scan needs different java version to run

Hi Team,

We have recently upgraded our SonarQube version from 8.9.6 to 9.9.3 and we upgraded our java version to java 17.

Before the upgrade all our builds in bamboo were working fine but post upgrade all our builds break - it seems to be issue with java version.

Our maven projects needs to run with java7 or java8 where as for sonar analysis it seems we need java17. So when we run the build with maven project jdk (java7 or 8) build fails for sonar analysis where as if we run the build with java17 it fails for maven build.

We are building in a single maven step - For eg: mvn clean install sonar:sonar

Is there a way we can tell maven to use different jdk while running mvn clean install and to use a diff jdk when running sonar:sonar. Note : We are running above in a single maven goal.

Please suggest - if this is achiveable - if there is any way we can define the JDK path for sonar in maven settings.xml file or in maven goals or any other alternative.

Note: We need to run our builds in single step and dont want to run in phases - like to run the clean install first than in second step change jdk to 17 and run soanr analysis.

Regards,
Saad

Welcome :slight_smile:

either use mvn cli like that
mvn clean install sonar:sonar -Dsonar.java.jdkHome=pathToJdk17

or use this in your pom

<properties>
  <sonar.java.jdkHome>pathToJdk17</sonar.java.jdkHome>
</properties>

or finally run Maven with Java 17 and use

<properties>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
</properties>

Gilbert

Hi Gilbert,

Thanks for your response.

I tried both the solution provided by you running in mvn ci and changing pom but unfortunately nothing worked. Is there anything we can do to settings.xml file of maven version?

Regards,
Saad

Hi Saad,

sorry my fault, didn’t read the documentary carefully enough.

Documentation Java has

Project’s specific JDK

In some situations, you might have to analyze a project built with a different version of Java than the one executing the analysis. The most common case is to run the analysis with Java 11, while the project itself uses Java 8 or before for its build.

If this is your case, you will need to set the sonar.java.jdkHome property manually to point the appropriate JDK (see below). By doing this you will specify which JDK classes the analyzer must refer to during the analysis. Not setting this property, while it would have been required, usually leads to inconsistent or even impossible to fix issues being reported, especially in relation with native JDK classes.

.. must refer to during the analysis ... means it’s a parameter but not the Java runtime analysis runs on.

That would also not work at all using mvn clean sonar:sonar as the latest sonar maven plugin versions do not run on Java 8.

Means it’s the other way around.

Either split your mvn cli calls = one for clean install running with Java 8, the second for mvn sonar:sonar running with Java 17 or use Java 17 and use the maven.compiler properties
In both cases use -Dsonar.java.jdkHome property to inform the scanner the analysis is for a project on Java 8.

We’re using two separate mvn cli calls, as there are legacy projects still on Java 8 or Java 11.

Hope that makes sense,
Gilbert

Hi everyone, I’ll have the same issue too, upgrading soon from version 8.9 to 9.9.
But, doing builds in Docker containers, it’s even more complicated.
For everyone who has a similar setup, will this make sense?

  1. Do the compilation (mvn clean install) in the Docker container of Maven with Java 8
  2. Copy the compilation artifacts to the Docker container of Maven with Java 11 or Java 17
  3. Do the scan (mvn sonar:sonar) in the Docker container of Maven with Java 11 or Java 17

Thanks