Apache maven is often misunderstood as just a java build tool, which means people think maven is a tool that only creates deployable artifacts (jar, war, zip, ear, etc) from the source code. Many beginners try to compare maven with other build tools like Ant and Ivy, but in reality, maven is much more than that. Because build tool such as Ant is focused solely on preprocessing, compilation, packaging, testing, and distribution. Maven provides many much more features than what is found in a build tool.

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information.

What is maven?

Maven is a project management tool based on a POM (project object model) and a set of standards to manage the project’s build, test, deployment, documentation, and reporting.

POM – A project object model is basically a pom.xml file which can be reused through inheritance.

Project Lifecycle – Various activities related to a project such as, build, test, run, reporting, document generation and etc are known as project lifecycle.

Maven is one of the most mature and stable tools out there, if you are a beginner don’t be scared, you will get your grip as you continue reading further. Maven is built on several principles which are discussed below.

1. Convention over configuration

The apache maven is built with convention over configuration principle, which means it assumes certain defaults and requires fewer configurations to work. You can relate it to Ruby on Rails, EJB3 or Spring Boot.

By default, the source code is assumed to be in ${basedir}/src/main/java and resources 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 dir and then create a distributable .jar file in ${basedir}/target.

2. Maven provides a common standard

Before maven became popular, it was a pain to brainstorm and agree upon a folder structure that will be used and respected by all the team members. Not just that, every project had its own build system. Too much time and efforts were getting invested (wasted) in doing things other than the actual project work.

The age before Maven was an age of inefficiency.

Today, most of the open-source and close source java projects use maven. There are other successors like Gradle, however, maven is still the most popular build tool. It works as a common interface and allows customization through plugins and configurations.

3. Reusability and customization through maven plugin

Maven plugins allow developers to add new behavior to the project’s build. Both, the dependencies and plugins are downloaded from the maven central repositories, allowing universal access. For example, maven spring-boot plugin allows you to work on a spring-boot project. Similarly, maven has plugins for every framework and every purpose.

4. Conceptual Model of a “Project” – pom.xml

Maven is not just about compiling the source code to byte code, you need to create a description of the project in a pom.xml file. Pom.xml is called the project object model where you provide all the information about the project like, project name, who maintains it, what are the other libraries and dependencies it uses, project’s license, etc.

The project attributes are described in the pom.xml.

What is pom.xml in maven?

Every project is uniquely identified by a set of attributes like name, groupId, artifactId, version. A project can use these coordinates (attributes) to declare dependencies. I understand this is too much of theory, will look into examples little later.

Use of pom.xml has several advantages.

  1. Dependency Management: You can declare the exact library names, versions, etc in the pom.xml file.
  2. Remote Repositories: You can specify the remote repository where the project build will be uploaded and the repository from where dependencies will be downloaded.
  3. Universal reuse of build logic: Maven plugins contain logic for custom builds and custom operations which can be reused.
  4. Tool portability / Integration: Maven is now well supported in all major IDEs like Eclipse, NetBeans, and IntelliJ.
  5. Easy searching of project Artifacts: Tools like Nexus allow you to index and search the contents of a repository using the information stored in the POM. You can use the online maven search tool to find a specific java dependency, just like npmjs for JavaScript dependencies.

The differences between Maven and Ant

Apache Maven

  1. Maven is a project management tool.
  2. Maven follows conventions over configurations. It knows where the source code and test code is because you followed the convention. Maven’s Compiler plugin puts the bytecode in target/classes, and it produces a JAR file in the target.
  3. Maven is declarative. All you do is create a pom.xml file and put your source in the default directory.
  4. Maven has a lifecycle which is invoked when you execute mvn install to build a jar file.
  5. The maven plugins are reusable.

Apache Ant

  1. Anit is a build tool.
  2. Ant does not follow conventions like maven. You need to specify where the src and test are located and where to put the build output.
  3. Ant is procedural and you need to configure ant exactly what to do and when to do it.
  4. Ant doesn’t have a lifecycle. You have to define goals and goal dependencies. You have to attach a sequence of tasks to each goal manually.
  5. Ant scripts are not reusable

A sample pom.xml file

Please spend some time observing the sample pom.xml below, see how project attributes are specified.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
  <name>The nb app</name>
    <!-- Hibernate -->
    <!-- HikariCP-->

As you can see in the above pom.xml file, groupId, artifactId and version are used to uniquely identify a project. properties are like key-value pairs which can be reused anywhere in the later part of the project object model. repositories define the central maven repo from where dependencies will be fetched, by default maven will fetch everything from maven.apache.org. Similarly, project dependencies and plugins are also defined.

I understand that the introduction part was too much theory, but trust me later part of the tutorial is fun.

Further reading:

  1. The Ultimate Java Build Tool Comparison: Gradle, Maven, Ant + Ivy
  2. Apache maven official site