Maven is a tool for automating the build process in Java, which uses POM (Project Object Model) as its foundation. Pom.xml is a configuration file that includes dependencies of the project, build and source directories, plugins, and other details of the project. Here is an example of the pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId> com.maven.example </groupId><artifactId>app</artifactId><version>1</version><dependencies><dependency><groupId>software.amazon.awssdk</groupId><artifactId>s3</artifactId><version>2.16.43</version><scope>test</scope></dependency></dependencies></project>
In this file, the minimal configuration requirements are:
project: root tag
modelVersion: should be set to 4.0.0 when using maven 2 and 3
groupId: the project group id, i.e., the name of the organization that created the project
artifactId: id of the current project, i.e., a unique name for the application
version: the version of the project under the given company
The other tags shown in the pom.xml
file are straightforward. The dependencies tag contains all the different dependencies that the project might have, with each one enclosed within a dependency tag. The project associated with this example pom.xml
file has a dependency on the AWS S3 service. To search for the specific groupId, artifactId, and other details of your project’s dependencies, you can go to mvnrepository and search for it. It gives you the entire configuration to add in your pom.xml
file. Then, once your project is built, these dependencies are automatically downloaded for your project – you don’t have to do anything manually!
Maven provides 3 types of repositories for dependency management. These are, Local, Remote, and Central Repository.
Local Repository: is a directory on the developer’s machine
Remote Repository: is a repository on a remote web server
Central Repository: is the main Maven Repository
Let’s now look at the benefits of using Maven and how it makes the life of Java developers easier.
Building Java projects traditionally requires:
Maven is used to automating all of these tasks! It can be used from the command line or can be integrated into IDEs (such as Eclipse or IntelliJ IDEA). Maven simplifies all the steps needed to build a Java project by:
pom.xml
fileNote: Maven has a feature that even manages transitive dependencies.
What this essentially means is that if you add a dependency that has needs dependencies to work, you don’t have to explicitly mention these as Maven automatically downloads them for you. This: