Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage multi app project in maven?

Tags:

java

module

maven

I am currently trying to migrate a multi-app project from Ant to Maven.
At the moment the project consists of multiple packages, creating some kind of dependency tree, without circular dependencies. The leaves of this tree are "application" packages, containing a Main. Intermediate nodes are "library" packages, used by other library "packages" or "application" packages.
The nodes are allowed to "grow together" to a single node or leaf. I figured out, that those packages should probably be grouped into maven modules and I now have a structure similar to this:

root
    - lib1
    - lib1A (depends on lib1)
    - lib1B (depends on lib1)
    - app1A (depends on lib1A)
    - lib2  (depends on lib1B)
    - lib2A (depends on lib2)
    - lib2B (depends on lib2)
    - app2  (depends on lib2A and lib2B)
    - lib3  (depends on lib2A and lib2B)
    - app3A (depends on lib3)
    - app3B (depends on lib3)

Basically a library and an application can depend on one or more other libraries.
Now I would like to be able to build each app on it's own and create an executable jar for it.
The way I am trying to do it now is the following:

  • configure the pom.xml of every app to use maven-assembly-plugin to create an executable jar.
  • Build each needed module for a specific app.
  • Build the app-module, which results in a executable jar.

So the build for app2 would build lib1, lib1A and lib1B, lib2, lib2A and lib2B and finally app2.
However, to automate the build, I would need to create a build-script for every app, which takes care of building all needed dependecies, which maven should already do by itself.
Also, if I want to build multiple apps at once, I would need to build all libraries multiple times, or track the already built modules by myself.
As I am new to maven, I am not sure if that's the correct way to manage such a multi-app project.
So I am asking for some advice on how to correctly manage this use case.

EDIT:
To clarify what I would like to be able to do:

  • build a single app with it's dependencies, without building all apps (running maven on the parent pom).
  • build multiple apps (not all) with their dependencies, without building the dependencies multiple times.
like image 970
Robert P Avatar asked Nov 16 '25 23:11

Robert P


1 Answers

I am now using a parent project, which defines the maven version, the common dependencies and the common plugin configurations.
The parent project also defines it's child modules in module-tags.
Every child module references this parent project and uses the same version.
To build the applications, I am running maven inside the parent project, using the -pl and -am flag, as mentioned in this comment.
The -pl flag tells maven to only build the listed modules, instead of building the whole project.
The -am flag tells maven to also build the needed dependencies.
Example:
Given the following structure:

parent
---- lib1
---- lib1A (depends on lib1)
---- lib1B (depends on lib1)
---- lib2 (depends on lib1B)
---- lib2A (depends on lib2)
---- lib2B (depends on lib2)
---- app1A (depends on lib1A)
---- app2A (depends on lib2A)
---- app2B (depends on lib2B)

Executing mvn clean install -pl app1A,app2A -am would build all modules except app2B and lib2B.
Also, the module lib1, which is used by app1A and app2A would only be built once.

Usually, you want to version each module independently, but in our case this would introduce a huge effort, since we have a lot of modules building on top of each other. Small changes in the "lowest" module (lib1 in the example) would cause changes in almost every module.
So in this case we would need to increase every version number and update all referenced dependencies in all modules.
We instead decided to always rebuild all dependencies, resulting in a always up-to-date Jar. That's why we only manage the maven version in the parent project.

like image 106
Robert P Avatar answered Nov 19 '25 13:11

Robert P