Search code examples
javajenkinssonarqubesonar-runner

Adding java8 tag to a rule does not prevent it to be played on non-java8 code


I'm working with SonarQube, running on a jenkins server for test source code in java. I specify the property sonar.java.source=1.6 in the file sonar-project.properties and I have a rule whose tag has been manually added with the value java8. In theory, this rule should not be applied for my analysis, but nevertheless it is doing it.

These are my sonar-project.properties:

# Required metadata
sonar.projectKey=TestSonar
sonar.projectName=Proyecto java TestSonar analizado con SonarQube Runner
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=src

# Language
sonar.language=java

# Encoding of the source files
sonar.sourceEncoding=UTF-8

# java version used by source files:
sonar.java.source=1.6

this is the log output:

10:14:36.269 INFO  - Language is forced to java
10:14:36.273 INFO  - Load server rules
10:14:36.324 INFO  - Load server rules (done) | time=51ms
10:14:36.365 INFO  - Base dir: C:\Develop\TestSonar
10:14:36.365 INFO  - Working dir: c:\develop\TestSonar\.sonar
10:14:36.369 INFO  - Source paths: src
10:14:36.369 INFO  - Source encoding: UTF-8, default locale: es_AR
10:14:36.370 INFO  - Index files
10:14:36.393 INFO  - 3 files indexed
10:14:36.396 INFO  - Quality profile for java: Sonar way
10:14:36.765 INFO  - JaCoCoSensor: JaCoCo report not found : C:\Develop\TestSonar\target\jacoco.exec
10:14:36.765 INFO  - JaCoCoItSensor: JaCoCo IT report not found: C:\Develop\TestSonar\target\jacoco-it.exec
10:14:36.767 INFO  - ERROR: No files to be analyse found, skipping this execution.
10:14:36.791 INFO  - Sensor JavaSquidSensor
10:14:37.012 INFO  - Configured Java source version (sonar.java.source): 6
10:14:37.022 INFO  - JavaClasspath initialization...
10:14:37.022 WARN  - Bytecode of dependencies was not provided for analysis of source files, you might end up with less precise results. Bytecode can be provided using sonar.java.libraries 

this is the rule that should be omitted for this analysis due to the java version:

I'm new to this, if you need any other information let me know

PS: sorry for my English


Solution

  • This is not a False positive, this is simply not the way the analyzer is working.

    Let me explain: Only a few rules from the SonarJava analyzers are explicitly targeting java 8 source code (and usually following versions). Around 20 for SonarJava 4.15. These rules are explicitly tagged with the java8 tag, but they are also implemented in a way they will react depending on the source version. This tag mechansim goes only one way:

    • Rules are defined with default tag(s), describing the way they work.
    • Adding/removing a tag does not change their behavior.

    Consequently, adding the java8 tag to a rule which do not have the tag won't make it react only on java 8 code, it has to be done at the implementation level. The same for way you can not simply add a "test" tag and expect it to be played against test sources only.

    So, in your case, this simply won't work. The rule will continue to apply on any code, independently from the tag.

    Note: Regarding the warning you are getting in the logs. You should also provide the property sonar.java.binaries to the analyzer, because without it, SonarJava won't operate at its best. It needs access to compiled bytecode of your project. Starting from version 4.12` SonarJava, it will also simply fail the analysis without the property filled.