Saturday, January 19, 2019

MAVEN Tutorial in Hibernate ..!!

Maven Tutorial

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.
  1. It makes a project easy to build
  2. It provides uniform build process (maven project can be shared by all the maven projects)
  3. It provides project information (log document, cross referenced sources, mailing list, dependency list, unit test reports etc.)
  4. It is easy to migrate for new features of Maven
Apache Maven helps to manage
  • 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:
  1. Download maven and extract it
  2. Add JAVA_HOME and MAVEN_HOME in environment variable
  3. Add maven path in environment variable
  4. 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:
maven structure

2) Add MAVEN_HOME in environment variable

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:
maven home directory 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 .
maven path

4)Verify maven

To verify whether maven is installed or not, open the command prompt and write:
  1. mvn −version  
Now it will display the version of maven and jdk including the maven home and java home.
Let's see the output:
maven verification 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:
  1. Local Repository
  2. Central Repository
  3. Remote Repository
Maven searches for the dependencies in the following order:
Local repository then Central repository then Remote repository.
maven repositories 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.
maven local repository

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
  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/settings-1.0.0.xsd">  
  5.   <!-- localRepository  
  6.    | The path to the local repository maven will use to store artifacts.  
  7.    |  
  8.    | Default: ${user.home}/.m2/repository  
  9.   <localRepository>/path/to/local/repo</localRepository>  
  10.   -->  
  11.   
  12. ...  
  13. </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/settings-1.0.0.xsd">  
  5.    <localRepository>e:/mavenlocalrepository</localRepository>  
  6.     
  7. ...  
  8. </settings>  
As you can see, now the path of local repository is e:/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
  1. <project xmlns="http://maven.apache.org/POM/4.0.0"   
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  5.   
  6.   <modelVersion>4.0.0</modelVersion>  
  7.   
  8.   <groupId>com.javatpoint.application1</groupId>  
  9.   <artifactId>my-application1</artifactId>  
  10.   <version>1.0</version>  
  11.   <packaging>jar</packaging>  
  12.   
  13.   <name>Maven Quick Start Archetype</name>  
  14.   <url>http://maven.apache.org</url>  
  15.   
  16.   <dependencies>  
  17.     <dependency>  
  18.       <groupId>junit</groupId>  
  19.       <artifactId>junit</artifactId>  
  20.       <version>4.8.2</version>  
  21.       <scope>test</scope>  
  22.     </dependency>  
  23.   </dependencies>  
  24.   
  25. </project>  

You can search any repository from Maven official website mvnrepository.com.

No comments:

Post a Comment