Maven Tutorial
Maven tutorial provides basic and advanced concepts of apache maven technology. Our maven tutorial is developed for beginners and professionals.
Maven is a powerful project management tool that is based on POM (project object model). It is used for projects build, dependency and documentation.
It simplifies the build process like ANT. But it is too much advanced than ANT.
Current version of Maven is 3.
Understanding the problem without Maven
There are many problems that we face during the project development. They are discussed below:1) 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.
2) Creating the right project structure: We must create the right project structure in servlet, struts etc, otherwise it will not be executed.
3) Building and Deploying the project: We must have to build and deploy the project so that it may work.
What it does?
Maven simplifies the above mentioned problems. It does mainly following tasks.
To install maven on windows, you need to perform following steps:
Download Maven latest Maven software from Download latest version of Maven
For example: apache-maven-3.1.1-bin.zip
Extract it. Now it will look like this:
Right click on MyComputer -> properties -> Advanced System Settings -> Environment variables -> click new button
Now add MAVEN_HOME in variable name and path of maven in variable value. It must be the home directory of maven i.e. outer directory of bin. For example: E:\apache-maven-3.1.1 .It is displayed below:
Now click on OK button.
Here, we have installed JDK and its path is set by default, so we are going to append the path of maven.
The path of maven should be %maven home%/bin. For example, E:\apache-maven-3.1.1\bin .
Now it will display the version of maven and jdk including the maven home and java home.
Let's see the output:
- 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
- Builds
- Documentation
- Reporing
- SCMs
- Releases
- Distribution
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
How to install Maven on windows
You can download and install maven on windows, linux and MAC OS platforms. Here, we are going to learn how to install maven on windows OS.To install maven on windows, you need to perform following steps:
- Download maven and extract it
- Add JAVA_HOME and MAVEN_HOME in environment variable
- Add maven path in environment variable
- Verify Maven
1) Download Maven
To install maven on windows, you need to download apache maven first.Download Maven latest Maven software from Download latest version of Maven
For example: apache-maven-3.1.1-bin.zip
Extract it. Now it will look like this:
2) Add MAVEN_HOME in environment variable
Now add MAVEN_HOME in variable name and path of maven in variable value. It must be the home directory of maven i.e. outer directory of bin. For example: E:\apache-maven-3.1.1 .It is displayed below:
Now click on OK button.
3) Add Maven Path in environment variable
Click on new tab if path is not set, then set the path of maven. If it is set, edit the path and append the path of maven.Here, we have installed JDK and its path is set by default, so we are going to append the path of maven.
The path of maven should be %maven home%/bin. For example, E:\apache-maven-3.1.1\bin .
4)Verify maven
To verify whether maven is installed or not, open the command prompt and write:- mvn −version
Let's see the output:
Maven Repository
A maven repository is a directory of packaged JAR file with pom.xml file. Maven searches for dependencies in the repositories. There are 3 types of maven repository:- Local Repository
- Central Repository
- Remote Repository
Local repository then Central repository then Remote repository.
If dependency is not found in these repositories, maven stops processing and throws an error.
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\SSS IT\.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: E:\apache-maven-3.1.1\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/settings-1.0.0.xsd">
- <!-- 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>
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/settings-1.0.0.xsd">
- <localRepository>e:/mavenlocalrepository</localRepository>
- ...
- </settings>
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
- <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.javatpoint.application1</groupId>
- <artifactId>my-application1</artifactId>
- <version>1.0</version>
- <packaging>jar</packaging>
- <name>Maven Quick Start Archetype</name>
- <url>http://maven.apache.org</url>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
You can search any repository from Maven official website mvnrepository.com.
No comments:
Post a Comment