Chapter 1. Introducing Apache Maven

Convention over Configuration

Convention over configuration is a simple concept. Systems, libraries, and frameworks should assume reasonable defaults without requiring that unnecessary configuration systems should “just work.” Popular frameworks such as Ruby on Rails and EJB3 have started to adhere to these principles in reaction to the configuration complexity of frameworks such as the initial Enterprise JavaBeans™ (EJB) specifications. An illustration of convention over configuration is something like EJB3 persistence. All you need to do to make a particular bean persistent is to annotate that class with @Entity. The framework will then assume table names and column names from the name of the class and the names of the properties. Hooks are provided for you to override these names if the need arises, but, in most cases, you will find that using the framework-supplied defaults results in a faster project execution.

Maven incorporates the concept by providing sensible default behaviors for projects. Without customization, source code is assumed to be in ${basedir}/src/main/java and resources are assumed to be in ${basedir}/src/main/resources. Tests are assumed to be in ${basedir}/src/test, and a project is assumed to produce a JAR (Java ARchive) file. Maven assumes that you want to compile byte code to ${basedir}/target/classes and then create a distributable JAR file in ${basedir}/target. Although this might seem trivial, consider the fact that most Ant-based builds have to define the locations of these directories in every subproject. Maven’s adoption of convention over configuration goes further than just simple directory locations; Maven’s core plugins apply a common set of conventions for compiling source code, packaging distributions, generating web sites, and many other processes. Maven’s strength comes from the fact that it is “opinionated.” It has a defined lifecycle and a set of common plugins that know how to build libraries and web applications. If you follow the convention, Maven will require almost zero effort—just put your source in the correct directory, and Maven will take care of the rest.

One side effect of using systems that follow “convention over configuration” is that end users might feel that they are forced to use a particular setup. While it is certainly true that Maven has some central opinions that shouldn’t be challenged, most of the defaults can be customized. For example, the location of a project’s source code and resources can be customized, names of JAR files can be customized, and through the development of custom plugins, almost any behavior can be tailored to your specific environment’s requirements. If you don’t follow convention, Maven will allow you to customize defaults in order to adapt to your requirements.

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.