I’m currently developing multiple web applications using Spring. I’m using Maven for building and Git for version control. At the moment I’m trying to find a way to split development of some things used by all webapps, e.g. I have some helper classes that are the same for all projects. The problem is, I don’t want to use only classes, but also resource files and some sort of parent POM while still being independent from a repository and able to benefit from Git.
Although I’m not enthusiastic to change the build system, I’m not a real fan of Maven. Especially the concept of inheritance and aggregation is what constrains me right now. Maybe Ivy is an option?
I’d want to give you a quick overview of my setup:
There’s some sort of parent project including some classes, Spring configuration files and other resources like templates, images and style sheets. Let’s call it base. This one is not a complete Spring webapp and won’t be deployed. There are several other projects which inherit from base and should be packed into a WAR. Let’s call them webapp1 and webapp2.
base, webapp1 and webapp2 have their own Git repositories:
\
|
|- base.git (base's repository)
|
|- webapp1.git (webapp1's repository)
| \
| base (base used as a Git submodule)
|
|- webapp2.git (webapp2's repository)
\
base (base used as a Git submodule)
I want to be able to change bases code from inside the the webapps using a Git submodule and also be able to build a fully functional WAR of each webapp using mvn package inside the webapp`s directory.
Maven’s parent or module don’t allow a dynamic approach like this. I didn’t find a way to use module like that at all and using parent for my needs is complex and static: Every change to base would require a new version to be pushed to the repository so that the webapp`s can inherit from it.
Maybe I didn’t completely understand Maven’s inheritance, but I’m pretty lost right now.
Did anyone achieve something similar with success? What build system did you use and how?
I found a working solution by myself.
The basic solution of my problem is a small sub-element of the
<parent>element called<relativePath>. It allows to search for the parent POM in the specified directory. Otherwise you would always need to deploy a new version before you could test it within your application.But that was only the starting point, allowing to work with a Git submodule. To get it really working, I needed to do the following:
Create a WAR with classes and resources from both, the webapp and
base, i.e. all files insideWhile resources are pretty easy to cover, a bit of configuration for
org.apache.maven.plugin:maven-war-pluginis needed to get the webapp resources into the WAR. Compiling of two different source folders requires a plugin, so I need to useorg.codehaus.mojo:build-helper-maven-pluginto get the classes ofbaseinto the WAR.Additionally, I used a lot of filtering for the resources, so there’s almost no need for customization of the basic files to get a webapp up and running. For example I use
${project.artifactId}inside my main template file, so my HTML<head>will look something like this for a webapp calledwebapp1:It really took a lot of trial and error, but at last I got it working and I think this is the best way to achieve my target using Maven. This would’ve been a lot easier using a dynamic tool like Buildr, but sadly Buildr is slow on Windows (thanks to Ruby) and doesn’t integrate very well into most IDEs.