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:
WARN: Class "XXX" is not accessible through the classloader sonar (Reference)
No jacoco-it.exec and jacoco-ut.exec, only run jacoco.exec at SonarScanner
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.
After that I run my SonarScanner to Scan my project, but I got those problems.
My question:
How can I run my SonarQube Scanner without any WARN ?
How to exclude files properly when scanning JUnit result? Because I use the reference settings, and Coverage still scan the files
How to create jacoco-it.exec and jacoco-ut.exec and run it on SonarScanner?
My project is based on this Reference
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.
Related
I have a multimodule Maven project, it is analyzed using the Sonar Maven runner and coverage is done with Jacoco. For our integration tests we want to see the coverage across all modules (because they are integration tests after all).
Therefore we configure the jacoco-it.exec file to be in ${user.dir}, with the appendproperty to true. This way all modules append their information to the same location and coverage is calculated over all modules.
But since append is true the file will still be there on a next run, since it isn't placed in a directory that maven cleans. This leads to incorrect coverage reports.
What is the best way to clean up this file after a sonar run? Ideally I would like to configure this in the same pom profile as our jacoco/sonar configuration, so that no other projects need to remember to set a clean step in Jenkins or whereever. The sonar/jacoco configuration is in a company wide parent pom file.
Since you are using Maven, you could try and use the maven-antrun-plugin to delete the file.
I don't know how you run Sonar Maven, but you can either bind the maven-antrun-plugin task to a phase after the one the Sonar Maven Runner is bound to (and you would have the file deleted automatically and the end of each run) or you can call the maven-antrun-plugin from the command line.
I got a root maven project, under which there are many independent modules (e.g. module_A, module_B, etc.).
One of these module is my integration-tests module, and it uses all the above external modules.
In order to have code coverage for all modules used by integration-tests, I use a workaround based on maven-ant (see this blog post).
Problem is:
The above generates csv/html report, yet sonarqube jacoco widget parses only jacoco*.exec files - which results in 0% code coverage.
Question is:
EDIT
here's an example project for the problem above.
You don't need to use that workaround. You can provide Sonar with integration tests coverage file with following property (you shall use it while executing sonar:sonar goal):
-Dsonar.jacoco.itReportPath=<coverage_file>
Here is detailed documentation:
http://docs.codehaus.org/display/SONAR/Code+Coverage+by+Integration+Tests+for+Java+Project
To sum up:
Compile all your modules.
Execute integration tests with jacoco enabled.
Execute Maven Sonar build adding mentioned property in command line.
I have prepared example project generating both unit and integration coverage results, you can check it here:
https://github.com/octosan/unit-and-integration-jacoco-coverage-with-sonar
You have to:
download newest Sonarqube version and start it
execute command:
mvn clean install sonar:sonar -Dsonar.jacoco.itReportPath=<absolute_path>/itest/target/jacoco-it.exec
add integration coverage widged in project dashboard in Sonar
I have a multimodule maven project with below structure
test-project(parent)-> packaging is pom
- test-project.ear
- test-project.war
The problem is I am generating a xml file during the mvn deploy phase before the sonar scan and it is generating a xml file under the parent project ,in this case it is generating immediately under the test-project. I want this file to be scanned by Sonar, but sonar seems to scan only the xml files under the ear and war files. Could any one help me on this
I am using SonarQube 4.3.2
SonarQube cannot analyse files that are in aggregator projects (i.e. projects with POM packaging).
I've been asked to get SonarQube up and running, but we'll be using maven instead of SonarQube Runner.
Is the sonar-project.properties configuration file only used for analysis with SonarQube Runner, or is it necessary for other analyzers, such as maven?
It is only for SonarQube Runner. If you want to pass extra properties to maven you have to pass it on command line with -D prefix. Example: mvn sonar:sonar -Dsonar.language=java.
Yes, as has been said, it is only for Sonar Runner because in this file you define your Project Name, Project Version, default locale from the source code of your Project and so on...
And in your case, using maven, the information like Project Name, Project Version and all about Project is defined in the pom.xml from your Project, and in the Maven you followed a folder structure and so you don't need to tell the source code locale.
In case of C# analysis also you need to add '-D' prefix. --
Something like this.
-Dsonar.language=cs
-Dsonar.dotnet.visualstudio.solution.file="EventProcessing.sln"
-Dsonar.gendarme.mode=
-Dsonar.gallio.coverage.tool=OpenCover
-Dsonar.dotnet.visualstudio.testProjectPattern=*.Tests*
The Java project I am working on is a list of testcases written in Java for testing C++ code. So I want to run the testcases from the src directory in the test phase of the maven lifecycle. How do I configure the maven surefire plugin to achieve this.
Since it is Java based tests that presumably will not be part of the production code, I suggest that you follow Maven's standard directory layout and move the tests to the src/test/java directory as usual (otherwise, the tests will be part of the binary file if someone executes mvn package).