Maven Tutorial
Maven Tutorial
Maven is primarily used for Java projects, but it can also be used to build and manage projects
written in C#, Ruby, Scala, and other languages. Standardization: Maven provides a standard
way to build and manage projects, which makes it easier to share code and collaborate with
other developers.
If you are working on a Java project, I highly recommend using Maven. It is a powerful tool
that can make your life as a developer much easier.
There are many problems that we face during the project development. They are discussed
below:
Adding set of Jars in each project: In case of struts, spring, hibernate frameworks, we need
to add set of jar files in each project. It must include all the dependencies of jars also.
Creating the right project structure: We must create the right project structure in servlet,
struts etc, otherwise it will not be executed.
Building and Deploying the project: We must have to build and deploy the project so that it
may work.
• Project Object Model (POM): The POM is a central file that contains information about
the project, such as its dependencies, build configuration, and reporting information.
• Dependency Management: Maven can automatically download and manage the
dependencies of your project. This saves you time and effort, and it ensures that your
project is always using the latest versions of its dependencies.
• Build Automation: Maven can automate the build process for your project. This includes
tasks such as compiling the code, running unit tests, and packaging the project.
• Reporting: Maven can generate reports about your project, such as code coverage reports
and unit test reports. This can help you to track the progress of your project and to identify
areas that need improvement.
• Documentation: Maven can generate documentation for your project, such as Javadocs
and project website. This can help you to share your project with others and to make it
easier for them to understand how it works.
A build tool takes care of everything for building a process. It does following:
Eclipse integrated with Maven by default i.e. Not required any extra Maven setup inside
Eclipse.
1. Open Eclipse
2. File -> New -> Maven Project
pom.xml: This is the Project Object Model (POM) file, which defines the project's
configuration, dependencies, and other settings.
src/main/java: This directory contains the main Java source code of your project.
src/test/java: This directory contains the test Java source code.
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zomato</groupId>
<artifactId>zomato</artifactId>
<version>0.0.1-SNAPSHOT</version>
</project>
• Inside above pom.xml file, we are going to add a section called as dependencies. Inside
dependencies we are adding individual dependency for every required jar files.
So, here we are not downloading jar files manually and not configuring with our project.
Hand overing this to Maven tool.
Example Dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zomato</groupId>
<artifactId>zomato</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.9</version>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11</artifactId>
<version>23.2.0.0</version>
</dependency>
</dependencies>
</project>
• Now observe under Maven Dependencies Section. All specified jar files and internal
dependent jar files are downloaded in project.
A Maven goal is a task that can be performed during the build process. Maven goals
are defined in the project's POM file.