Understanding Maven

Nipuna Upeksha
Javarevisited
Published in
9 min readAug 24, 2021

If you are a Java developer one of the most frequent words that you hear during your life is Maven. So what is Maven and what does it do? So in this article, we are going to cover a few of the most important concepts regarding Maven and how can we use it for automation.

What is Apache Maven?

As indicated in the https://maven.apache.org/,

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.

Looking at Apache Maven more comprehensively,

  • This is a project management tool with a project object model.
  • Which has a set of standards.
  • Which has a project life cycle.
  • A dependency management system.
  • A logic that executes the plugin goals during various lifecycle phases.

Having the above-mentioned features, Maven can give a simple project setup that uses the best practices, and with Maven our project can follow a consistent structure that is independent of the IDEs. In addition, one of the most important usages of Maven is that it simplifies the declaration of project dependencies.

Focusing on the last point, when you build a traditional Java application, you include the dependencies by downloading them directly and including them in the project structure. If you have already done this you know how much of a laborious work that is. In addition, to download those dependencies you have to keep a track of their versions for your project to work. But that is where Maven comes to help. It helps you to keep a track of your dependencies and their respective versions along with downloading them when they are needed to build the project.

This process is done by Maven using a separate file called pom.xml. By doing this Maven simplifies the project dependency management greatly.

Download Apache Maven

To use Maven, go to https://maven.apache.org/ and click the Download link. You can click the Download in the side panel or the Download, Install, Run Maven in the content section.

Home Page of https://maven.apache.org

When you click that link you will be directed to the downloads page, where you will see the system requirements for Maven (Always try to download the latest version of Maven). The most important requirement here is the JDK version. Right now the latest version of Maven is 3.8.2 and the JDK version requires for that is JDK 1.7.

Download Page

And when you scroll down you will see the installation instructions and the downloadable files. Here, we will select the apache-maven-3.8.2-bin.zip file and will look at the installation instructions later.

Download apache-maven-3.8.2-bin.zip

Now go to the installation instructions page. There you can see the instructions on how to install Apache Maven on your local machine.

Installation instructions page

As per instructions, first, unzip the downloaded zip file. After unzipping the file you will have a directory apache-maven-3.8.2. And the contents of that folder look like this.

apache-maven-3.8.2 directory

Here, we will look at how to install this on a windows machine. For Linux machine users you can follow the following guide (steps 3 & 4).

https://docs.wso2.com/display/IS400/Installing+Apache+Maven+on+Linux

For windows users, copy the apache-maven-3.8.2 folder and paste it at C:\Program Files. Then type environment variables in windows search in your taskbar and open edit the system environment variables.

Editing environment variables

Then click on Environment Variables and click on New in System Variables. For variable name, give MAVEN_HOME, and for value, give C:\Program Files\apache-maven-3.8.2.

Setting up MAVEN_HOME

Then select the Path variable in System Variables and click Edit. And then click New on the popup and type %MAVEN_HOME%\bin. Then press OK.

Adding Maven to the path

Then you can open the command line and type mvn -v to check whether the Maven is added to your path or not.

mvn -v command

I have already installed Java and added it to the path. You can install Oracle JDK or Open JDK for Java and set it up in the path variable by defining a new variable JAVA_HOME as we discussed before. You can check whether the variables are working by typing echo %JAVA_HOME% and echo %MAVEN_HOME% in your command line.

JAVA_HOME and MAVEN_HOME

Understanding Maven — POM

As mentioned above Maven works with a project object model or POM. As per Github, “Introducing Apache Maven”,

POM stands for Project Object Model. This model has a set of standards, a project life cycle, a dependency management system, and a logic for executing plugin goals at defined phases in a lifecycle.

When creating a Java project using Maven, the following features can be seen.

  • Projects are set up with default behaviors.
  • Source code must be in the src/main folder.
  • Resources necessary for the project are in another folder.
  • Test cases are in a specifically named folder.
  • The target folder is used for the final JAR file.

Let’s look at a project structure a get a basic idea about the Maven and the POM file.

Understanding Maven — Sample Demo

To get a basic idea about Maven we will create a simple Maven project using the spring initializer. You can either use it with an IDE like IntelliJ or use the start.spring.io website. Here, I will use the start.spring.io website since Maven is IDE-independent.

start.spring.io

Here, you can see that for the artifact name we have “demo” and for the Java version, I have selected Java 8. And for dependencies, I have added Spring Web, Spring Data JPA, and Rest Repositories. Now click on Generate button and a zip file will be downloaded. Extract the zip file and open it using an IDE of your preference. I will use IntelliJ and I will open this using the pom.xml file.

It will take few seconds to sync and after that, you can see the following project structure.

The project opened in IntelliJ

Here, in the opened pom.xml file you can see the specifications you specified during the project initialization in the spring initializer.

Understanding Maven — Maven Life Cycle

If you open the side menu button named Maven, you can see the phases in Maven Life Cycle. There are nine important phases in the Maven Life Cycle.

  • clean — clear the target directory into which Maven normally builds your project
  • validate — validate the project is correct
  • compile — compile the source code of the project
  • test — test the compiled source code using a unit testing framework
  • package — package the compiled code
  • verify — run any checks to verify the package is valid
  • install — install the package into the local repository
  • deploy — copies the final package to a remote repository
  • integration-test(not in this project) — deploy the package into an environment where integration tests can be run
Maven Life Cycle

As plugin goals can be attached to each lifecycle phase, Maven executes the goals attached to each phase. And a phase can have zero or more goals. For example, if you open the terminal inside the IDE and type mvn install then multiple goals are executed. For now, let’s skip the tests phase and run Maven inside our IDE. To do that open the terminal and type mvn clean install -DskipTests

mvn clean install -DskipTests

When you run that you will find that all the dependencies which are mentioned in the pom.xml file are downloaded into your project and the project is bundled into a .jar file(in target folder).

You can find the bundled file in the target folder as well as in the .m2 folder. You can go to the .m2 folder by going to C:\Users\<USER_NAME>\.m2

Understanding Maven — Maven Repositories

When it comes to Maven there are two repositories, The Maven Central Repository, and the local .m2 repository.

Looking at the Maven Central Repository, you can visit the site by using search.maven.org It is the one having all the dependencies that you have mentioned in the pom.xml file.

Maven Central Repository

And the .m2 repository is the local repository that is in your local machine. So, when you run mvn clean install command which we run earlier, the local machine connects to the Maven Central and downloads all the dependencies you mentioned in the pom.xml to your local .m2 repository. And after that, those dependencies are added to the External Libraries folder in your project, and hence solving the problem of dependency management.

Understanding pom.xml

As we mentioned above, the pom.xml file contains all the information about a project. Even if you don’t have any dependencies added, you can create the pom.xml file and add your dependencies later. The simplest pom.xml file that you can have, looks like this.

The <groupId></groupId> , <artifactId></artifactId> and, <version></version> make up the project. Therefore they are essential when you are creating a Maven project. These are often mentioned as project coordinates.

The pom.xml file is an XML file(Extensible Markup Language) that has syntax similar to HTML. And if you are having multiple projects inside a Maven project all of those projects extend the super POM automatically.

When you are adding dependencies to your pom.xml file, you have to add them to the group named <dependencies></dependencies> A simple example of that is shown below.

When using dependencies you can use <scope></scope> tag to define the scope you want that dependency to run on. For example, JUnit dependency can have <scope>test</scope> since it can be used for the test phase. There are other scopes like compile, provided, runtime and, system.

  • compile — default scope
  • provided — when JDK is expected to provide them
  • runtime — for executing and testing, not compiling
  • test — when testing, not required during normal operation
  • system — similar to “provided”, but must specify the explicit path to the JAR on a local file system

To learn more extensively about the scopes read the article given below.

https://howtodoinjava.com/maven/maven-dependency-scopes/

Now, let’s look at Maven plugins. A plugin is a collection of one or more goals and a goal is a unit of work in Maven. There are many core plugins like the JAR plugin, Compiler Plugin, Surefire plugin, etc. In addition, Maven allows the users to create custom plugins too. To see the full list of the core plugins, go to https://maven.apache.org/plugins/index.html

Maven core plugins

For example, to use the compiler plugin, you can type mvn compiler:compile Here, compile is the given goal.

mvn compiler:compile

These are the basic concepts that you should know when using Maven. When it comes to writing the pom.xml file using XML, you can always follow the documentation to get an understanding of the syntax.

Cheers!

--

--

Nipuna Upeksha
Javarevisited

Software Engineer | Visiting Lecturer | AWS SAA | M.Sc. in Big Data Analytics