Run test phase in maven while skipping validate & compile - maven

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

Related

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

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

What maven goal is required to run maven sonar analysis?

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

Do not run integration tests when running mvn clean install

I want the following to happen:
when I run mvn clean install , I want unit tests alone to run (no integration tests)
When I do mvn integration test , I want integration test alone to run (no unit tests)
when I do mvn test unit test alone should run.
I tried few things with Maven Surefire plugin and Maven failsafe plugin but could not
achieve this. What I have tried is: added Surefire and Failsafe plugins, tried separating the unit tests with annotations, as well separating unit and integration tests at package level, and keeping specific naming convention for unit and integration test.
What I am able to achieve is run unit tests alone on mvn clean install, but when I run Maven integration test I am not able to stop unit tests from running. Any idea how I should go about it?
The problem you have that you don't understand the Maven life-cycle which has the following phases (only excerpts):
clean
...
compile
...
test
..
pre-integration-test
integration-test
post-integration-test
..
install
This means in other words your requirement to do mvn clean install without running the integration tests is not satisfiable by the defaults (conventions).
I would suggest to create a profile where you put the maven-failsafe-plugin into so you can control if integration-tests will run or not. This results into a thing like this:
mvn -Prun-its install
to run the integration tests you can use the following:
mvn verify
which will include running the unit tests but this can be suppressed by using:
mvn -DskipTests=true verify
Using mvn test will run only the unit tests no integration tests cause it's earlier in life cycle.

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