sonar.jacoco.reportPaths is not working with sonar gradle plugin - gradle

I have a multi-module gradle project. Tests for one of my module are in separate modules. For ex: ProjectA, ProjectATest1, ProjectATest2, ProjectATest3. Jacoco execution reports are getting created in the test projects. I want to do sonar analysis of my ProjectA and sonar fails to find the jacoco files.
in ProjectA, sonarqube properties, I gave
sonarqube{
properties {
property "sonar.jacoco.reportPaths","ProjectATest1/gradleBuild/jacoco/Tests.exec", "ProjectATest2/gradleBuild/jacoco/Tests.exec", "ProjectATest3/gradleBuild/jacoco/Tests.exec"
}
}
But I get this exception
Could not find method property() for arguments
[sonar.jacoco.reportPaths,"ProjectATest1/gradleBuild/jacoco/Tests.exec",
"ProjectATest2/gradleBuild/jacoco/Tests.exec",
"ProjectATest3/gradleBuild/jacoco/Tests.exec"]
SonarQube: Coverage incomplete on multimodule gradle project with JaCoCo the answer says it should work. Is there a bug in sonar gradle plugin?

Could not find method property() for arguments [sonar.jacoco.reportPaths,"ProjectATest1/gradleBuild/jacoco/Tests.exec", "ProjectATest2/gradleBuild/jacoco/Tests.exec", "ProjectATest3/gradleBuild/jacoco/Tests.exec"]
It means that the method property disallow passing 4 arguments. You have to define paths in a one string (comma separated list):
sonarqube{
properties {
property "sonar.jacoco.reportPaths", "ProjectATest1/gradleBuild/jacoco/Tests.exec,ProjectATest2/gradleBuild/jacoco/Tests.exec,ProjectATest3/gradleBuild/jacoco/Tests.exec"
}
}

Related

Need help on java2wsdl using gradle

I have a java project to which I build it using gradle build and generate a war file.
Currently my requirement is to generate WSDL file at the time of build from java classes. I came to know about axis2-java2wsdl-maven-plugin and found the syntax of applying it in gradle. But I am not able to get the tasks list or the example of using this plugin in gradle to generate the WSDL file using this plugin.
Can anybody let me know of how to use this plugin or any other help so that I can generate WSDL file form my java classes.
Dependency section which I included in build.gradle:
repositories {
mavenCentral()
}
dependencies {
'org.apache.axis2:axis2-java2wsdl-maven-plugin:1.6.2'
}
axis2-java2wsdl-maven-plugin is a maven plugin not a gradle one.
Moreoever, gradle plugins must be defined in a buildscript closure or a plugins one if you want to use the new plugins DSL.
Here, you are just using the maven plugin as a regular dependency for your project.
As far as i know, there is not "java2wsdl" gradle plugin.

SonarQube: Coverage incomplete on multimodule gradle project with JaCoCo

I am building a SonarQube 6.2 server which is already analyzing my Java 8/Gradle 3.3 projects. When adding JaCoCo to a multimodule gradle project, I realized that SonarQube is measuring code coverage on a "per-module" basis:
If a class is located in module A and a test for this class is located in module B, SonarQube figures the class is not covered.
I want to measure code coverage across all modules, not on a per module basis. How do I achieve this?
There are lots of similar questions but no helpful answers, although the situation seems quite common to me. Jenkins for example does that per default.
I decided to build a blueprint on github to clarify the issue.
The main build.gradle consists of
plugins { id "org.sonarqube" version "2.2.1" }
subprojects {
apply plugin: 'java'
apply plugin: 'jacoco'
repositories { mavenCentral() }
dependencies { testCompile "junit:junit:4.12" }
}
modA/build.gradleis empty.
It contains 3 classes: TestedInModA, TestedInModATest and TestedViaModB.
modB/build.gradlejust declares a dependency to modA:
dependencies { compile project(':modA') }
It contains just one class: TestedViaModBTest, testing the class TestedViaModB located in modA.
My (private) Jenkins instance shows 100% coverage for the two classes included while SonarQube says only the class TestedInModA (which is tested in its own module) is covered.
How can I modify my build process to see "cross-module coverage" in SonarQube?
I would love to update my project so future visitors to this question can find a working example.
My working solution (thanks #Godin)
add the following to the subprojects closure
tasks.withType(Test) {
// redirect all coverage data to one file
// ... needs cleaning the data prior to the build to avoid accumulating coverage data of different runs.
// see `task cleanJacoco`
jacoco {
destinationFile = file("$rootProject.buildDir/jacoco/test.exec")
}
}
add
task cleanJacoco(dependsOn: 'clean') { delete "$buildDir/jacoco" }
outside the subprojects closure.
When you perform build JaCoCo Gradle Plugin will produce modA/build/jacoco/test.exec and modB/build/jacoco/test.exe that contain information about execution of tests in modA and modB respectively. SonarQube performs analysis of modules separately, so during analysis of modA for the file TestedViaModB it sees only modA/build/jacoco/test.exec.
Most common trick to cross boundaries - is to collect all coverage information into single location. This can be done with
JaCoCo Gralde Plugin
either by changing location - see destinationFile and destPath ( since information is appended to exec file, don't forget to remove this single location prior to build, otherwise it will be accumulating information from different builds and not just from different modules ),
either by merging all files into single one - see JacocoMerge task. And then specify this single location to SonarQube as sonar.jacoco.reportPath.
Another trick: SonarQube 6.2 with Java Plugin 4.4 supports property sonar.jacoco.reportPaths allowing to specify multiple locations.
If you are interested in the solution with sonar.jacoco.reportPaths (see answer of Godin), have looke at this gradle code:
tasks.getByName('sonarqube') {
doFirst {
// lazy initialize the property to collect all coverage files
def jacocoFilesFromSubprojects = subprojects.findAll {it.plugins.hasPlugin('jacoco')}
.collect {it.tasks.withType(Test)}.flatten().collect {it.jacoco.destinationFile}
sonarqube.properties {
property "sonar.jacoco.reportPaths", jacocoFilesFromSubprojects.join(',')
}
}
}
This will collect all coverage binary files and set them as comma-separated list to the sonar property. Considers all test tasks of sub projects where jacoco is applied and their jacoco destination file configured.

Is it possible to override sonar.modules in the sonarqube gradle plugin?

I'm trying to set up sonar analysis across a non-standard multi-project directory structure with a root project to aggregate all the sonar data.
Using sonarqube runner, I've tried to set the property sonar.modules with all the sub-projects (which are not in child directories), but the plugin seems to override the property with erroneous default values that cause my build to fail.
The overwrite seems to happen in this java class : https://github.com/SonarSource/sonar-gradle/blob/master/src/main/java/org/sonarqube/gradle/SonarQubePlugin.java on line 190.
Is there a way around this problem?
I have not tried, but looking at how other properties(sonar.projectKey, sonar.projectName, sonar.projectVersion) within the same addGradleDefaults method are begin set, Did you try the same pattern followed for configuring the other properties per SonarQube plugin documentation ?
FYR, Here is the link from SonarQube on How to use the plugin and or override the default values within the gradle build script. http://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Gradle
Here is the code snippet
apply plugin: 'org.sonarqube'
sonarqube {
properties {
property "sonar.projectName", "My Project Name"
property "sonar.projectKey", "org.sonarqube:java-gradle-simple"
}
}

Checkstyle gradle Unable to instantiate AvoidConstantAsFirstOperandInConditionCheck when checking style

I have configured my Gradle build script to make use of checkstyle together with the added sventu checkstyle checks, however when I execute the checkstyleMain task, the build fails with the following error:
* What went wrong:
Execution failed for task ':checkstyleMain'.
> Unable to create a Checker: cannot initialize module TreeWalker - Unable to instantiate AvoidConstantAsFirstOperandInConditionCheck
This happens even though I have included the checkstyle jar in my build. Below is the relevant parts of my build script:
repositories {
mavenCentral()
maven {
url "http://sevntu-checkstyle.github.com/sevntu.checkstyle/maven2"
}
}
checkstyle {
configFile = new File("etc/config/dev.xml");
toolVersion = "6.8"
}
configurations {
checkstyle
}
dependencies {
checkstyle "com.github.sevntu.checkstyle:sevntu-checks:1.13.4"
}
Note that the build works when I remove the dependencies section and test with a checkstyle xml doc that does not have the extra sevntu checks configured. My configuration is also similar to the example at sevntu-checkstyle/checkstyle-samples
What am I missing here?
So I finally figured it out:
Turns out the example at https://github.com/sevntu-checkstyle/checkstyle-samples/blob/master/gradle-project/build.gradle only works if you put the full classpath of the custom checks as the name for each custom check.
This is due to checkstyle not knowing where the custom checks are located in the package. Turns out checkstyle can find this out if you include a checkstyle_packages.xml file in the jar that describes the packages that contains the checks.
Unfortunately there is no such file in com.github.sevntu.checkstyle:sevntu-checks:1.13.4. To get this information, you also need to include "com.github.sevntu.checkstyle:sevntu-checkstyle-maven-plugin:1.13.4", which basically contains nothing but the checkstyle_packages.xml file.
So I've added this to my dependencies and the checkstyle rules finally parses:
dependencies {
checkstyle "com.github.sevntu.checkstyle:sevntu-checks:1.13.4",
"com.github.sevntu.checkstyle:sevntu-checkstyle-maven-plugin:1.13.4"
}
Hoping this will save someone some pain in future :)

Code coverage for EJB's Using Wildfly-Arquillian-Gradle-Jacoco

I am trying to set-up code coverage for my project. I am using Wildfly 8.2 server, gradle as a build tool, and JUnit and Arquillian for testing. In gradle I have configured jacoco plugin to generate code coverage. I have a task called jacocoTestReport which allows me to generate an html report.
Something about running the tests:
I am working on a multi module project, each sub-project has a Deployments class in which we have two methods - one for creating a shrinkwrap archive of REST classes and other for non-REST classes. In arqullian.xml we are configuring this as REST_CONTAINER and NON_REST_CONTAINER and giving path to WildFly installation directory. When we run gradle build test , It will run the whole tests by deploying the REST.ear and non-REST.ear and generate the coverage reports.
The issue is code coverage for EJB's and other server managed classes are showing 0% (From primary ananlysis of coverage report). Also I analysed the jacoco.exec, there I found the classes which are showing 0% coverage are not listed in the file (Mostly bean classes).
Can someone provide me the correct configuration which works for the combination: Wildfly-Arquillian-Gradle-Jacoco
Note: I am ok to use tools other than jacoco, tried cobertura but same result.
This worked for me (but I used jboss7 should not be a problem) source:https://developer.jboss.org/thread/241883
apply plugin: 'jacoco'
jacoco {
toolVersion = '0.7.4.201502262128'
reportsDir = file("$buildDir/jacoco")
}
dependencies {
testCompile 'YOUR_ARQUILLIAN_ADAPTER'
testCompile 'org.jboss.arquillian.junit:arquillian-junit-container:1.1.5.Final'
testCompile 'org.jboss.arquillian.extension:arquillian-jacoco:1.0.0.Alpha7'
}
// Important: add the Jacoco libs on the test classpath (required for the Jacoco Arquillian extension to work).
sourceSets {
test.runtimeClasspath += configurations.jacocoAnt
}

Resources