Chapter 1. Maven and Its Philosophy

If we ask software developers what Maven is, the majority will probably respond that Maven is a build tool. We can't say they are wrong, but this definition is not complete. If we want to be more precise, we should say that Maven is a project management tool that provides build and distribution functionalities, code generation, and communication features. Above all, Maven provides an advanced dependency management system that is able to retrieve transitive dependencies and download them from both local and remote repositories.

Maven is built with a plugin-based architecture; the core architecture provides a set of features that can be extended through a set of official or custom plugins downloadable from repositories.

Maven comes with the convention over configuration philosophy. The origin of this philosophy resides in the idea that accepting conventions resulting from a set of past experiences leads to advantages such as saving time, reuse, and maintenance simplification. Maven pursues this philosophy through the use of defaults, which means that unnecessary configurations should be avoided; a project should just work.

While the use of defaults is a powerful concept, users might want to customize some behavior. Maven meets the users' needs by allowing the customization of almost all defaults.

In this chapter, we will:

  • Introduce Maven and explain its basic concepts
  • Present the example project used to show the concepts that we will treat
  • Start structuring the project, deepening some of the concepts introduced

Core concepts of Maven

In this paragraph, we will explore some of Maven's core concepts through a simple example. Maven defines project configurations through a Project Object Model (POM), which is stored in a file named pom.xml.

The following example of pom.xml defines a simple project. Such a simple POM file is capable of compiling and building the project without the need to specify any additional information:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <!-- Project coordinates -->
    <groupId>com.mycompany.projects</groupId>
    <artifactId>my-first-maven-project</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>My First Maven Project!</name>
</project>

Reading the example POM file, the following concepts emerge:

  • Inheritance: This concept simply means that everything that is not specified in a POM file is inherited from the upper-level POM. At the top level, Maven provides a parent POM defining all default values, which were mentioned earlier. According to this principle, multimodular projects are often structured with a root POM file defining common settings and a pom.xml file related to each submodule to manage each module's peculiarities.
  • Overriding: This concept derives from the preceding one. All the values defined in the lower levels of the POM hierarchy override the definitions in the upper layers. As we can see in the preceding example POM file, only the project's coordinates have been defined, any other value is inherited from the parent POM. A project is uniquely identified into Maven repositories by its coordinates, which is composed of groupId, artifactId, and version. Project coordinates are fundamental since they allow Maven to correctly manage modules and plugins.
  • Modularity: When we install Maven, we formally install only its core functionalities. Whenever we need some extra features, we can find it in some plugin. Plugins as well as software dependencies are downloaded from a set of configurable repositories.
  • Repository: Maven downloads a project's dependencies and plugins through repositories. Maven only distinguishes between two types of repositories: local and remote. The local repository is a folder inside the machine in which a project is being developed, acting as a cache with respect to the remote repositories. For what concerns remote repositories, Apache provides a central repository containing thousands of common dependencies, which is the default one. Maven does not rely on this specific repository, thus allowing users to define their own custom repositories.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset