Using maven and still struggling with the build process? Or wish it was better?
What to expect out of the article?

  • Building all the modules of a project at once
  • Parameterized builds with jenkins
  • Making your life easy

Stating some facts about maven which you might already know.

  1. Maven is a very smart build tool
  2. ‘Head of the Family’ POM
    To make life easier with build processes with maven. Follow what I call ‘Head of the Family’ POM. This pom is basically the pom which lies at the topmost level in the pom hierarchy. [The original, in case you follow vampire diaries]. The following are the advantages of using this pom.
    * The head of the family decide what versions of artifacts are to be used
    * Let it decide what is the current build version of the revision
    * What are the individual versions of the modules involved
    * Global variables like repositories, profiles etc can go into the head of the family pom
    * No redundancy, clean children poms
  3. Using properties defined in the parent pom
    The properties declared in the parent pom are available to the child poms. Say we have defined a build version in the parent pom, <build.version>6.0.5-SNAPSHOT</build.version>
    This build version can be accessed in the child poms using ${build.version}
  4. What does the modules tag in a pom mean?
    Three of the most important features of maven are inheritance, aggregation and dependencies. When we include a modules tag, we state that the project is a multi-module project and is dependent on a list of modules. So, we build a multi-module project, all the modules are build one by one in the order in which they are listed.

Lets apply what we just learnt

  • Building all the modules of a project at once
    As stated in point 4, the modules tag builds the children modules. So, using the head of the family pom correctly, weaving your way through the hierarchy of the project in the correct order will allow you to build the whole project at once.
  • Parameterised builds with jenkins
    Ever felt the need to use an if else in the maven pom. Well the most accepted way of doing that is to create profiles. Profiles is simple terms can be imagined as a set of properties declared in pom. Using the same job for building artifacts for two different environments will require setting up various profiles for the build. But, what if there is a large number of permutations of the properties, then it is not suggested to use profiles for each such set of properties. Here comes jenkins to our rescue. Using a parameterised build will allow setting up the property values just before the build. Thus, giving you flexibility of changing properties without having to commit to the repositories.In jenkins, setting parameters for a job means to set up an environment variable.
    To know about setting up parameters, just go through setting up parameters.
    Now, to use a parameter in the maven pom, just prepend ‘env’ to the parameter name, i.e. build.version becomes -> ${}. And now you are smart enough to interpret that the env variables can also be used in the pom. e.g. ${env.JAVA_HOME}.


In case, your project is using some continuous delivery tool like jenkins and not following what I just mentioned, please do yourself a favor and implement what you just learnt to make your life easy.
Lets spend less time building what has already been built.