Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: Combine web projects

I have following Maven projects set up:

  • PM-Core
  • PM-Web (with a dependency to PM-Core)
Now, this project is used for several clients but for each client there are some small differences: mostly differences in configuration files but some clients also require additional java files (which may not be installed for the other clients).

I've been considering several alternatives on how to support this with maven but am still looking for the perfect solution.

The best solution I can think of is to create a separate maven project for each client (e.g. PM-CLIENT1, ...) which contains only the client specific configuration files and additional java files or jsp's, ... . Next step would be to consider the PM-Web project and the client project as one web project, meaning: have them combined (packaged) into 1 war file with files from the client project having precedence over files from the PM-Web project.

More concrete: running mvn package on PM-Client1 would take everything from PM-Web, add/replace the files from PM-Client1 and then package this into a single war.

So the question is: how to achieve this with maven?

like image 590
Stijn Geukens Avatar asked Jan 14 '10 12:01

Stijn Geukens


People also ask

How do I integrate two Maven projects?

let's assume that you have two maven projects A and B. Now create a new project C in your intllij-idea. In project C, create a new module A1. Copy the dependencies and build, properties (if present) from pom.

What is multi Maven project?

A multi-module project is built from an aggregator POM that manages a group of submodules. In most cases, the aggregator is located in the project's root directory and must have packaging of type pom. The submodules are regular Maven projects, and they can be built separately or through the aggregator POM.

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.


3 Answers

Yes, this can be done using Overlays. The sample on the webpage is exactly what you are talking about.

For the project structure, you could have something like this:

.
|-- PM-Core
|-- PM-WebCommon (of type war, depends on core)
|-- PM-Client1 (of type war, depends on webcommon)
`-- PM-Client2 (of type war, depends on webcommon)

And use overlay in PM-Client1 and PM-Client2 to "merge" them with PM-WebCommon and package wars for each client.

UPDATE I won't cover all the details but I think that declaring the war dependency with a scope of type runtime is required when using overlay, this is how overlay do work (actually, the whole overlay thing is a kind of hack). Now, to solve your eclipse issue, one solution would be to create a JAR containing the classes of the PM-WebCommon project. To do so, use the attachClasses optional parameter and set it to true. This will tell maven to create a PM-WebCommon-<version>-classes.jar that you'll then be able to declare as dependency in PM-Client1 (with a provided scope). For the details, have a look at MWAR-73 and MWAR-131. This is also discussed in the FAQ of the war plugin. Note that this is not a recommended practice, the right way would be to move the classes to a separate module (and this is the other solution I wanted to mention).

UPDATE (201001018): I've tried the attachClasses parameter and it works with version 2.1-beta-1 of the plugin.

like image 76
Pascal Thivent Avatar answered Oct 22 '22 02:10

Pascal Thivent


You could use profiles see http://maven.apache.org/guides/mini/guide-building-for-different-environments.html and use classifiers to distinguish between the artifacts from the different builds for the same version. In this setup, you could create additional optional modules for each of your clients specific customisations under the parent project i.e.
+ PM
++ PM-Core
++ PM-Web
++ PM-Client1
++ PM-Client2

Or you could look at using use the maven assembly plugin

like image 23
crowne Avatar answered Oct 22 '22 03:10

crowne


Compare also the answers for question different WAR files, shared resources .

like image 1
Hans-Peter Störr Avatar answered Oct 22 '22 03:10

Hans-Peter Störr