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

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

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

Does the packaging action builds the project?

When we launch the maven command mvn package , does this command builds the Spring project at the same time ? Or does it just create the packaging file ?
Short answer - Yes!
Long answer:
Maven has a set of lifecycles (from https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html):
validate
compile
test
package
verify
install
deploy
Executing any given lifecycle will first execute the previous lifecycles - so when running mvn package it will first execute validate, then compile, then test, and then finally package.
If you run mvn package (or even just mvn compile) and check your target/classes directory you'll find your compiled .class files.

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

Could not resolve dependencies for project, except when called with package

I have an EAR project, defined very similarly as the reference project of WildFly. In addition to that, I have another JAR project JarProject
which is included in:
parent's pom.xml (as a <module> and in its <dependencyManagement>)
the EjbProject's pom.xml (with the scope provided)
the EarProject's pom.xml (with the default scope, compile)
The problem is that when executing mvn eclipse:eclipse or mvn wildfly:deploy (from the parent project) it fails with the error
[ERROR] Failed to execute goal on project EjbProject: Could not resolve dependencies for project groupId:EjbProject:jar:0.0.4-SNAPSHOT: Could not find artifact groupId:JarProject:jar:0.0.4-SNAPSHOT -> [Help 1]
It somehow looks the JarProject in the local repository, and does not see it in the parent project.
Very important note: mvn package works without problems, as mvn package wildfly:deploy or mvn package eclipse:eclipse do, but without pacakgeing before eclipse:eclipse, it fails.
Maven version: 3.0.4 and 3.3.3
I'm not sure I got the question. Hope this will help...
Goals "eclipse:eclipse" and "wildfly:deploy" do not trigger building of your projects. To use a project as a dependency it needs to be built from the parent pom or installed in you repository.
When you do mvn package eclipse:eclipse the package triggers the build of the JarProject.
If you want do be able to do only mvn eclipse:eclipse then you need to perform a mvn install on your JarProject first.

Resources