divesh premdeep
divesh premdeep

Reputation: 1070

Independently building Maven submodules

After being introduced to Maven in my most recent project, I've been experimenting with it for the past couple of weeks.

I currently have a multi-module project in my development environment. Project "A" and Project "B" are child modules of Project "root", with B having a dependency on A.

I am aware that I can build B independently by using mvn reactor:make... as outlined here. However, I'm curious as to why am I not allowed to build B from inside B's root folder.

To illustrate this, I first built A independently by running a clean install from within A's root directory. However, when I tried doing the same action from B's root directory, Maven reported an error as below -

Could not find artifact org.divesh.mavenrnd:root:pom:1.0 in central

It looks like Maven is not able to resolve the parent's POM file. Any thoughts on why this is happening? From my initial understanding of Maven multi-module projects, the main reason to split a project into sub modules is to share dependencies. This should not, however, prevent me from building a particular module independently from within its folder.

Thanks.

EDIT

Ran an mvn -N clean install to install only the root project's POM in the rep. After this, I was able to successfully build B after building and installing A. There is still one thing I'm not quite clear about - Assuming I've installed the root project's POM in the repository, if I try to build A, does it refer to the parent root POM directly above it or the POM that is installed in the repository?

Upvotes: 0

Views: 1033

Answers (2)

khmarbaise
khmarbaise

Reputation: 97547

You should use mvn -pl ProjectToBuild lifecycle like from the root of your tree:

mvn -pl module-b package 

You shouldn't use mvn reactor:make anymore. Except you are using maven 2.0

If you wan't to be sure that everything is depending on module-b is build as well you should use:

mvn -amd -pl module -b package

The best is having a folder layout which represents the appropriate structure of your project and not using relativePath is necessary.

Upvotes: 1

Andrew Logvinov
Andrew Logvinov

Reputation: 21851

That's right. The error you mentioned tells you that maven cannot find parent's pom.

You have 2 options here:

  1. First call mvn -N install in your root project directory to install parent pom to your local repository. Then you can go to B's directory and build the submodule.
  2. Specify <relativePath> in your submodule pom. By default maven will look for parent pom only one level up the file system. If you have other directory structure in your project, you have to specify this option in order for maven to locate your parent pom.

Here's a sample:

<parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    <relativePath>../../pom.xml</relativePath>
</parent>

Upvotes: 3

Related Questions