Maven Introduction

Aug 02, 2023 by SkilledEngg

In this article, I have explained about maven in very simple language. This article is intended for begineers. This article is part of a series on topic Maven Complete Understanding. If you are really interested to learn Maven in depth from basic to advanced. Please go through all the articles of this series. You can start from here.


Table of Contents:

  1. What is Maven
  2. Importance of Maven
  3. Dependency Resolution
  4. Dependencies Configuration
  5. Repositories Configuration
  6. Optimisation While Downloading Dependencies

What is Maven

For every project, after writing the code, we perform some certain steps like:

  • Compiling the project
  • Testing the project (ie running the tests of the project)
  • Packaging the project
  • Deployment of the project using the created package

Since, these are the fixed and redundant steps we need to perform each time whenever we run any application.

To automate these redundant processes, build tools came into picture. Maven is one of them, which is widely used in the current industry everywhere from the small startups to the big corporates.

Importance of Maven

So let’s see now, what automation Maven is providing us?
Since, these above steps are more or less required each time. For that, we have two options for it:

Scenario 1:

Where we are not using Maven or any other build tool. Then we perform all of these required steps manually one by one.

Example:

  1. First we need to download the required dependencies manually one by one from the internet and add those into the Java path.
  2. Then we manually compile the project running a command.
  3. Once it is success, we execute another command to run the unit tests.
  4. Then if those are success then we execute one more command to run the integration tests.
  5. After success of the above commands, we execute command to package the application.
  6. Now, we will wait for the packaging to happen, after completion of that we will copy the packaged file from the target folder to the application server/web server. (whichever we are using).
  7. Then we run a command now to manually start up the server.
  8. After these many steps, our application will be up and running to serve the traffic.

In addition to above, sometimes in some of the organisations , we perform these below steps also:

  1. In case we want to generate reports of the executed test cases, then we also execute one more command.
  2. Then if in your organisation, it is uploaded to some central location, then we will have to run that command also which will upload the generated reports to a remote location.

Scenario 2:

The another option, where we are using MAVEN then we do not need to perform any of the above things manually. We will just run only one command and MAVEN will do all for us.

NOTE: In option 1, these tasks are the very less thing what MAVEN actually does for us.

Now, let’s discuss each and every task in detail, whatever MAVEN does for us.

Dependency Resolution

First of all, To build and run our project successfully we need dependencies. MAVEN does that job for us and it downloads all the required dependencies from internet.

Ques: What are dependencies?
Ans: In brief, a dependency is nothing but simply a JAR file used by our Java application.

But if you want to learn in-depth about dependencies, types of dependencies, transitive/optional dependencies etc, I have explained all these concept in this article in detail. Please go through that article and then resume this article.

Ques: How does MAVEN know, which all dependencies our project needs?
Ans: We are the one who specify the required dependencies in the maven configuration file. From that configuration file only MAVEN knows about the required dependencies.

Ques: How do we tell MAVEN about the required dependencies for our project?
Ans: We define the required dependencies in one configuration file, which is pom.xml. MAVEN always looks for this particular file in our project for each configuration. Dependency configuration is one of the configuration we mention in this file. So, this configuration file is like heart of the MAVEN and it is a must to keep this file in a MAVEN project. Otherwise MAVEN will not be able to find the configurations and it will not function as expected.

Till now, we have defined required dependencies in pom.xml file and MAVEN has read those. But now the question is: MAVEN does not know where these dependencies are kept on the entire internet.

Ques: How MAVEN will know, exactly from where it needs to download these dependencies?
Ans: Obviously, it can not search the dependencies on entire internet. It needs some location where it is present. So, that specific location is also provided by us in the same pom.xml file.

Okay, so till now MAVEN knows what dependencies it has to download and where it has to search on the internet. Great!

Now, let’s assume a scenario that MAVEN is downloading one dependency, which is nothing but a jar file. But inside that jar file it is using some other library’s code. So, that library is another jar file.

Example:
We need logutil.jar file, but it needs another library called consoleprint.jar

How, that will be resolved now, since we can’t know always about these libraries which are required by our jar file. Also, we have just defined the logutil.jar in the pom.xml configuration file. We can think of it like dependency of dependency.

Ques: How dependency of dependency is resolved by MAVEN?
Ans: In such scenarios, where primary dependency requires some other dependency then MAVEN itself handles this by downloading all those secondary(indirect) dependencies also.

These secondary, tertiary.. dependencies are called transitive dependencies. Learn here more about transitive dependencies.

Now, we will go deeper into technical understanding but before going deeper, I would suggest to download and install Maven first if you have not installed MAVEN yet on your local. So that you can learn the concepts by doing practical along with reading the article. Please go through these steps to download & install MAVEN on your system.

Till here, we have understood the concept. Now, we will understand how to configure dependencies in the pom.xml file.

Dependencies Configuration

Since, this is an xml file. So, the structure of the file will be an xml only. Where we will have opening and closing tags.

To define configurations related to dependencies we use <dependencies> tag.
Ex: <dependencies> </dependencies>

Inside this tag, we define all the required dependencies one by one. For each dependency, we use <dependency> tag.
Ex: <dependency> </dependency>

So, if we have multiple dependencies then it will look something like this: <dependencies> <dependency></dependency> <dependency></dependency> <dependency></dependency> </dependencies>

Now, inside each <dependency> tag, we define some details about the library which we need for our project.
Ex: <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.2</version>

Here, MAVEN uniquely identifies any dependency using combination of groupId and artificatId.
groupId tells about the organisation, who owns this particular library.
artificatId is the library’s name.

We need to use both groupId and artificatId. Since, one organisation can publish multiple libraries. And multiple organisations can publish library with the same name. So, we can not skip any of the tag here.

Let’s talk about third tag which is version. Well, any library itself is nothing but a code written and maintained by some developers or community. In which there will be continuous feature additions. Because of that, there will be new versions coming up regularly. So, we use <version> tag to specify which version we need in our project.

So, collectively the entire dependencies tag will look like this: <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.7.2</version> </dependency> </dependencies>

In this example, we have specified 2 dependencies.

Repositories Configuration

Till here, we have learned how to configure the required dependencies. Now, we will define the location on the internet where MAVEN will look to download these dependencies.

The location on the internet, where these libraries are kept is called repository.

If we don’t define anything in our pom.xml file then MAVEN uses it’s default remote repository location. Which is hosted on http://repo1.maven.org/maven2. This is also called maven central repository.

In some rare cases, some libraries are not present at the default location. For such scenarios, we use other repositories.
So, if we don’t want to use the default remote repository location and we want to override this or use some other location. Then we can do so by defining the <repositories> tag in the pom.xml file. Here in this tag we can define any number of such remote locations.
Ex: <repositories> </repositories>

To specify the path of each location, we use <repository> tag inside <repositories> tag. Ex: <repository> </repository>

So, finally it will look like this: <repositories> <repository></repository> <repository></repository> </repositories>

Ex: <repositories> <repository> <id>java-net-repo</id> <url>https://maven.java.net/content/repositories/public/</url> </repository> <repository> <id>jboss-repo</id> <url>http://repository.jboss.org/nexus/content/groups/public/</url> </repository> <repository> <id>spring-repo</id> <url>https://repo.spring.io/release</url> </repository> </repositories>

The above is the example of defining multiple repositories in the POM.xml file.
In case of multiple repositories, MAVEN first searches on the default central repository, if it does not find the library there then it searches one by one sequentially on the repository locations defined inside the <repositories> tag.

Sometimes, while working on a project in an organisation, some dependencies are kept hosted at some internal repository.
Such repository is called remote repository. These repositories can be accessed by the employees of that organisation only and in the private network of that organisation only.

Optimisation While Downloading Dependencies

Let’s talk about one dependency which is needed in multiple project.
So according to our understanding till now, MAVEN will have to download this dependency again and again for each project. But, this is not an optimised way.

MAVEN optimises this process by creating one offline repository on our local computer. Which is located at ~/.m2/repository location. At this location MAVEN keeps all the libraries which has been downloaded till now.
So, when new project requires any dependency which already exists at ~/.m2/repository location, MAVEN will not download that again and it will use the existing one.

I hope you would have learned something new from this article. This article is part of Complete Maven Understanding Series. To learn further things about Maven in detail, please go through the entire article series.

Still, if you have any feedback/suggestion/concern, please write us.

Leave a Comment