Maven tutorial

Apache Maven is a powerful Build Automation and Project Management Tool used primarily for Java projects. It helps in the management of a project's build lifecycle, dependency management, and documentation the overall project structure. Maven uses a Project Object Model (POM) file to define the structure of a project, including its dependencies, build settings, plugins, and more in the pom.xml file.

The Current version of Maven is 3.9.6 [Check to all software versions URL]

Main features of Maven following tasks and manage to helps:

  • Builds
  • Dependency
  • Documentation
  • Reporting
  • SCMs [Version Control System Tools]
  • Releases
  • Distribution

 

  • It makes a project easy to build
  • It provides uniform build process (maven project can be shared by all the maven projects)
  • It provides project information (log document, cross referenced sources, mailing list, dependency list, unit test reports etc.)
  • It is easy to migrate for new features of Maven

Dependency Management: Maven manages project dependencies by fetching libraries and dependencies from remote repositories, making it easier to handle project dependencies and ensuring that the required libraries are available during the build process.

Build Lifecycle Management: Maven provides a defined build lifecycle consisting of phases (such as compile, test, package, install, and deploy) that facilitate the build process. Developers and testerss can execute various goals (tasks) associated with each phase.

Consistent Project Structure: Maven follows a convention over configuration approach, enforcing a standard project directory layout. This standardization simplifies project navigation and enhances collaboration among team members.

Plugins: Maven is extensible and supports a wide range of plugins, enabling developers to perform various tasks such as compiling code, running tests, creating documentation, and more, without requiring manual configuration.

To work with Maven, developers typically create a POM file that specifies project information, dependencies, build configurations, and other settings. Maven uses this POM file to manage the project throughout its lifecycle, including building, testing, packaging, and deployment.

Overall, Maven simplifies the software development process by providing a consistent and structured approach to project management and build automation in Java and other related technologies.

What is Build Tool

A build tool takes care of everything for building a process. It does following:

  • Generates source code (if auto-generated code is used)
  • Generates documentation from source code
  • Compiles source code
  • Packages compiled code into JAR of ZIP file
  • Installs the packaged code in local repository, server repository, or central repository.

Maven Repository:

A Maven repository is a directory of packaged where all the project jars, library files, plugins, and other project dependencies file with pom.xml file are stored and managed.

Maven searches for dependencies in the repositories. There are 3 types of maven repository.

  1. Local Repository
  2. Central Repository
  3. Remote Repository

Maven searches for the dependencies in the following order:

Local Repository -> Central Repository -> Remote Repository

1) Maven Local Repository

Maven local repository is located in your local system. It is created by the maven when you run any maven command.

By default, maven local repository is %USER_HOME%/.m2 directory. For example: C:\Users\Techlearn.in\.m2.

Update location of Local Repository

We can change the location of maven local repository by changing the settings.xml file. It is located in MAVEN_HOME/conf/settings.xml, for example: D:\apache-maven-3.9.6\conf\settings.xml.

Let's see the default code of settings.xml file.

settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"   

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   

   xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/sett...">  

  <!-- localRepository  

   | The path to the local repository maven will use to store artifacts.  

   |  

   | Default: ${user.home}/.m2/repository  

  <localRepository>/path/to/local/repo</localRepository>  

  -->  

</settings>  

Now change the path to local repository. After changing the path of local repository, it will look like this:

settings.xml

  1. ...  
  2. <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"   
  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  4.    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/sett...">  
  5.    <localRepository>d:/mavenlocalrepository</localRepository>  
  6.     
  7. ...  
  8. </settings>  

As you can see, now the path of local repository is D:/mavenlocalrepository.

2) Maven Central Repository

Maven central repository is located on the web. It has been created by the apache maven community itself.

The path of central repository is: http://repo1.maven.org/maven2/.

The central repository contains a lot of common libraries that can be viewed by this url http://search.maven.org/#browse.


3) Maven Remote Repository

Maven remote repository is located on the web. Most of libraries can be missing from the central repository such as JBoss library etc, so we need to define remote repository in pom.xml file.

Let's see the code to add the jUnit library in pom.xml file.

pom.xml