views:

1248

answers:

5

I have a multi-module Maven project with a parent project P and three sub-modules A, B, and C. Both B and C are war projects and both depend on A. I can type "mvn compile" in P and have all of the sub-modules properly compiled. The problem comes when I want to do operations for specific modules.

I'd like to be able to package a war for project B, but when I run the package command from B's directory, it complains that it can't find the dependencies for A. I understand from this question (http://stackoverflow.com/questions/808516/maven-and-dependent-modules) that perhaps Maven isn't really designed for this type of dependency resolution, but that begs the question of how do I package B? Do I have to run "mvn package" for the entire project hierarchy when I really just want B? Do I have to install snapshots of A into my local repository every time I want to package B? This second scenario isn't much fun when A is still under active development.

Any best practices here?

+2  A: 

If you have previously run mvn install on project B it will have been installed to your local repository, so when you build package A Maven can resolve the dependency. So as long as you install project B each time you change it your builds for project A will be up to date.

You can define a multi-module project with an aggregator pom to build a set of projects.

It's also worthwhile mentioning m2eclipse, it integrates Maven into Eclipse and allows you to (optionally) resolve dependencies from the workspace. So if you are hacking away on multiple projects, the workspace content will be used for compilation. Once you are happy with your changes, run mvn install (on each project in turn, or using an aggregator) to put them in your local repository.

Rich Seller
Is there an easy way to guarantee that all the dependent projects for my target project have been installed? The hierarchy I have is actually much more complex than the one described here, so try to remember if I've run mvn install for each dependent project since I've last made code changes could be tricky.
Brian Ferris
Yes, using modules to build the set of projects, see the link added to the original answer.
Rich Seller
+1  A: 

Maven absolutely was designed for this type of dependency.

mvn package won't install anything in your local repository it just packages the project and leaves it in the target folder.

Do mvn install in parent project (A), with this all the sub-modules will be installed in your computer's Maven repository, if there are no changes you just need to compile/package the sub-module (B) and Maven will take the already packaged and installed dependencies just right.

You just need to a mvn install in the parent project if you updated some portion of the code.

victor hugo
A: 

You say you "really just want B", but this is false. You want B, but you also want an updated A if there have been any changes to it ("active development").

So, sometimes you want to work with A, B, and C. For this case you have aggregator project P. For the case where you want to work with A and B (but do not want C), you should create aggregator project Q.

Zac Thompson
+2  A: 

Take a look at my answer here. The Maven Reactor plugin is designed to deal with building part of a project.

The particular goal you'll want to use it reactor:make.

deterb
A: 

Any best practices here?

Use the Maven advanced reactor options, more specifically:

-pl, --projects
        Build specified reactor projects instead of all projects
-am, --also-make
        If project list is specified, also build projects required by the list

So just cd into the parent P directory and run:

mvn install -pl B -am

And this will build B and the modules required by B.

Pascal Thivent