Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixing 'Class is not accessible through the ClassLoader.' warning with Jenkins, SonarQube, and Gradle

When SonarQube analyzes my Java project which is built using Gradle and Jenkins, I get a lot of warnings about third party libraries not being accessible through the ClassLoader:

WARN  - Class 'org/slf4j/Logger' is not accessible through the ClassLoader.
WARN  - Class 'com/google/gson/Gson' is not accessible through the ClassLoader.

These libraries are all listed as dependencies in my build.gradle.

I read here about using the sonar.libraries property where I'd give a path to the Jar. But because Gradle downloads those dependencies for me, the paths look like this on my machine: /home/siberut/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.5/6b262da268f8ad9eff941b25503a9198f0a0ac93/slf4j-api-1.7.5.jar.

And those paths change with every new version of the library.

So how can I get I get rid of those warnings? Is there maybe a way to let Gradle tell SonarQube about the location of the Jars?

Thanks

Edit: I'm using SonarQube Server 4.1.1, Gradle Plugin 1.23, Sonar Plugin 2.1, Sonar Runner 2.3 and gradle --version gives:

------------------------------------------------------------
Gradle 1.10
------------------------------------------------------------

Build time:   2013-12-17 09:28:15 UTC
Build number: none
Revision:     36ced393628875ff15575fa03d16c1349ffe8bb6

Groovy:       1.8.6
Ant:          Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:          2.2.0
JVM:          1.7.0_21 (Oracle Corporation 23.7-b01)
OS:           Linux 3.10-2-486 i386

Here is the complete console output of a build including all the warnings: Link

Jenkins calls my build.gradle like this: Jenkins calls build.gradle

Jenkins calls SonarQube like this: Jenkins calls SonarQube


Edit: Just like Peter Niederwieser said, letting Gradle invoke SonarQube gets rid of the warnings. The relevant part of my configuration is here.

like image 237
Matthias Braun Avatar asked Oct 02 '22 14:10

Matthias Braun


2 Answers

You'll have to set sonar.libraries. But in order to set this property manually, you'll have to define a Gradle task that copies all external dependencies to a lib directory, and then use sonar.libraries=path/to/lib/*.jar to reference them. Instead, I'd invoke Sonar via the sonar-runner Gradle plugin, which will take care of setting the above properties (plus sonar.libraries and others) for you.

like image 62
Peter Niederwieser Avatar answered Oct 12 '22 02:10

Peter Niederwieser


The following fixed this issue for me in build.gradle

sonarqube {
    properties {
        def compileDependencies = project.configurations.compile.files.collect {it.path}join(",")
        def compileOnlyDependencies = project.configurations.compileOnly.files.collect {it.path}join(",")

        property "sonar.java.libraries", "$compileDependencies,$compileOnlyDependencies"
        property "sonar.test.libraries", "$compileDependencies,$compileOnlyDependencies"

    }
}
like image 44
schnatterer Avatar answered Oct 12 '22 00:10

schnatterer