POM Best Practices

Maven can be used to manage everything from simple, single-project systems to builds that involve hundreds of interrelated submodules. Part of the learning process with Maven isn’t just figuring out the syntax for configuring Maven; it is learning the “Maven Way”—that is, the current set of best practices for organizing and building projects using Maven. This section attempts to distill some of this knowledge to help you adopt best practices from the start without having to wade through years of discussions on the Maven mailing lists.

Grouping Dependencies

If you have a set of dependencies that are logically grouped together, you can create a project with pom packaging that groups dependencies together. For example, let’s assume that your application uses Hibernate, a popular Object-Relational Mapping framework. Every project that uses Hibernate might also have a dependency on the Spring Framework and a MySQL JDBC driver. Instead of having to include these dependencies in every project that uses Hibernate, Spring, and MySQL, you could create a special POM that does nothing more than declare a set of common dependencies. You could create a project called persistence-deps (short for “persistence dependencies”) and have every project that needs to do persistence depend on this convenience project. See Example 9-13.

Example 9-13. Consolidating dependencies in a single POM project

<project> <groupId>org.sonatype.mavenbook</groupId> <artifactId>persistence-deps</artifactId> ...

Get Maven: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.