Sharing Configuration

Ok, so that's a lot. Your pom is probably well over 1000 lines long now. Not only that its very complicated and hard to reproduce. Sure it does everything we want it to but what good is that if we can't easily maintain the configuration across lots of projects.

There are really two ways to go about doing this. Neither is perfect but together they can get pretty close.

Parent Poms

Maven support the concept of inheritance of pom configurations. You can do this easily by specifying a parent element in your pom.

The child will now receive all the configuration from the parent. You can override settings in the child and add additional content to your hearts content.

Parents are a great place to setup dependencyManagement and pluginManagement sections. This ensures all your child modules will use the same version of a dependency and the same version of a plugins in their builds.

Some things to be aware of.

Its difficult to exclude something. Meaning if the parent defines a plugin its not very easy for the child to turn that plugin off. Therefore, be careful about adding ALL your configuration into a single monster parent.

That's where the second approach becomes useful

Tiles

Maven doesn't natively support a concept of composition of configuration. But a third party tool, known as Maven Tiles brings that capability.

Maven tiles allow you to compose in project configuration:

Note: You can not use tiles in a parent as a way for all the children to get the tile. It doesn't work that way. Tiles are meant to be used directly where they are needed.

Tiles of Tiles

Instead of having to list all the tiles you want in particular module you can compose the tiles together using a another tile of course.

Parent Pom's And Tiles

I recommend you put some common configuration in your parent poms and utilize tiles for most of the build configuration aspects.



DO include in your parent pom:

Basic Project configuration (scm, url, issuement management etc)

PluginManagement

DependencyManagement

Project encoding

DO NOT include in your parent pom:

Dependencies

Tiles

DO use tiles on all your leaf projects to configure the build as needed in a repeatable way.

Finished Example

Now that we have our tiles and some parent poms we have lots of wonderful features to help us be great programmers, active members of the community flexibilty for the future.

Flattening

Oh wait there’s more!

Now that you have your set of parents and all your plugins you have one last anoyance. If someone goes to use your library as it stands they will also have to download all your parent poms. On top of that, all your poms that you deployed to your artifact repository will still contain all your build configuration.

In order to clean up those poms and eliminate the need to download the parents we can flatten a pom.

Flattening a pom brings all the dependencies into a single pom. It also removes a lot of unnecessary information from the pom when it is deployed. Things like, plugins, properties, reporting, etc.

Further Reading

Conclusion

We covered LOTS of stuff here and only scratched the surface of how you can customize the plugins we mentioned here. Hopefully, this provides a building block for your own maven endeavors.

Don't forget to checkout all the code mentioned, along with a complete example, and tiles for each plugin, at my github repo: