2

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

Community
  • 1
  • 1
Ben McCann
  • 16,431
  • 22
  • 74
  • 96

2 Answers2

1

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.

Anonymoose
  • 4,882
  • 4
  • 29
  • 40
Brian Topping
  • 3,049
  • 25
  • 32
  • My pom.xml for project B does list project A as a dependency. The problem is that it is using the installed project A and not the project A on the file system. This makes development a pain. Basically what I'd like is for project A to be automatically reinstalled beforehand every time project B is recompiled. – Ben McCann Dec 07 '10 at 03:40
  • Then you need to make a parent POM with both of these projects and build from there. Or pull it into an IDE. – Brian Topping Dec 07 '10 at 04:00
  • For others that might come across this question, the answer basically is that it's impossible. Brian suggested on the other thread that using Maven for development is just too painful and instead it should be used only to package a final build. http://stackoverflow.com/questions/4371373 – Ben McCann Dec 07 '10 at 23:51
  • Well, not quite. Using Maven for *iterative* development is far more painful than using an IDE which understands Maven metadata. There's a huge difference! – Brian Topping Dec 09 '10 at 22:04
0

Working in the Eclipse IDE with m2eclipse, you can enable workspace dependency management for compile time, as well as in Maven launch configuration. In this case, compiled classes from dependent project will be used instead of the jar from the Maven local repository. That works for a regular projects without customized jar packaging.

Naturally, in the command line, you either build and install each project, or use a reactor project to build both projects in one run, but that is significantly less convenient then working in the IDE.

Eugene Kuleshov
  • 30,122
  • 5
  • 61
  • 63
  • I have "Resolve dependencies from Workspace projects" turned on, which is the default. However, when I open up my project B pom.xml in Eclipse and go to the "Dependency Hierarchy" tab, I see that it's pulling in transitive dependencies from project A based off the installed project A and not the project A in my workspace. – Ben McCann Dec 07 '10 at 03:36