Here we will learn, how to install Maven on windows or local machine for java build and dependency management.
How To Install Maven on Windows
There are few steps to install Maven in our local machine. These are as follows:
- Go to google and type -> download maven
- Download zip file of maven. [Download]
- Go to download folder -> go to the folder where bin folder is available ->
Copy path -> Go to my computer property -> go to system environment variables ->
Click on New -> Variable name – MAVEN_HOME
Variable value – paste the copied path (path of till bin folder)
4. Open path of system variables, edit it and add below the line.
- Verify Maven:Open command prompt and check maven version using below command-
Now it will display version of maven and other details:
Table of Contents
What is Maven and It’s Components?
In this section, we will learn what is build tool, what is maven and it’s components.
What is Build Tool?
The build tool is used to set up everything which is required to run your java code independently. It generates source code, compiling the code and packaging the code into a jar.
Maven:- Maven is a build tool used primarily for Java projects. It is also a powerful project management tool that is based on POM (Project Object Model). It is used for project build, dependency and documentation.
Apache Maven is an advanced build tool to support the developer at the whole development process of a software project. Maven allows you to automate the process of the creation of the initial folder structure for the Java application, performing the compilation and the packaging of the project.
Different Concepts of Maven:
POM (Project Object Model):- It is an XML file that contains information about the project and configuration details such as dependencies, source directory, plugin, goals etc. used by Maven to build the project. Other information such as the project version, description, developers, and mailing lists also be specified.
Dependencies and Repositories:- Dependencies are external Java libraries required for Project and repositories are directories of Packaged JAR files. The local repository is present in our local machine in .m2 folder. If dependencies are not available in local maven repository (.m2 folder) then maven downloads them from the central maven repository.
Build life Cycles, Phases, and Goals:- A sequence of steps to execute the task in maven is called maven build lifecycle.
Build Profile:– It is a set of configuration values, which can be used to set or override default values of maven build. We can add this profiles in POM file under profiles element tag.
Build Plugin:– It is used to perform some specific goal. Actually, every task is done by plugins. For example, a java program can be compiled with the maven-compile-plugin’s by running the following command.
Maven Build Life Cycle
It is a well-defined sequence of phases, which defined the order in which the goals to be executed.
In Maven, every build has a life cycle which is called build lifecycle. It contains different phases. I have mentioned some main phases below:
Validate: Validates if the project is correct and if all necessary information is available in the project.
Compile: Source code compilation is done in this phase.
Test: In this phase, actually we run/execute the compiled source code.
Package: When successfully done compilation and run of the source code, it packaged the code the JAR, WAR or EAR format.
Install: Install the jar files onto local maven repository which can be used as a dependency in other projects locally.
Deploy: Copy the final package to the remote repository for sharing with developers and projects.
A maven repository is a directory where all jars files (dependencies) and other plugins which are configured as part of any project are stored so it can easily access by Maven.
There are 3 types are a repository in maven.
Local Repository: It is located in our local machine. When we run any maven command for the first time then it gets created.
This repository keeps your project’s dependencies (library jars, plugins jar etc.). When you run a Maven build, then maven automatically download all dependencies jars into a local repository.
Central Repository: This repository is provided by maven community itself. This repository contains a large set of commonly used for any Java project. This is located on the web so an Internet connection is required if developers want to make use of this central repository.
Remote Repository: Sometimes, Maven does not find some jars in the central repository as well so, to prevent this situation, Maven provides the concepts of Remote Repository, which is developer’s own custom repository containing required libraries or other projects jars.
Search Sequence: Firstly search dependency in the local repository, if not found then search the dependency into Central Repository, if not found then search dependency into the remote repository.
Maven provided the following two types of Plugins –
Build plugins: These plugins are executed at the time of build process. These plugins should be declared inside the <build> element.
Reporting plugins: These plugins are executed at the time of site generation. These plugins should be declared inside the <reporting> element.
Maven POM: pom stands for Project Object Model. It is an XML file which contains information about project and configuration information such as dependencies, build directory, source directory, plugin, goals etc. for the maven.
While executing the task, maven took the information form the POM.xml file and execute the task.
Main elements of Maven projects
Project: This is the root tag in which we specify the basic schema setting.
ModelVersion: It should be 4.0.0
GroupId: This is an Id of project’s group. This is always unique.
ArtifactId: This is an Id of the project. This is the general name of the project.
Version: This is the version of the project. It specifies the version of the artifact under the given group.
<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”>