In a multi-module Maven project can the parent project have implemented java classes in it? If yes, then how do we use those classes in the child project (Since the parent project is packaged as pom)?
-
3Can it have source files? Yes. We can configure maven to use code from various sources (see, e.g., [this question](https://stackoverflow.com/questions/9752972/how-to-add-an-extra-source-directory-for-maven-to-compile-and-include-in-the-bui)). The actual question is: should we do it? I would not recommend it. – Turing85 Aug 11 '21 at 23:04
2 Answers
The parent Project in a multi module project should not contain any source code.
Source code belongs to the modules.
If you need shared source code, write a common
module and use it as dependency in the other modules.

- 33,516
- 10
- 64
- 142
Yes, it can. But they are not going to be built (by default), since the <packaging>pom
creates, well, a .pom
file artifact in the Maven repository, no .jar
, .war
, etc.
Furthermore this is an absoulute unusual way of Maven project structure/configuration. I strongly recommend to avoid this under all circumstances since this is working against Maven rather than with it.
Remember also that a multi-module/aggregator project is not (necessarily) a parent of its sub-modules. A project is a parent if other modules declare it as <parent>
. Such a parent can be an aggregator, and often it actually is one, but it doesn't have to be one.
See also POM Reference, Inheritance v. Aggregation:
[...] You often see projects that are both parents and aggregators. [...] However, an aggregator project and a parent project are both POM projects, they are not one and the same and should not be confused. A POM project may be inherited from - but does not necessarily have - any modules that it aggregates. Conversely, a POM project may aggregate projects that do not inherit from it.
Long story short, there are the following relationships:
- Aggregator¹ (
<packaging>pom
and containing<modules>
) → (sub-)projects ... for, well, aggregation of (sub-)module projects - Parent project (
<packaging>pom[can also be ¹]|jar|war|...
) ← child project containing<parent>
... for inheritance (of POM configuration from the parent to the child)

- 14,080
- 5
- 48
- 107
-
So, can we build those files as part of the child module? Like the build file will have both the parent and the child artifact id mentioned. And even before building, can we use the classes present in the parent module in the child module? – the_novice Aug 11 '21 at 23:31
-
1What do you mean by using the classes before building? Using them for what? – tgdavies Aug 11 '21 at 23:40
-
@the_novice Why do you want to build the sources of a different project? What's the point of that? The use case? (And, BTW, also the other way round applies: a [sub-]module is not (necessarily) a child project. Please separate these. With "_child module_" it's not clear what you exactly mean: inheritance (parent/child project) or aggregation (aggregator/[sub-]module). You didn't mention any artifactId yet. The identifier of project's i.e. the combination of groupId/artifacId/version (a.k.a. GAV) must be unique. What do you mean by "_before building_"? – Gerold Broser Aug 11 '21 at 23:49
-
By child module, I mean inheritance (parent/child project). To rephrase my question, is it possible to use classes from parent project in the child project? I was asking this, as someone suggested me this design of implementation. I wanted to know if it's possible to do that. – the_novice Aug 12 '21 at 00:47
-
@the_novice It is possible, BUT NOT if the parent has `
pom`. Don't listen to this _someone_ any longer unless he/she gives you a real good explanation in detail (which you should add to your question then) why he/she suggested this. See also J Fabian Meier's answer and the addition to my answer (2nd paragraph). – Gerold Broser Aug 12 '21 at 09:14 -
1Thank you so much Gerold and Fabian for the explanation and guidance – the_novice Aug 12 '21 at 13:37