SonarQube 3.5.1 - how to exclude directory from analysis - sonarqube

I have my project code structure something like
module1
submodule1
submodule2
module2
submodule1
submodule2
module3
submodule1
submodule2
pom.xml
If I want to exlude the full directory module1 from sonar analysis, how to do that?
I tried multiple options like skiModules, exlusions, could not succeed.

Currently, the best option is to define a profile (in your POM) in which you redefine the list of modules that you really want to analyse (so in your example, you won't list module1). Then, when running a SonarQube analysis, you have to activate this profile:
mvn sonar:sonar -PsonarQubeAnalysisProfile
Note that we should soon implement a feature that will allow you to set a property "sonar.skip" in the POM of the modules that you want to exclude in order to make all this simpler. Please watch and vote for the following JIRA ticket: MSONAR-91

In your sonar-project.properties file add the following (to exclude multiple directories use comma separated folder paths)
sonar.exclusions=module1/**, module2/**
And while running the sonar runner I got the following in the log
Excluded sources:
module1/**
module2/**

Related

How to get a failure file/excludeFromFailureFile argument from classpath for maven-pmd-plugin?

The maven-pmd-plugin allows to get ruleset files from the local filesystem (with an absolute path only, which makes it unflexible to use) and from classpath which practically means for a multi-module project that rulesets are put into a separate project which is build by a Maven parent or aggregator to that it's available for its siblings.
How to achieve that for failures files which are specified in the excludeFromFailureFile parameter of the plugin configuration in the POM? Putting them next to the rulesets which are recognized doesn't work, the PMD Maven plugin article on multi-module projects doesn't contain information (why?) and google appears to redirect to rulesets on classpath issues systematically.
Loading from classpath allows to be able to specify the resource once in form of a dependency and to be able to build at every level of a multi- and or hierarchical project structure - consider the following example:
aggregator:
- pom.xml
- parent:
- pom.xml (maven-pmd-plugin here)
- module1:
- pom.xml
- module2:
- pom.xml
where I can validate and build the aggregator (which will run on a CI service), the parent and single modules with one specification. Using basedir and parent.basedir doesn't allow that.
It's possible to put the failure file in a git or other SCM submodule, checkout the file during each project root which one wants to be able to build (parent project and eventual aggregators of the parent) and synchronize them manually at every change. That's not classpath resolution and complicated.
I'm using maven-pmd-plugin 3.8.

Excluding directories from Sonar Qube scan in a multi module project in Jenkins without modifications to project

I have created a Jenkins job which performs a SONAR QUBE analysis.
I have added the SONAR scan as a generic build step.
I have not made any modifications to the project to enable the SONAR integration since the project should not have to care about SONAR. (i.e. no changes to POM.xml and no properties file(s) added)
My SONAR integration is working, without any mods necessary to the project, which is great.
However, it is a multi module project and now I want to exclude one of the modules from the scan, since this module contains auto generated code.
Here are the Maven commands that I’m using to trigger the SONAR scan in Jenkins:
sonar:sonar
-Dsonar:host.url=http://url.com:9000/sonar
-Dsonar.login=myusername
-Dsonar.password=mypassword
Here is my project structure:
root
Project A
Project B
Project C
I want to exclude ProjectB from the scan.
I have tried -Dsonar.exclusions=/ProjectB and it did not work.
Here is an excerpt from the log:
[INFO] ------------- Scan Project B
[INFO] Base dir: /workspace/jenkins/…/workspace/CI-JOB-NAME/projectB
[INFO] Working dir: /workspace/jenkins/…/workspace/CI-JOB-NAME/projectB/target/sonar
[INFO] Source paths: pom.xml, src/main/java
[INFO] Binary dirs: target/classes
[INFO] Source encoding: UTF-8, default locale: en_US
[INFO] Index files
[INFO] Excluded sources:
[INFO] /projectB
[INFO] 44 files indexed
[INFO] 0 files ignored because of inclusion/exclusion patterns
As shown in the log, the excluded sources property is set but not working since it says 0 files ignored...
Question: How do I exclude everything inside the project B directory from the scan?
(Preferably using a command in the Jenkins job and not adding any properties files or other modifications to the project itself)
Is the “Excluded sources path” relative to the “Base dir” path or the Root??
Should I define the exclusion path as an absolute path?
The documentation has a dedicated section for this.
The options that seem suitable for your case:
use build profiles to exclude some module (like for integration tests)
use Advanced Reactor Options (such as -pl). For example mvn sonar:sonar -pl !module2
For example using this last one, you could do:
sonar:sonar -pl "!projectB"
-Dsonar:host.url=http://url.com:9000/sonar
-Dsonar.login=myusername
-Dsonar.password=mypassword
Is the “Excluded sources path” relative to the “Base dir” path or the Root?? Should I define the exclusion path as an absolute path?
According to the documentation on exclusions,
patterns are relative to the base directory, so not absolute paths.
In any case, I don't think this option is useful in your case,
because in a multi-module maven project,
the files of a sub-module are indexed with the path relative to the module's base directory. So a pattern like projectB/** will not match anything, as projectB is not part of the paths used. If you have some unique package name in the module, let's say someuniquepackage, located at src/main/java/someuniquepackage, then the pattern src/main/java/someuniquepackage/** would work.
In anycase, I recommend the -pl option above, or using Maven's profile feature.

Maven surefile plugin use

Can any one please tell me the use of adding plugins in POM.xml file in Maven project. I tried working with creating new maven project and run it. In the site which reffered suggesting to add those two plugins. Can anyone please tell me the use ? Are these plugins important for running maven test ?
The Surefire plugin is an implicit plugin that will automatically run your tests in a jar project, i.e. mvn test binds to the surefire:test goal. You don't need to add it to your POM unless you need to customize its configuration.
From the Surefire documentation:
By default, the Surefire Plugin will automatically include all test
classes with the following wildcard patterns:
"**/Test*.java" - includes all of its subdirectories and all Java
filenames that start with "Test". "**/*Test.java" - includes all of
its subdirectories and all Java filenames that end with "Test".
"**/*TestCase.java" - includes all of its subdirectories and all Java
filenames that end with "TestCase".
See this page for more details:
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bindings
http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html
http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

Maven surefire test results for a project with various modules

I've a parent pom project with various submodules. I want to get a report of the unit tests results but using surfire plugin, I get an independent result for each module.
I mean, if I execute:
mvn surefire-report:report
from the parent directory where the parent pom is located, it creates a surefire-report.html for each subproject but what I want, is only one html with the results of all the subprojectes.
Is there any way to achieve this automatically? Using surefire or some other plugin.
To create the aggregate report please try to use the following command at theparent project.
mvn surefire-report:report -Daggregate=true
I hope this may help.

maven build multiple rpms basing on multimodule project

I'm trying to set up maven to build my project in the specific way. I have the following structure:
pom.xml
module1
pom.xml
module2
pom.xml
module2.1
pom.xml
module2.2
pom.xml
module2.3
pom.xml
I actually want to build separate rpms based on module1, module2.2 and module2.3 and want all these rpms be included into root one. Could, please, anybody help me with that if there is any ways to do this using maven and it's plugin only. Also I want this to be done via profiles, if it's possible. All my tryings led me to nothing.
Thanks in advance!
It looks like you need the maven-assembly-plugin. You should use it and define <moduleSets/> in your assembly descriptor.

Resources