What maven goal is required to run maven sonar analysis? - sonarqube

I have seen sonar builds failing if I run mvn package or mvn verify as build goals, however if I change it to mvn install it passes.
Can you explain why maven install goal is needed for sonar to work properly?

In a multi-module build an aggregator plugin can't resolve dependencies from target folder. So you have two options:
mvn clean install && mvn sonar:sonar as two separate processes
mvn clean package sonar:sonar as a single reactor

Related

Run test phase in maven while skipping validate & compile

Working with a large multi-module maven project. Want to run phases as follows
mvn clean install -DskipTests
Then run unit tests. But this fails, even though code is already compiled
mvn surefire:test

Execute maven install phase without executing maven compile

Working with a multi-module project. Want to run maven commands as follows:
mvn clean compile
Then maven install phase without again executing maven compile
Not possible.
You would need to call the goals directly, phases cannot be run separately.
you can (now) skip phases by directly calling the goal via
mvn <plugin>:<goal>
e.g.
mvn compiler:compile
see this answer for details.
for install it should be mvn install:install

Sonarqube analysis for selected modules

I have a multimodules project with many dependencies. Until now after successful build I was performing sonar analysis of whole project. To save time and build project I detect which modules have been changed and run maven command:
mvn install -pl module1,module5,module2 -amd
Is there any way to do static analysis only for built modules? Something like mvn sonar:sonar -pl module1,module5,module2 -amd? of course it doesn't work, do you have any idea?
I know that there is option:
mvn sonar:sonar -pl !module2
but my project has more than 50 modules so if commit change only 5 modules I will have to list 45 others modules.
SonarQube always requires full scan. If you exclude some modules and execute the scanner, then excluded modules will disappear from SonarQube. It means there is no such flag.
Read more: How to analyse only new added lines of code?

Running maven multi-module failsafe tasks independently from mvn clean package

I have a multi-module project a. Sub-module x includes an a simple integration test which requires also a dependency on sub-module y.
I would like to be able to separate the compilation and package phase from running the tests. When I run the following command, the integration test run successfully
mvn clean verify
When I run the following command, it fails
mvn clean package && mvn failsafe:integration-test failsafe:verify
[ERROR] Failed to execute goal on project x: Could not resolve dependencies for project a:x:jar:1.0-SNAPSHOT: Could not find artifact a:y:jar:1.0-SNAPSHOT -> [Help 1]
The underlying reason is that I would like to run the unit-tests and various integration tests each in separate jenkins tasks after the compilation completes (without running compile and package phase again). Reproducible code is here https://github.com/itaifrenkel/failsafe-test. Using Maven version 3.2.1.
Clarification: I cannot mvn install on jenkins machine since I have concurrent builds of different git versions (that have the same maven version).
When you execute mvn clean verify, the build succeeds: Maven resolves the y dependency because it is in the same project reactor and y was packaged successfully into a jar inside this reactor. If you take a look at the log, you will notice that this command triggered the maven-jar-plugin, which is expected since this plugin is bound to the package phase and verify phase comes after it in the build lifecycle.
The command mvn clean package && mvn failsafe:integration-test failsafe:verify actually executes 2 commands. First, mvn clean package will succeed and package the application (same reason as before: y is in the reactor and is packaged into a jar).
However, for the second build, mvn failsafe:integration-test failsafe:verify, since y was not packaged into a jar inside the reactor, Maven can't resolve the dependency directly so it needs to look for it in the repository. Since this artifact was never installed in the repository (and is obviously not available in Maven Central), the dependency can't be resolved, hence the error message
As such, you have 2 possible solutions:
Install the y dependency into the local repository with mvn install:
mvn clean install && mvn failsafe:integration-test failsafe:verify
Running jar:jar before the integration tests so that Maven can resolve the y dependency. This does not rebuild the project: it makes the assumption that the project was already built before by simply asking maven-jar-plugin to make a jar out of the result of the previous build.
mvn clean package && mvn jar:jar failsafe:integration-test failsafe:verify

Ignore test cases while running maven project with sonar

I have installed sonar and trying to analyze maven based application using following command :
clean install -Dmaven.test.skip=true -Psonar sonar:sonar
still its executing test-cases.
What if I doesn't want the analysis of unit test cases?
Found the solution, added -DskipTests=true with maven command.
The full command which works for me is :
mvn clean install -DskipTests=true -Dmaven.test.failure.ignore=true sonar:sonar
-Dsonar.database=mysql
-Dsonar.jdbc.driver=com.mysql.jdbc.Driver -Dsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8
mvn sonar:sonar -Dmaven.test.skip=true
is also an option, but make sure that your unit tests ran before to have results for unit test coverage in sonar

Resources