Running a Maven goal on a child module via its parent

We're currently using a master Maven project with pom packaging to wrap various child projects, including libraries and applications of various types (especially Jersey servlets and standalone client applications).  The child projects are also defined using Maven pom files.  IntelliJ IDEA is proving quite good at working on top of this Maven-based project layout - it is somewhat ugly that it creates its own 'redundant' representations on disk, but at least that process is a silent one, and IDEA doesn't mind much if we don't put those files in source control.  All in all it seems a great way to get the power of the IDE features while keeping the project structure in a form that's not tied to the IDE.  It's also great to be able to use IDEA to add new child modules, using archetyps or not - it does a great job of updating the parent, adding backreferences from the children, and so on.

However, we have a problem when we want to trigger a Maven goal on a child module which depends on other child modules.  I am new to Maven, so could have the wrong end of the stick, but it seems to be the case that executing the goal directly on the child module is not the right thing to do - Maven will not automatically build dependencies first.  The right thing to do is execute the goal on the parent module, but use the "-pl" switch to tell it you really only want to run the goal on the child.  Then you also add the "-am" switch, and it will build the dependencies first, as you'd expect.

With IDEA, we can set that up fairly easily (if rather crudely) in a custom run configuration, by pointing it at the parent module and specifying something like "compile -pl ChildLibrary -am" as the 'goals' field.  They're not actually all goals, but they go onto the command line and Maven does the right thing.

However I don't want to set up a lot of custom run configurations to do this, especially as those are local workspace details that are not shared with other team members via source control.  I'd much rather be able to pick and choose lifecycle goals from the "Maven Projects" window, which already provides the ability to run a Maven goal directly, but doesn't know that it ought to run child modules' goals via the parent, using "-pl" and "-am".

Being new to Maven, I don't know whether this is always the right thing to do when executing a goal on a child module, or whether it's sometimes preferable to run the goal directly.  It certainly seems to be a better default, though, and perhaps there should be a configuration setting to choose the behaviour, and a context menu offering both options.

On the other hand, maybe my whole approach here is wrong.  We are at an early stage and I would be interested to hear alternatives for organizing this kind of project tree, where several related applications depend upon actively-developed libraries, and developers often need to build and run the various applications in isolation without rebuilding other applications and libraries which they're not interested in at the time.

Please sign in to leave a comment.