4

In my project I have two modules (A,B) depend on common module C. I would like A,B share common configuration, such as repository configuration, plugin configuration, etc. In order to achieve this, C is made the parent pom and A,B inherit from it (no aggregation is required, so C does not reference A,B)

However, C by itself contains some java code, along with unit tests. Now, if I run mvn test inside C nothing is executed. Supposedly, pom-packaged modules should not include any code.

Is the above configuration terribly flawed? Should yet another common pom be introduced even though one already exists?

Ivan Balashov
  • 1,897
  • 1
  • 23
  • 33

1 Answers1

3

At very typical pattern in Maven projects is to have a module named "parent" which holds your common Maven-specific configuration, profiles, properties, dependency versions, etc. This is The Maven Way (TM).

The structure looks something like the following:

|-- parent
|-- common
|-- module-a
|-- module-b

So, common, module-a and module-b each depend on parent, and module-a and module-b depend on common.

See this excellent resource: http://www.sonatype.com/books/mvnex-book/reference/multimodule-web-spring.html

Note that "parent" can be a sibling of its child modules, in terms of directory structure. There is no requirement that the directory structure hierarchy match that of the modules.

noahlz
  • 10,202
  • 7
  • 56
  • 75
  • Thanks, Noahz! I was kinda afraid to hear it. Any idea why parent and common cannot be the same module, at least theoretically? Is there any fundamental issue here? – Ivan Balashov Sep 26 '12 at 22:09
  • Single responsibility principle. No technical reason. – noahlz Sep 26 '12 at 22:38
  • I have this same exact project structure, but want instead of having to produce three different jars (common, module-a, and module-b), I'd like to just build module-a and module-b, with the code in common appended to both. Is there any way to do this? – sgonzalez Jun 15 '18 at 22:21