Corey
Corey

Reputation: 1059

How does Maven handle dependencies between modules?

According to the Maven lifecycle, mvn install will "install the package into the local repository, for use as a dependency in other projects locally". The local repository then stores all the jars that I downloaded remotely.

My modules have dependencies with other modules. When I run mvn package, nothing is stored in my local repository, but the dependencies appear to be fulfilled. So how does Maven handle the inter-module dependencies? Does Maven refer to the jars of each module from the built target directories or does it fetch them from another location?

Upvotes: 0

Views: 261

Answers (2)

333kenshin
333kenshin

Reputation: 2045

Corey,

You are correct, going strictly by Maven docs implies mvn compile on:

parent_pom/
  subA/
    pom.xml
  subB/
    pom.xml    # depends on subA

should fail since subA hasn't been pushed out to the local repo.

What's happening under the hood is that Maven uses the reactor to trick the build into looking into target dir of earlier submodules on the same build.

Beyond the scope of this particular question, the maven-reactor-plugin is one of the most opaque parts of Maven, but also one of the most powerful if you master it. You would do well to read up on it.

Hope that helps.

Upvotes: 1

Robert Scholte
Robert Scholte

Reputation: 12345

It depends on the phase you're executing. Before compile, Maven will fail, since there are no classes compiled. Between compile and package, the target/classes is used. For package and later, the target/artifactId-version.jar is used.

Upvotes: 1

Related Questions