Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-module IntelliJ project with maven - How to add dependencies from one module to another?

Let's say I have a maven project which has some maven modules inside.

My main module depends on the other modules, so when I compile the main module they should be compiled together.

The question is, how to add these modules as dependencies to the main module?

I know if I have a custom lib that I want to use with maven, let's say a utilities project, I have to compile the jar of the project, do a mvn install:install-file on it to install it on the local repository and then add it to the pom.xml.

Do I have to do this with all my modules and add the dependency to the pom.xml on my main module? Because if it should be done like this, there will be a lot of work to do when changing code on the other modules.

What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?

like image 639
Mateus Viccari Avatar asked Dec 22 '14 17:12

Mateus Viccari


People also ask

Can two Maven modules depend on each other?

Because modules within a multi-module build can depend on each other, it is important that the reactor sorts all the projects in a way that guarantees any project is built before it is required. The following relationships are honoured when sorting projects: a project dependency on another module in the build.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.


1 Answers

The question is, how to add these modules as dependencies to the main module?

The same way you add any other dependency to your maven project. By adding group id, artifact id and version to <dependency> element

Do I have to do this with all my modules and add the dependency to the pom.xml on my main module?

If your main module depends on some module A then only the pom of the main module should contain dependency declaration towards module A. You do that for all the dependencies of your module.

I don't know what you mean by "a lot of work when changing the code on other modules". Maven has nothing to do with code changes, it just builds the projects whatever they look like at the given moment...

What is the best practice to use avoid the trouble of compiling/installing the modules to local repository?

Any project that you invoke mvn install on gets built and it's jar copied to local repository. That's all you need to do to get the jar into the repo. This will also put all the dependent jars, if available, into the local repo.

As for best practices for multi module projects:

If your parent project (the one that has modules inside) has <modules> section that lists the modules of your application, and modules are in subdirectories of your parent project, then you simply mvn install (or whatever you want to do) the parent project and that will cause all the modules to be built in order defined by declared dependencies between them. That means that if your main module has dependency on module A, then module A will be built before the main module. This way you can build and install all your modules with one command. On the other hand this approach makes more tight coupling between modules which is not desired in some cases, so it depends on your use case whether it is a good approach or not.

like image 97
miljanm Avatar answered Oct 03 '22 21:10

miljanm