user2393012 user2393012 - 3 months ago 19
Java Question

Why Maven doesn't install dependencies in multimodule project?

My project structure (multi module) is like this

parent
projectA
projectB
... other modules


parent
also actually has a parent (Spring Boot).

I have set up a Jenkins jobs to compile & test on every commit, so it runs:

mvn -f pom.xml clean install


And that all works fine.
ProjectB
depends on
ProjectA
(which is like a common classes type of project) and is a Spring boot application. So the dependency information is the regular:

<dependency>
<groupId>Group</groupId>
<artifactId>ProjectA</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>


ProjectB
has a separate job in Jenkins to build the deployable jar file and deploy it to server. So the command there is:

mvn -f ProjectB/pom.xml clean install antrun:run


This fails with a message like:

[WARNING] The POM for Group:ProjectB:1.0-SNAPSHOT is missing, no dependency information available
...
[ERROR] Failed to execute goal on project host-api: Could not resolve dependencies for project Group:ProjectB:1.0-SNAPSHOT: The following artifacts could not be resolved: Group:ProjectA:jar:1.0-SNAPSHOT...


Now I can resolve this by doing a
mvn install
in the
ProjectA
directory - I've tested this and it does resolve the issue.

But my question is why should I have to? Shouldn't Maven figure out it should be installing the jar in the local repo?

Thanks in advance

Answer

TL;DR Tell maven about the structure of your project.

When you run the command

mvn -f pom.xml clean install

Then maven uses the reactor to work out the order of the modules, something like the following is output:

[INFO] Reactor build order:
[INFO] ProjectA
[INFO] ProjectB

So Maven first builds project A, then builds project B.

When you run the command:

mvn -f ProjectB/pom.xml clean install antrun:run

Then you have a problem; maven isn't starting from the parent - it's starting from a child. It's not told about the hierarchy of projects needed to be built first.

If you want to build a single project from a maven multimodule project, along with dependencies you should use:

mvn -f pom.xml -pl ProjectB -am install antrun:run

Where:

  • -pl ProjectB is the "project list" option, it tells maven to build these specific projects
  • -am is the "also make" option, it tells maven to build any projects that the projects in pl are dependant on
Comments