Maven is the most popular, de-facto build tool for Java projects. But Maven is much more than a build tool. In fact, it can be used to manage an entire project.
As a budding computer science professional, you will inevitably run into Maven POM files that can be hard to understand without knowing the fundamentals of Maven.
This tutorial introduces you to Maven concepts like plugins, goals, dependencies, and more.
Today, we will learn:
This unique course provides easy to follow, in-browser exercises to teach Maven concepts. This course will help you quickly become proficient while working with Maven.
Build Java Projects with Maven
A build is the process of converting source code files into software artifacts to un on a computer. This process is usually managed by a build tool, a program that controls other programs.
Maven is the most popular, de-facto build and management tool for Java projects. Maven is based on the concept of a project object model (POM), which helps to automatically manage a project’s build, documentation, and reporting from a central piece of information.
Maven has become very popular over the years, and it has defined a common interface for building software. Maven’s implements plugins that can be retrieved from the Maven repository.
Simply put, Maven is a tool for building and managing Java-based projects that make work of Java developers much easier.
Maven offers support for many tasks, including:
The Maven Build Tool is ideal when a project has many dependencies or when project dependency versions update often. It is also well-suited for continuous builds, integration, and testing, or when you need to generate documentation from your source code.
Pros:
pom
file.pom
file.Cons:
Now that we know what Maven is and how it works, let’s learn about the most salient features of this build tool. We will break these down in more detail later in the article.
Maven can accept various plugins to perform tasks. Since the core of Maven is small, plugins are essential to leverage the intelligence of this tool. Plugins are just code that implement logic to perform various tasks during the build process.
Some of the most common plugins are:
A Maven plugin is made of goals that are a unit of work. A goal is an action we want to take on the project defined by a POM file. Take a look at this diagram to understand how these components interact:
Learn Maven without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments - making learning quick and efficient.
A Maven project is described by a POM file (Project Object Model), which is a declarative description of your Java project. The POM is an XML file that defines the project’s unique Maven coordinates, dependencies, required plugins, parameters, etc.
When invoked, Maven looks for the POM file in the current directory structure. Without it, an error will be thrown. Then, Maven reads the POM, gathers the configuration information, and executes the goal.
Some of the configurations that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on.
You can also specify the other information such as the project version, description, developers, and mailing lists.
Let’s look at a minimal version of a POM file.
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>io.datajek</groupId>
<artifactId>empty-project</artifactId>
<version>1</version>
</project>
The file contains minimal project description without dependencies or plugins. If you run mvn install
in the directory containing the above POM file, you’ll see a successful build.
There are several elements we use for creating this file:
Other elements include dependencies, name, scope, and packaging.
Maven supports a build lifecycle, which is an ordered set of actions used for building a project. A build life cycle is made up of build phases, and each phase has plugin goals, which are executed when the phase executes.
To understand this better, let’s look at an example.The clean lifecycle includes:
pre-clean
clean
post-clean
The phases of a lifecycle execute in order, meaning that pre-clean
will execute first, clean
secondly, and finally post-clean
.
If we run the clean phase in an EmptyProject using mvn clean
, we can specify the phase with the mvn
command. When we execute the clean
phase, the previous phase will also execute if it has any plugin goals.
A repository in Maven holds both build artifacts and dependencies. In software development, artifact refers to objects that are produced in the process, like design documents, data models, and workflow diagrams.
There are two main types of Maven repositories:
Generally speaking, local repositories can be left alone, except for cleaning. You will need to download remote repositories, which are triggered when declaring a dependency.
Maven will download from the central repository by default.
Downloading Maven is quite easy. Start by verifying that you have Java installed. Check if your Java environment variable is set. From there, download Maven from the official site.
You can unpack the zip from anywhere in your system and add the bin directory to the PATH environment variable and system variable.
From there, cmd
and run the mvm -v
command. A proper installation will print the following lines:
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T01:19:05+05:30)
Maven home: C:\apache-maven-3.5.3\bin\..
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: C:\Program Files\Java\jdk1.8.0_151\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
You should now have a strong idea of what Maven is and how to use it in your Java projects. Maven can make your build process far easier and automate the time-consuming tasks. But there is still a lot more that Maven can offer you.
The advanced Maven concepts that you should learn next are:
To get started with these advanced concepts and get more practice with what we’ve learned today, check out Educative’s course Build Java Projects with Maven. This unique course provides easy to follow, hands-on, in-browser exercises to teach Maven concepts like plugins, goals, dependencies, and more.
After these lessons, you’ll be working confidently with Maven in no time!
Happy learning!
Free Resources