0

EDIT: I have changed this question so that it is not simply a duplicate of Can maven projects have multiple parents? (the answers for which seems to assert that a module can't have 2 parents)


Q: Maven: can a child module of a multi-module parent project be used in another completely separate project ?

Consider a ChildCommon project that is an API module or common implementation library to be shared by many projects.

There are at least 2 possibilities:

Case1: If 2 parents are allowed (with Maven 3.3.3):

ParentA
|--ChildCommon

ParentB
|--ChildCommon

Case2: Instead of 2 parents, a completely separate project depends directly on the child module:

ParentA
|--ChildCommon

ProjectB dependsOn ChildCommon [EDIT: ANSWER IS, YES, THIS WAY WORKS]

And if so, if part of the Maven build spec for the ChildCommon module has been siphoned off (up) into ParentA, how does one ensure the correct Maven spec is used when it is used by ParentB or ProjectB (without having to repeat things from ParentA brute force) ?

[EDIT: Answer is that Maven handles that perfectly via the "externally" referenced child's parent POM.]


EDIT: Expanding: My aim is to have my POM Optimizing and Refactoring cake using a parent POM and eat it. I have a set of 6 core modules that work together to nicely achieve the modularisation I need to develop a core web app system:

CoreLib, CoreAll, CoreImp, CoreAPI, CoreEJB, CoreWeb

CoreWeb runs as a standalone web app, and can also be reused by other web apps using overlays.

I won't go into here why I need 6, but it's in fact elegant for my case. It is however wearisome having to sometimes clean and build each one. It is also error-prone repeating certain artifact versions in each pom.xml (my main concern).

A specific web app combo leverages (at least some of) the above core modules (including CoreWeb):

SpecAPI, SpecEJB, SpecWeb

But another applications (a non-web one) might leverage only a subset of the core ones such as CoreAll, CoreImp, and would not under any circumstances want coupling to, for example, CoreWeb.

Community
  • 1
  • 1
  • answer to the question is YES. could you better exemplify the latter part of your query. – Naman Dec 01 '16 at 11:46
  • @nullpointer The answers to this posting http://stackoverflow.com/questions/1636801/can-maven-projects-have-multiple-parents seem to assert that one can't have 2 parents, although elsewhere I read that it is planned to support it in future Maven. I am on Maven 3.3.3. Another option for me is to have a separate project that can at least depend on (target) specifically a ChildCommon module (without the entire set of siblings within its one parent). – Webel IT Australia - upvoter Dec 01 '16 at 12:09
  • for the case1, I highly doubt that this shall be implemented. What use case do you seek out of creating a module as a child of two parents? – Naman Dec 01 '16 at 12:17
  • @nullpointer 'What use case do you seek out of creating a module as a child of two parents? ' (or being able to at least depend on a child module from a separate proejct). Please see detailed explanation in edited answer. – Webel IT Australia - upvoter Dec 01 '16 at 12:22
  • 1
    why not simply use individual child modules as a dependency in any other project that you might require it in and those that you want to put in for Specx – Naman Dec 01 '16 at 12:27
  • @nullpointer Will try simply depending on "common/shared" child from another project; am concerned (maybe overly anxious) that this will trigger building beyond the targeted child via its multi-module parent. Have Git, so can run away easily if it fails. – Webel IT Australia - upvoter Dec 01 '16 at 12:32
  • @nullpointer BTW 'nullpointer' is an "exceptional" username. – Webel IT Australia - upvoter Dec 01 '16 at 12:33
  • 1
    ` that this will trigger building beyond the targeted child via its multi-module parent.` I doubt that. It wouldn't. Every module is build separately independent of its parent. It only depends on the `` defined within its own pom.xml. – Naman Dec 01 '16 at 12:35
  • 1
    @nullpointer Using `ProjectB dependsOn ChildCommon` (with ProjectB not a 2nd parent of ChildCommon) works at least when ChildCommon has a single parent POM. I did not try 2 parents, but I don't have to. Once the parent POM is loaded, I can even re-build the various child modules that ProjectB depends on individually. *If you restate this as an answer I will accept it immediately (give you some stack points).* – Webel IT Australia - upvoter Dec 01 '16 at 14:08
  • If the problem is solved for you, I guess that must already be existing in a lot of places in SO itself. would merely be a duplicate. Can just close this in a point you get your problem solved. – Naman Dec 01 '16 at 14:21

1 Answers1

0

If you have a child project/module ChildCommon with a parent POM project:

ParentA
|--ChildCommon

You MAY reference ChildCommon to reuse it in another project ProjectB without adverse side-effects such as cascading to all other children of ParentA.