This post refers to another issue whose post I mistakenly marked as resolved: previous post
The problem remains: the scanner report shows deprecations of java language 9+ on jdk 8 compiled code. “sonar.java.source” isn’t working.
Another setting not making any effect is “sonar.exclusions”. I have set sonar.exclusions=**/*.js,**/*.mjs,**/*.css but the scanner still scan and report problems in some .js files
Using the maven plugin on some java 8 projects, it gives errors like this:
[ERROR] /src/myclass1.java:[23,30] package org.omg.CORBA.portable does not exist
[ERROR] /src/myclass1.java:[400,163] cannot find symbol
symbol: class ApplicationException
location: class myclass1
[ERROR] /src/myclass2.java:[5,24] package javax.annotation does not exist
[ERROR] /src/myclass2.java:[51,21] cannot find symbol
symbol: class Resource
location: class myclass2
Running with the maven plugin on other projects where it works still results in deprecated java code from versions above 8.
But the exclusions seen to work.
Code smell
... new Integer(rs.getInt("someVariable")) ...
Remove this use of "Integer"; it is deprecated.
Gitlab job:
scan-job2:
image: localregistry/maven:3.8-amazoncorretto-11
stage: test
tags:
- docker
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
cache:
key: "${CI_JOB_NAME}"
paths:
- .sonar/cache
rules:
- if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/'
script:
- mvn clean verify sonar:sonar -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_PROJECT_ANALYSIS_TOKEN -Dsonar.projectKey=$SONAR_PROJECT_KEY -Dsonar.qualitygate.wait=true -Dsonar.java.source=1.8 -Dsonar.sourceEncoding=ISO-8859-1 -Dsonar.exclusions=**/*.js,**/*.mjs,**/*.css -Dsonar.branch.name=$CI_DEFAULT_BRANCH -DskipTests -gs $ARTIFACTORY_SETTINGS_XML --batch-mode
In theory, sonar.java.source should do the trick, and tell the analyzer what version you are using. Some rules are then going to adjust based on this value. Unfortunately, the rule java:S1874 is not one of these rules, and it relies solely on what information is read in your byte code.
Now, if your project is using Java 8, and the analyzer reads from bytecode (of the JDK) that the constructor of Integer() is deprecated, it means that the analyzer is reading the bytecode of the wrong JDK (probably your analysis runtime JDK).
This discrepancy is supposed to be handled auto-magically by the gradle or maven scanner (are you using the latest versions?). However, you can set manually the JDK that is used for your project by setting the following property: sonar.java.jdkHome. In your context, you then want to set to this property the "Path to jdk directory used by the project under analysis.". You might want to have a look at this documentation page to know more about the parameter.
The sonar maven plugin runs only on Java 11 and above, right?
So, if i have a java 8 project (which i cannot change) that uses modules/libraries removed in java 11, like javax.annotation, i can’t use the maven plugin, right?
run the maven build with your JDK 8 (required due to the nature of your project)
run the sonar:sonar maven job with JDK 11.
About 1., I believe however that you can still build a java 8 project with JDK 11, as long as you set sources version and release versions in the maven compiler as being java 8, so you should be able to do it in one go.