How to integrate jacoco plugin with gradle? - gradle

I need to integrate jacoco plugin in to build.gradle or grade.properties file. anyone help me with setting this

Please check documentation of JaCoCo Gradle Plugin - https://docs.gradle.org/current/userguide/jacoco_plugin.html
For example you can add
apply plugin: 'jacoco'
// ...
test {
jacoco {
enabled = true
destinationFile = file("$buildDir/jacoco/jacoco.exec");
}
}
to your build.gradle file.

Related

Intellij - Gradle - Unable to build - Failed to apply plugin [id 'org.ajoberstar.grgit']

I am working on Intellij 2019.2.4 with Java 11.0.13 JDK and gradle 7.3.3 in settings.
Gradle-wrapper.properties file has - distributionUrl=https://services.gradle.org/distributions/gradle-4.8.1-bin.zip
I have tried manual proxy configuration and auto detect proxy too.
I am unable to build the project and getting following error -
A problem occurred evaluating root project 'bbi'.
> Failed to apply plugin [id 'org.ajoberstar.grgit']
> Could not create plugin of type 'GrgitPlugin'.
> org/eclipse/jgit/errors/RepositoryNotFoundException
My build.gradle has following
repositories {
maven {
url jcenter_url
}
maven {
url maven_central_url
}
}
dependencies {
//Check for the latest version here: http://plugins.gradle.org/plugin/com.jfrog.artifactory
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.9.1"
classpath "org.ajoberstar:gradle-git:1.7.1"
}
}
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'org.ajoberstar.grgit'
If I try to comment the plugin org.ajoberstar.grgit , it fails on the artifactory plugin which comes latter in the build.gradle file as below:
allprojects {
apply plugin: "com.jfrog.artifactory"
artifactory {
contextUrl = main_repo_url //The base Artifactory URL if not overridden by the publisher/resolver
publish {
repository {
repoKey = repo_key
username = artifactory_user_name
password = artifactory_password
maven = true
}
}
Note: Also I am unable to re-import the gradle project. It does not list the gradle tasks or build folders in the tab.
Pls provide a solution to fix this gradle issue.
Thanks

Configure gradle projects in SonarQube

I am working with Liferay DXP and I would like integrate SonarQube in my workspace, I am using gradle.
My workspace is called: test-workpace
My gradle.properies file (path: test-workspace/gradle.properties) is:
systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.sourceEncoding=UTF-8
systemProp.sonar.forceAuthentication=true
systemProp.sonar.login=<mytoken>
# DefiniciĆ³n de variables para el proyecto.
description = 'Gradle - Sample Project'
group = 'com.test.sonarqube.gradle'
version = '1.0.0'
My build.gradle file (path: test-workspace/build.gradle) is:
buildscript {
repositories {
mavenLocal()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath group: "org.sonarsource.scanner.gradle", name:"sonarqube-gradle-plugin", version:"2.5"
}
}
group = 'com.test.sonarqube.gradle'
apply plugin: "org.sonarqube"
When I execute "gradle sonarqube" all workspace is scanned but I would like to configure each modules like a project in SonarQube.
Someone know how to configure gradle files to do it?.
Thank you very much!
As Olaf points out: This question was also posted to https://web.liferay.com/community/forums/-/message_boards/message/104477676
You could configure all the subprojects as a different SonarQube project with the following in your build.gradle:
subprojects{
sonarqube {
properties {
property 'sonar.projectName', "${-> project.name}"
}
}
}
You can also set the property sonar.projectKey or any other property from https://docs.sonarqube.org/display/SONAR/Analysis+Parameters
I took the idea of the lazily evaluated project name from: How can I make Gradle extensions lazily evaluate properties that are set dynamically by tasks?

Gradle subproject ordering and generate jar (spring boot + angular)

I am a Gradle newby. I have the following project setup:
Root
core: contains spring boot 2 application
ui: angular 5 front-end application
Goal: I want to run 'gradle build' from my root folder and it should contain one jar file which includes the Angular app.
I got the 'ui' covered:
apply plugin: "com.moowork.node"
buildscript {
repositories globalRepositories
dependencies {
classpath "com.moowork.gradle:gradle-node-plugin:1.2.0"
}
}
node {
// based on current version of Angular 5
version = "8.9.1"
npmVersion = "5.6.0"
download = true
}
task buildAngular(type: NpmTask) {
args = ['run', 'build']
}
buildAngular.dependsOn(npm_install)
build.dependsOn(buildAngular)
The above gradle definition will build and generate the Angular files in the static backend core application.
The 'core' gradle build file looks like this (I excluded the dependencies), nothing special:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'xxx.xxxxxx'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
buildscript {
repositories globalRepositories
ext {
springBootVersion = '2.0.0.M7'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
jar {
baseName = 'spring-boot-angular'
version = '1.0.0'
}
How can I make this possible? I want the following actions to be triggered when I run 'gradle build' from my root project:
run first 'gradle build' in ui
then second run 'gradle build' in core
use the generated jar file from 'core' as the end result
I can't stand the groovy like syntax, can't wait for Gradle Kotlin DSL to mature :P
Hope somebody can help. I will open source this setup (together with Spring 5, Hibernate 5 and flyway) when I get this up and ready. Thanks in advance.
You need to include the result of the frontend (ui) buildAngular task inside the jar generated in the backend (core) build:
bootJar {
dependsOn ':ui:buildAngular'
into('BOOT-INF/classes/static') {
from "${project(':ui').projectDir}/dist"
}
}
The fact that the bootJar task now depends on the buildAngular task of the frontend will make gradle order them as needed.
You can browse this project of mine to have an example using basically the same setup (except it uses yarn instead of npm to resolve dependencies)
Answer of JB Nizet should work. You can also add the following in the root gradle file:
build.dependsOn("core:build").mustRunAfter("ui:build")
Above answer is in my opinion cleaner.

sonarqube gradle plugin excluding jacoco integration tests

I'm trying to integrate the sonarqube gradle plugin with the jacoco plugin:
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.1'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'
My build/jacoco folder contains:
integrationTest.exec
test.exec
However, the sonarqube plugin only recognises the 'test.exec' file:
18:20:45.130 INFO - JaCoCoItSensor: JaCoCo IT report not found: C:\Users\abc3\Downloads\sme-letter\target\jacoco-it.exec
:
18:05:55.609 INFO - Analysing C:\Users\abc3\Downloads\sme-letter\build\jacoco\test.exec
How do I get the sonarqube plugin to recognise 'integrationTest.exec'
Thanks
Mark
I'm not really sure, whether this will work for Gradle plugun, but you may try.
Sonar has a property to specify the name of the integration tests JaCoCo report. This property is called sonar.jacoco.itReportPath (sonar.jacoco.reportPath for unit tests report).
And as far as I know, gradle sonar plugin let you add custom properties to it. So you can change IT report name via properties as follows:
sonarqube {
properties {
property "sonar.jacoco.itReportPath", "build/jacoco/ integrationTest.exec"
}
}
It works for me, but only after merging all jacoco reports into one file AND (important) deleting all other reports
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.junit.reportsPath", "$projectDir/build/test-results"
property 'sonar.jacoco.reportPaths', "${rootProject.buildDir}/jacoco/mergeJacoco.exec"
And you need a merge jacoco task
Task mergeJacoco = tasks.create('mergeJacoco', JacocoMerge, {
doFirst {
delete "$buildDir/jacoco/mergeJacoco.exec"
}
destinationFile(new File(buildDir, 'jacoco/mergeJacoco.exec'))
executionData fileTree('./').include('**/*.exec-partial')
doLast {
delete fileTree('./').include('**/test.exec-partial')
}
})
mergeJacoco.outputs.upToDateWhen { false }
project.tasks["sonarqube"].dependsOn mergeJacoco
mergeJacoco.mustRunAfter ":myProject:test"
And setup jacoco to use those "partial" files
subprojects {
....
jacoco {
destinationFile file("$buildDir/jacoco/test.exec-partial")
append = false
includes = ['com.mypackage.*']
}
}
You'll get unit and IT reports mixed in one, but that's the best I could get

Gradle - how to exclude Findbugs on /src/test/java

Is there a way to exclude Findbugs execution on classes under /src/test/java. I tried the following but it doesn't seem to work.
classes = classes.filter {
!it.path.contains("**classes\\test\\org*")
}
Sure. The documentation of the Findbugs extension says:
sourceSets : The source sets to be analyzed as part of the check and build tasks.
And the example just above shows an example doing exactly what you want:
apply plugin: "findbugs"
findbugs {
sourceSets = [sourceSets.main]
}
i.e. only analyze the main sourceSet, and not the test sourceSet.
For Gradle 4.5.1
apply plugin: 'findbugs'
findbugs {
findbugsTest.enabled = false
}
It isn't mentioned anywhere in documentation, at least I, as 1 day gradle user, don't find it, but it works.

Resources