Friday, April 13, 2012

Maven pom tree



Here is a very nice trick I discovered almost by chance. Maven is an amazing tool to build software project. It scales...
Complex and big projects follows the SCM (Source Control Management) structure, usually a tree. Maven's best weapon is indeed to structure the project in a tree, where each node contains a pom that follows more or less the following rules:
  • module list is the list of children,
  • it shares definition with its subtree,
  • its parent pom is the parent in the tree.
  • its groupId is parent groupID plus parent artifactID
This way, at any place in the tree we have a good idea of what the effective pom is. The project groups together under a sub tree the type of artifact beeing constructed. Each level in the tree is refining the artifacts build instructions as the pom files provides more and more inherited definitions. For example:

documentation/
java/
-product1/
--web/
--wars/
--ears/
-samples/
tools/
-scripts/
--javascript/
--shell/
--ant/

etc.

Indeed, this is smart.

However sometimes in the tree there are duplicates. A java-like project can occur at several places and then it becomes impossible to share, except at the closest common parent.

Fortunately, there is a trick that uses a quite original feature: pom can refer to any arbitrary parent pom. The idea is then to keep separated a hierarchy of pom. These are very specialized and inherits all from a root pom.

poms/
-pom.xml
-pom-java.xml
-pom-war.xml

Now we keep the project tree, but when needed a pom can break the parent rule and refers to one of these poms instead of the tree parent. Different subtrees of same kind can now share the same parent pom.

No comments: