If you are getting below error while running sonar and calculating code coverage from gradle

❌
java.io.File org.gradle.api.tasks.testing.JUnitXmlReport.getFile()

then you are at the right place.

Solution

For solving this error, you can attempt any of the below solutions.

1. Check SonarQube Plugin Compatibility

Check if the version of sonarqube plugin is compatible with the gradle version.

For this, check the below plugins block in your build.gradle file

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
plugins {
id "org.sonarqube" version "X.Y.Z" // Use the latest compatible version
}
plugins { id "org.sonarqube" version "X.Y.Z" // Use the latest compatible version }
plugins {
  id "org.sonarqube" version "X.Y.Z" // Use the latest compatible version 
}

Or if you are using legacy approach to applying plugins, then check the dependencies section inside buildScript as below

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
buildScript {
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.3.0.3225"
}
}
buildScript { dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.3.0.3225" } }
buildScript {
  dependencies {
   classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:4.3.0.3225"
  }
}

This solution worked for me when I faced the same issue.

2. Upgrade Gradle

Upgrade gradle version to a higher version or to latest version in your project.

For this, you can use gradle wrapper with below command

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
./gradlew wrapper --gradle-version=8.2.1
./gradlew wrapper --gradle-version=8.2.1
./gradlew wrapper --gradle-version=8.2.1

3. Verify Java Version

Check the java version to be compatible between gradle and sonarqube versions.

There might be a change that the java version is higher than the gradle and sonarqube versions.

4. Ensure reporting file

Ensure your test reports are being generated properly, as SonarQube relies on these reports.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
test {
useJUnitPlatform() // Ensure JUnit 5 compatibility
reports {
junitXml.enabled = true
html.enabled = true
}
}
test { useJUnitPlatform() // Ensure JUnit 5 compatibility reports { junitXml.enabled = true html.enabled = true } }
test {
    useJUnitPlatform() // Ensure JUnit 5 compatibility
    reports {
        junitXml.enabled = true
        html.enabled = true
    }
}

5. Clear build cache

Sometimes, cached files may cause issues.

Clear the build cache:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
./gradlew clean build --no-build-cache
./gradlew clean build --no-build-cache
./gradlew clean build --no-build-cache

6. Inspect Stacktrace

Finally, if none of the above solutions work, then run your sonarqube task with stacktrace flag as shown below to get more information about the problem

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
./gradlew sonarqube --stacktrace --info
./gradlew sonarqube --stacktrace --info
./gradlew sonarqube --stacktrace --info

Hope the article was useful and solved the problem.

Categorized in:

Gradle,