Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a Maven module have to be a subdirectory of a parent project?

Tags:

module

maven

I've been looking into multi-module projects in Maven, and I can only find examples of modules existing as subdirectories to a parent project. Would I be mistaken in deducing from this that there's no way to share a module between two maven projects without having to package it up and deploy it to a repository? That would strike me as needlessly cumbersome.

For example, say I'm actively working on two related projects that don't talk to each other except through an API bridge I'm working on that I plan to use for future things. The bridge is constantly updating as work on the two projects progress.

So, if I want to leverage Maven modules for this effort, one project pom.xml would look like this...

<groupId>net.syndog</groupId>
<artifactId>myproject</artifactId>
.
.
.
<modules>
    <module>myproject-bridge</module>
</modules>

...and another looks like this...

<groupId>net.syndog</groupId>
<artifactId>myotherproject</artifactId>
.
.
.
<modules>
    <module>myproject-bridge</module>
</modules>

Assuming modules in Maven must be subdirectories of the projects in which they're a part of, how can myproject and myotherproject both use the myproject-bridge module at once?

Is this even possible with Maven, or do I have to package and deploy myproject-bridge to a repo every time I make a tweak to it? Perhaps I'm mistaken, but this seems like it would be such a commonplace issue, but no one seems to be talking about it. Am I going about this all wrong?

like image 561
Syndog Avatar asked Apr 07 '12 09:04

Syndog


2 Answers

The usual way of using such a dependency is to define a dependency which results in this:

<dependency>
  <groupId>net.syndog</groupId>
  <artifactId>xyz-api</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

but the result of this approach is that you need to have a separate project which contains exactly this api. This can be simplified by using a IDE which solves the dependencies between the projects via the workspaces instead of the repository. Otherwise you need to do regularly a mvn install (if you are working locally only) or mvn deploy (via repository manager).

The other solution would be to use a real multi-module project which would look like this:

+-- pom.xml (root)
+-- myproject-bridge
     +--- pom.xml
+-- myproject-a
     +--- pom.xml
+-- myproject-b
     +--- pom.xml

where project-a and project-b using the myproject-bridge via a simple dependency. In the root you define the modules (as you mentioned) via

<modules>
 <module>myproject-bridge</module>
 <module>myproject-a</module>
 <module>myproject-b</module>
</modules>

and in myproject-a you simply define

<dependency>
  <groupId>net.syndog</groupId>
  <artifactId>xyz-api</artifactId>
  <version>${project.version}</version>
</dependency>

and in myproject-b the same way. You must be aware that using a module does not necessarily means to define a dependency. This gives you also the opportunity to use the myproject-bridge from a complete different project either after release or as SNAPSHOT.

like image 147
khmarbaise Avatar answered Sep 16 '22 12:09

khmarbaise


If we take the question to be that in the title, then the question is this:

Does a Maven module have to be a subdirectory of a parent project?

So long as the question is about Maven multi-module projects and not Maven inherited projects, then the answer to that question is, "No." Here's why.

As matsev's answer above makes clear, "Each module listed is a relative path to the directory containing the module (emphasis added)." It need not be a sub-directory so long as it is a relative path from the super-project (I avoid the terms "parent" and "child" so as not to involve the unrelated concept of inherited projects) to the sub-project. This constraint can be satisfied in a variety of ways that may be OS-dependent. For example, you might include parent directory path elements (..) to traverse to the sub-module directory. Or, you might use symbolic links. In any case, no, it is not strictly true that a Maven module must be a sub-directory of a Maven multi-module project's directory.

like image 28
David A. Ventimiglia Avatar answered Sep 16 '22 12:09

David A. Ventimiglia