What Is Maven?
Maven, or MVN, is a powerful software project management tool used in the Java development environment to manage and build projects as well as to maintain dependencies.
In this blog, we give an overview of Maven, how it's used, how it works, and compare the benefits and popularity of Maven, Gradle, and Ant.
What Is Maven Used For?
Without a build tool, managing and building Java applications would be a very painful, long, and often-repeated process. With Maven, it’s easy to maintain the project libraries using the dependency system and build the project using one of the goals.
How Does Maven Work?
Maven itself requires Java installed on your machine. You can verify if Maven is installed on your machine by running “mvn -v” in your command line/terminal. Maven is based on the Project Object Model (POM) configuration, which is stored in the XML file called the same – pom.xml. It is a structured format that describes the project, it’s dependencies, plugins, and goals.
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>SampleProject</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> </dependencies> </project>
The above example represents the structure of a simple project that has one dependency on a 3rd party library, JUnit. This file is always in the root of the project folder and when the Maven control commands are executed, the file from the current location is used. Maven also supports multi-module projects, where each of the modules is represented by individual pom.xml stored in respective directory.
Maven Plugins and Build Lifecycle
All the plugins are called using the following command syntax “mvn [plugin_name]:[goal]”. The goal basically represents the function of the plugin user wants to execute. There are already a few plugins present in Maven, so you can often see being it used without the [plugin_name], such as “mvn compile”. As there are sometimes more plugins called during the application build, Maven defines Build Lifecycle. This is used to associate plugin with one of the phases and when the phase is executed, so it’s the plugin. Maven defines the default lifecycle by the following phases:
- validate - validate the project is correct and all necessary information is available
- compile - compile the source code of the project
- test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
- package - take the compiled code and package it in its distributable format, such as a JAR.
- verify - run any checks on results of integration tests to ensure quality criteria are met
- install - install the package into the local repository, for use as a dependency in other projects locally
- deploy - done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
Just getting started with Maven? Our one-page cheat sheet includes the above info, and a lengthy list of Maven options to help you on your way.
Is Maven Popular?
Maven is without any doubt one of the most popular build tools in Java. Together with Gradle, a different build tool with similar functionality, they are used in more than 90% of the Java projects. The rest of the projects are using Ant or other proprietary build tools.
As for each project, there might be different reasons why they choose Maven. For many, the biggest benefits of Maven are in how it manages the project and works with dependencies.
Maven vs. Gradle
While both tools are used with the same goal – namely to automatize the application build process – there are still some major differences. While Maven is using an XML (pom.xml) for a project configuration, Gradle has its own domain-specific language (DSL) based on a Groovy (build.gradle) or Kotlin (build.gradle.kts) code.
|Maven Benefits||Gradle Benefits|
|Dependency management||Official build tool for Android|
|POM – easier to adapt||Better performance|
|Many plugins available||Using Groovy/Kotlin|
The fact that Gradle is using programming language may be big pros for some teams. In a head to head performance comparison with Maven, Gradle often dominates in some key areas.
Maven vs. Ant
Apache Ant is the predecessor of Apache Maven. First released in 2000, Ant was developed as a replacement for a build tool Make, which was used widely in software development in the past. Using an XML file, Ant is used to automatize build tasks. But, without the addition of the Apache Ivy plugin, Ant doesn’t support dependency management.
|Maven Benefits||Ant Benefits|
|POM – easier to adapt||No forced coding convention|
|Many plugins available||No forced project structure|
Ant's limitations were the reason why most of the development team later moved to the Maven, which is not only a build tool but a complex tool that helps with software lifecycle management. There are still a number of projects that use Ant as the main build tool, including SAP Hybris.
Without build tools like Maven or Gradle, developing and maintaining projects would be a painful process. Maven is a great tool that helps not only with building the application but also manages all the dependencies. Gradle is taking a large part of the Java build tool market, and largely focuses on the same functional goal – to manage the build process. The difference is in the way how both tools works. The successful predecessor, Apache Ant, is still there with us but its market share is now really low.
If you are about to start a new project and you are considering which tool to use, always check the main features of both tools and choose the one that works the best for you. While Android developers will most likely go with Gradle, as it is a default build tool for Android applications, they can still decide for Maven. Maven has support for plugins and there is an official Android-Maven-plugin that adds support for Maven.
If you want to hear some additional discussion on Maven, and where it sits within the Java build tool space, be sure to watch this webinar. It breaks down the results of our 2020 Java developer survey, including commentary on the top technologies being used within Java applications today.
If you're looking for more Maven-specific content, the links below are worth checking out.
Today, streamlining your development processes is more important than ever. So what's one issue that gets in the way of almost every Java developer? Redeploys! If you're ready to skip redeploys and get back to coding, try JRebel. JRebel lets developers skip redeploys and maintains application state so developers can keep doing what they do best, coding amazing Java applications.