Episode 0: Maven

Episode 0: Maven

Introduction to Maven

When developing a Java application, we may require extra JARs such as JUnit, JDBC, Spring, Hibernate, Commons Logging, JSON and so on. One solution to that is to manually download each JAR from their website and add them to your build path/classpath.

JAR or Java ARchive is a file format used to package Java applications and their resources for the purpose of distribution.

Build path is the path where the IDEs look for class files, resources and other dependencies to build the project.

Classpath is the path where JVM looks for the class files to run the Java program.

Maven, on the other hand, offers a better solution. It will download and make the JARs available during compile time or runtime automatically.
Since these JARs are the dependencies that Maven requires to build the project, they are often called Maven dependencies or dependencies for short.

How Maven works?

For Maven to download the dependencies, It needs a config file. By default, this config file is called pom.xml. This pom.xml file contains the list of dependencies that are required to build our project.
Maven scans the pom.xml file and searches for the dependencies in the local repository. The local repository exists in a specific folder on our machine.

By default, the Local Maven Repository is located in a folder named m2
For Unix or MacOS it exists at ~/. m2/repository
For Windows, it exists at C:\Users\{your-username}\.m2\repository

If we don't have the dependencies in the local repo then Maven will download them from the Maven Central Repository that exists on the internet. Before loading the dependencies in our project, Maven saves these dependencies into the local repo for future use. Finally, these dependencies are loaded into our project.

When we build and run the app. Maven will handle the classpath / build path for us. Based on the config file i.e. pom.xml, Maven will add the dependencies automatically.

The POM.xml file

POM or Project Object Model file is a configuration file for our project. This is located at the root of the Maven project.

A Quick Look into the pom.xml

The pom.xml file contains the following:

  1. Project Metadata: It contains the Project name, Version and some more information about the project.

  2. Properties: It contains the properties that can be accessed anywhere in the pom.xml using the ${java.version} syntax where java.version is the property name. The properties can also be used as a default value for the plugins.

  3. Dependencies: List of dependencies such as Spring, Hibernate etc

  4. Plugins: Additional custom tasks that we want to run. For example, generate JUnit test reports etc.

Project coordinates

Project coordinates are used to uniquely identify a project (or dependency).
Project coordinates contain -

  1. Group ID - Name of the company, group or organization. The convention is to use the domain name in reverse order.

  2. Artifact ID - Name of the project.

  3. Version - A specific release version like 1.0
    If the project is still under development then 1.0-SNAPSHOT.

To add a dependency we need Group ID and Artifact ID but the version is optional. However, it is a best practice to include the version as well.
This may be referred to as GAV (Group ID, Artifact ID and Version)

Below is an example of project coordinates for JUnit

To find the project coordinates of a dependency, we can either visit the JAR's website or visit the Maven Central Repository and search for the dependency.
👇 Project coordinate from the maven central repository for JUnit5 with version 5.9.3.

Transitive Dependencies

When Maven retrieves a project dependency. It will also pull the supporting dependencies. For example: If we need Spring as a dependency and Spring needs commons-logging as a dependency then maven will download them both to build our project.

Standard Directory Structure

Maven uses a standard directory structure to keep the files organized.

☝️ Above is an example of the standard directory structure.
👇 Below is the description of the directories.

DirectoryDescription
src/main/javaJava source code
src/main/resourcesProperties & Config files used by our app
src/test/javaUnit testing code
src/test/resourcesUnit testing properties & Config file
targetDestination directory for compiled code. Automatically created by Maven.

Because of this standard directory structure, Maven projects are easily shareable between IDEs.

Maven Wrapper

Maven wrapper files allow us to run the Maven project even if we don’t have Maven installed or present on our path. Also, If the correct version of Maven is not found on the machine then it would automatically download the correct version and runs Maven.

For MacOS & Linux the Maven wrapper file is mvnw
For Windows the Maven wrapper file is mvnw.cmd

Example:
To clean the previous build (that just deletes the target folder) using the maven command we can use the command mvn clean

Suppose, we don't have Maven installed and want to run the same command then we can use the Maven wrapper

  • For Windows: ./mvnw.cmd clean

  • For MacOS & Linux: ./mvnw clean

Note that these commands should be run from the root directory, where mvnw files are present.

TL;DR

Maven is a Project management and build tool. It helps us manage and load the dependencies into our project as needed. It also helps us in building our Project. Maven projects are IDEs independent, meaning we can load them in any IDE of our choice and start working on the project.

This episode only serves as a starting point. As we progress through the series, we will learn a few more things about Maven and its command.

Peace Out ✌️