1
votes

I have a simple project that need to Run JUnit test and let SonarQube to scan / run the Test of JUnit result with SonarScanner. I put my project at Github here

There are some problems:

  1. WARN: Class "XXX" is not accessible through the classloader sonar (Reference)
  2. No jacoco-it.exec and jacoco-ut.exec, only run jacoco.exec at SonarScanner
  3. Cannot exclude some files when running Test JUnit, because I only want to Run Test in my Service Folder (Reference)

This is my sonar-project.properties :

# Required metadata
sonar.projectKey=com.example:Sample
sonar.projectName=Java :: Example :: SonarQube Scanner
sonar.projectVersion=1.0

# Comma-separated paths to directories with sources (required)
sonar.sources=src
sonar.exclusions=src/main/resources/**
#sonar.test.inclusions=src/main/java/com/example/service/**, src/test/java/**
#sonar.test.exclusions=src/main/java/com/example/controller/**

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

#Jacoco
sonar.junit.reportsPath=target/surefire-reports
sonar.jacoco.reportPath =target/jacoco.exec
sonar.jacoco.reportMissing.force.zero=true
sonar.java.binaries=target/classes/**
#sonar.java.libraries=libs/**

When I Run Maven Test until build success, It create some files and folders in folder Target.

enter image description here

After that I run my SonarScanner to Scan my project, but I got those problems.

My question:

  1. How can I run my SonarQube Scanner without any WARN ?
  2. How to exclude files properly when scanning JUnit result? Because I use the reference settings, and Coverage still scan the files
  3. How to create jacoco-it.exec and jacoco-ut.exec and run it on SonarScanner?

My project is based on this Reference

1
You don't need sonar-project.properties if you are using Maven: please refer to documentation about Analysis using Scanner for Maven - docs.sonarqube.org/display/SCAN/… usage of it should be enough to avoid many configuration headaches, e.g. such as warning Class "XXX" is not accessible. jacoco-ut.exec and jacoco-it.exec are products of execution of jacoco-maven-plugin for unit tests and integration tests respectively. - Godin
I use sonarqube and sonarscanner as third party. That's why I have sonar-project.properties. I download the RAR of SonarQube and SonarScanner, and then First, I start my SonarQube and then I go to CMD, going to my folder (sampleProject) and then I run my Scanner. The setting of my SonarScanner vased on sonar-project.properties - David Vincent
Q: "How can I run my SonarQube Scanner without any WARN ?" A: Use SonarQube Scanner for Maven. - Godin

1 Answers

2
votes

Use SonarQube Scanner for Maven instead of just SonarQube Scanner. Otherwise you fall into troubles with configuration, into which you fall, because need to do everything manually:

  • sonar.sources most likely is incorrect - should be src/main/java and will be properly configured by Scanner for Maven, and that's why you're getting undesired test files analysed as main files
  • as well as sonar.tests that normally for Maven based projects points to src/test/java
  • sonar.java.libraries is empty and that's why you're getting Class "XXX" is not accessible through the classloader sonar - it might be quite hard to list all required JAR files of dependencies of your Maven project, while again Scanner for Maven will do this automatically and most importantly will do this correctly
  • sonar.projectKey with Scanner for Maven will be set automatically to Maven groupId:artifactId
  • sonar.projectName and sonar.version with Scanner for Maven will be set correctly to Maven project name and version respectively, so that you don't need to change version, when it will change in Maven project
  • others such as sonar.sourceEncoding, sonar.java.binaries, sonar.java.test.binaries and sonar.java.test.libraries with Scanner for Maven will also be set automatically

If needed you'll still be able to provide other additional properties

  • via command line such as mvn ... sonar:sonar -Dsonar.something=something
  • or in SonarQube UI
  • or specify them directly in properties section of pom.xml

How to create jacoco-it.exec and jacoco-ut.exec and run it on SonarScanner?

Usually jacoco-ut.exec refers to coverage data about unit tests and jacoco-it.exec refers to coverage data about integration tests. They are created by jacoco-maven-plugin, in your pom.xml you configured jacoco-maven-plugin for creation of jacoco.exec.