Search code examples
maven-2m2eclipsemaven-3

Picking up changes in multi-module Maven project


I have a multi-module Maven project where module B depends on module A. How do I get module A to be rebuilt whenever I rebuild module B? Right now I have to manually install module A before doing anything with module B and it's a real pain.

P.S. I see someone else asked a similar unanswered question: Maven: How to use jetty:run in a multi-module Maven project, without needing to install


Solution

  • This is solved in two ways. First, the POM for B needs to include the <dependency> for A. As long as A is in the repository when B is built, it will get the correct version.

    To make sure A gets built before B, the Maven reactor needs to know about this dependency. This is done in a multimodule build with <module> elements. The top-level POM is set for <packaging>pom</packaging> and it would have two <module> elements, one for A and one for B. It doesn't matter what order they are listed or how deep, if they are reachable from the source project, they will be built in the right order.

    Note that there is no way to try building B and have the Maven reactor go find the source for A and check it. The reactor always needs a source project, and both A and B must be found through the graph of <module> elements. This is because the build for B cannot tell if the repository artifact for A is up-to-date, it must run A's build and let it figure it out. And for them to do so at the same time, a parent project that includes both as described here must be the source project that gets built.

    Also note that while Maven is no slower than Ant for multi-module projects of this sort, importing your Maven project into an IDE will generally result in far faster builds than Maven can perform itself.