Sonar Report + Multi module maven + Jacoco plugin - sonarqube

I am new to Sonar.
I have multi-module maven project.
please find the Project structure below
-Parent
| -Module A
|
-pom.xml (Module A)
| -Module B
|
-pom.xml (Module B)
| -Module C
| |
| -pom.xml (Module C)
|
--pom.xml (parent pom)
Note : (Module C is shared module in Module A/ Module B - means Internally module C will the part of into Module A , Module B library)
Based on the Project requirement we have these structure.
Module A - works for external users.
Module B - works for internalusers.
Module C -common b/w both the module.
I am trying to create single sonar report for both the module, but I am not able to integrate all the module junit report (Module A and Module B report)
into single report.I followed couple of example to combine the muti-module maven porject but nothing works.
similar issue1 similar issue2
github-example (reference given in sonar)

I had similiar problem with over 40 modules which were even nested. What you have to do is to create whole sonar configuration in parent pom.
<sonar.host.url>set url here (default is localhost)</sonar.host.url>
<sonar.login>user for host url (default admin)</sonar.login>
<sonar.password>password for host url (default is admin)</sonar.password>
<sonar.projectName>optional name for whole project in sonar view</sonar.projectName>
<sonar.projectDescription>optional project description for sonar view</sonar.projectDescription>
<sonar.projectBaseDir>like name says you can set project base dir, if you have parent pom as a separate module then you can type ".." to set main directory with all modules</sonar.projectBaseDir>
Set properly all modules which will be analysed separately for the whole project:
<sonar.modules>module1, module2, module3</sonar.modules>
Configure each one of them properly:
<module1.sonar.projectName>module1</module1.sonar.projectName>
<module1.sonar.projectBaseDir>module1/</module1.sonar.projectBaseDir>
<module1.sonar.sources>optionally set sources to proper directory for example src/main/java</module1.sonar.sources>
<!-- similiar for other 2 projects -->
That way all junit reports will be used per module but it will be listed in single project with modules. That way you will be able to see reports per module and per whole single project.
It's a good practise to check results after adding every single module. In case of failure simply check error in console and fix the problem.

I am able to achieve Integration test coverage using sonar with Jococo plugin.
To run the Sonar in local, i was facing the issue with SCM error in sonar.
Every time it was failing in the in sonar report creation.
For resolving the issue in local you need to disable the SCM configuration in sonar.
login in local sonar as Admin - admin/admin (default username/password)
Now under setting we have SCM tab - disable the SCM Senor and save the SCM Setting.
Now in Dashboard --> Configure widgets .Search "Integration Tests Coverage"
Now add widget into your Project Dashboard.
Follow the same configuration in your pom.xml as given in the link.
https://github.com/SonarSource/sonar-examples/tree/master/projects/languages/java/code-coverage/combined%20ut-it/combined-ut-it-multimodule-maven-jacoco

Related

How to list all usages of a gradle module

I have a project with multiple modules (gradle modules) and some are depend on some others, for example :modules:backend:core has a project dependency on :modules:libraries:util:core and some others.
In my gitlab CI job I am able to tell when there are changes within some module (e.g. :modules:libraries:util:core) by listening to something like modules/libraries/util/core/**/*, and then triggering a build of that changed module.
Now the issue I have is how to figure out where this module is used, so that I can build the other side also (in this example I would need to build :modules:backend:core once :modules:libraries:util:core is changed).
Is there some way to list all usages of given module ?
https://github.com/vanniktech/gradle-dependency-graph-generator-plugin
You can use this plugin to create "your project module dependency graph"
./gradlew generateProjectDependencyGraph
or "whole dependency graph".
./gradlew generateDependencyGraph
You can find this file from app/build/reports/dependency-graph and app/build/reports/project-dependency-graph directory.
The folder includes three files: png, svg and dot.
In the dot file, you can get the module dependency.
":app" -> ":base" ["style"="dotted"]
":app" -> ":moduleA" ["style"="dotted"]
":moduleA" -> ":base" ["style"="dotted"]

How to setup multi-project IntelliJ workspace with Gradle?

Hello I am working on using Gradle to build a few services. I tend to have seperate repositories for each "code package" i.e. library or service.
In IntelliJ, my setup tends to be the following:
- IntelliJ Workspace
-- Service A
--- pom.xml
-- Service B
--- pom.xml
-- Library A
--- pom.xml
-- Library B
--- pom.xml
Service A & B both depend on Library A & B. This works great, within a single IntelliJ Workspace I can modify Library A and B and startup the services and all is well.
I know you can do composite builds and multi-module builds with Gradle but like I said all the 4 modules above are seperate repositories so this won't work well.
Will IntelliJ support a similar setup as above with Gradle where it recongizes that Library A & B is the library that Service A & B depend on? Or does everyone do a manual publish to Maven local for this consistently?
Thanks!
Rephrasing your question -
there are 3 separate gradle projects service1, lib1, lib2
service1 depends on lib1 and lib2
what is expected is
for local development - where source for all 3 projects locally exists - use the local code build
refer to the repository artifacts if local source does not exist
Assuming a simple folder structure like this.
app-code
- service1
- lib1
- lib2
service1 - settings.gradle
rootProject.name = 'service1'
include ':lib1'
project(':lib1').projectDir = new File('../lib1')
include ':lib2'
project(':lib2').projectDir = new File('../lib2')
service1 - build.gradle
dependencies {
implementation (project(':lib1').projectDir.exists() ? project(':lib1') : 'com.lib1:lib1:1.0.0')
implementation (project(':lib2').projectDir.exists() ? project(':lib2') : 'com.lib2:lib2:1.0.0')
}
how intellij import looks

Gradle specify settings dir path for a sub project

In a multi-project build I have a module that in itself is composed of two sub-projects. If I just want the option of building the top-level module but also ensure both the sub-projects within it are also built, how I do achieve this?
include 'moduleA', 'moduleB', 'moduleC' (root project settings.gradle)
project(':moduleC').projectDir = new File('path to custom module that includes sub-projects)
project(':moduleC').settingsDir = ?? (gradle fails because there is no settingsDir path)
but moduleC has a settings.gradle in itself that has
include 'api'
include 'server'
Now I want both these to be triggered when I specify gradlew :moduleC:build, but instead it just builds moduleC root project. Is there a way? This use case does seem valid to me (i.e. for modularity, you want to keep the inclusion of sub-projects at moduleC's level and not at root level).
Thanks,
Paddy
As of Gradle 2.2, only a single settings.gradle per build is supported. If that file contains include "moduleC:api" and include "moduleC:server", then running gradle build from moduleC's project directory will also build api and server.

How to generate the code coverage report using jacoco where source code and tests are in different projects?

I have a maven java project in some folder (it has some unit tests), and tests for the same code in another different project (different directory). Both source code and test share the same parent pom. Now I want to generate the code coverage report using JaCoCo.
How to instrument sources? How to run tests on instrumented code? And how to integrate and get the result report?
Say Project ABC contains the code and project XYZ contains the test cases.
Note:
Project ABC and Project XYZ are independent projects
Both ABC and XYZ contains multiple sub projects(Need to integrate everything).
I had a similar problem. I found a solution by changing the path of the jacoco report path:
<sonar.surefire.reportsPath>${project.basedir}/../target/surefire-reports</sonar.surefire.reportsPath>
<sonar.jacoco.reportPath>${project.basedir}/../target/jacoco.exec</sonar.jacoco.reportPath>
I added these properties and configured the jacoco plugin to append reports and not overwrite them by:
<configuration>
<append>true</append>
</configuration>
This way jacoco write the reports to the base directory of the multi module project. The sonar plugin finds these and analyses them.
I had a problem when building a "reference application" with multiple sub-modules and trying to generate test coverage for the sub-modules and have them push to sonarqube. The issue I was having was that since the sub-modules referenced each other, the resulting jacoco reports were getting overwritten and I'd end up with results for a single module. This may not be the same issue as posted above, but I did solve it by using "append" in gradle, so just want to show how to do that.
In the build.gradle file for each module, I have:
testOptions {
unitTests.returnDefaultValues = true
unitTests.includeAndroidResources = true
unitTests.all {
jacoco {
append = true
includeNoLocationClasses = true
}
systemProperty 'robolectric.enabledSdks', '28'
}
}
s

project name in gradle must be a directory?

Is it mandatory to have a directory with same name as project in gradle ? My code is like this:
.
|-- build.gradle
`-- suites
`-- lrgabc.gradle
1 directory, 2 files
content of suites/lrgabc.gradle
$ cat suites/lrgabc.gradle
project(':lrgabc') {
task block1 << {
println "Hello from lrgabc.block1"
}
}
Content of build.gradle
apply from: 'suites/lrgabc.gradle'
When I run, gradle -q :lrgabc:block1 I get
* Where:
Script '/home/skgupta/gradle-examples/multiproject/suites/lrgabc.gradle' line: 1
* What went wrong:
A problem occurred evaluating script.
> Project with path ':lrgabc' could not be found in root project 'multiproject'.
Is it mandatory to have 'lrgabc' as a directory under 'multiproject' ?
What I'm trying is under one single project, create multiple test suites using gradle. Each test suite is one gradle file, where each task in that gradle represent one test [These are non java tests, like perl, python..etc]
How do I solve this ?
Each subproject in a multi-project Gradle build needs to have a separate project directory. However, directory names do not have to correspond to project names (this is configurable in settings.gradle).
You really want to take a look at the documentation for multi-project builds - http://www.gradle.org/docs/current/userguide/multi_project_builds.html There are also samples in Gradle distribution that can help you.
You don't need to create new projects to create more test tasks. You can have these tests in one project. It is quite common to define multiple test suite like unit and functional tests this way. http://blog.safaribooksonline.com/2013/08/22/gradle-test-organization/ shows a possible implementation.

Resources