Introduction to Maven - Part 1

Deniz GÜRSOY
3 min readApr 14, 2021

If you are a Java developer, you probably use and hear about Maven a lot. But what is it? It might be hard to understand what it does and how it does it at the first glance. This can be an introduction to Maven for you.

First rule of learning Maven like learning anything else is to believe that

Maven is easy. There is nothing to be afraid of it.

We will learn step by step what Maven is and what it does. Let’s start…

What is Maven?

Maven is a build tool and dependecy manager. Maven does mainly two jobs for you. It builds your project in the packaging format (jar, war, pom, etc.) that you specify in project information part and fetch the dependencies your project needs.

How does the maven know all these?

Maven has a configuration file where it stores all the information about your project. That file is pom.xml. A Maven project basically has the following structure:

    src
├───main
│ ├───java
│ └───resources
├───test
│ ├───java
│ └───resources
└───pom.xml (this is the magic file)

Let’s understand the folder structure together. As the names imply, the main folder contains the actual source code of your project and the test folder contains test codes of your project. Simple!

If you paid attention, main and test folders contain two folders with the same names - java and resources folders. Java folders contain source code file and resources folders contain other files used by your project. A great example to resource file is application.properties file which Spring’s application settings are written. Test folder has java folder because our test codes are also java codes and since test codes may use some resources, it has a resources folder too.

What is inside pom.xml?

Pom.xml looks complicated but it is not! We will analyze every part of it together one by one. Pom.xml contains four parts namely project information, properties, dependencies and the build.

Let’s dive deep into project information part:

Every Maven project is identified by three pieces of information - groupid, artifact id and version. These three values can be any string value but you should follow industry standarts. Group id defines maintainer of the project. Artifact id is the name of project and the version is the current version of the project. You can also set packaging by using packaging tag.

<groupId>org.example</groupId>
<artifactId>sample</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

Properties part allows you to define variables which you can use inside pom.xml. In the example below, I declared apache-commons-verison property which i will use later in dependency part.

<properties>
<apache-commons-verison>3.11</apache-commons-verison>
</properties>

Dependencies tag is the place where you list all dependencies your project uses. Every dependency is written inside a new dependency tag. You should write project information of the dependency inside a new dependency tag. Maven will bring all dependecies that you listed here for you. We will discuss how Maven finds dependencies in next parts.

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache-commons-verison}</version>
</dependency>
</dependencies>

Note: In the version part of the dependency, I used the property declared in the properties part. Property name should be wrriten between ${ } in the places where you want to use it.

In the example above, The sample project I created is dependent to commons-lang3 jar maintained by Apache. After adding the dependency to project’s pom, I was able to use classess in commons-lang3 in my Maven project.

Lastly, we have the build part. In the build part you can list all the plugins that you want to execute during the build. You can do various jobs with the plugins. You can move files or zip contents etc. We will cover plugins in the next parts in details.

<build>
<plugins>

</plugins>
</build>

--

--