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.
- 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
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
Difference between Ant and
Maven
Ant and Maven both are build tools
provided by Apache. The main purpose of these technologies is to ease the build
process of a project.
There are many
differences between ant and maven that are given below:
Ant
|
Maven
|
Ant doesn't has formal
conventions, so we need to provide information of the project structure
in build.xml file.
|
Maven has a convention to
place source code, compiled code etc. So we don't need to provide information
about the project structure in pom.xml file.
|
Ant is procedural, you need
to provide information about what to do and when to do through code. You need
to provide order.
|
Maven is declarative,
everything you define in the pom.xml file.
|
There is no life cycle in
Ant.
|
There is life cycle in
Maven.
|
It is a tool box.
|
It is a framework.
|
It is mainly a build tool.
|
It is mainly a project
management tool.
|
The ant scripts are not reusable.
|
The maven plugins are reusable.
|
It is less preferred than
Maven.
|
It is more preferred than
Ant.
|
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
Maven searches for the dependencies in the following order:
Local repository then Central repository then Remote repository.
If dependency is not found in these repositories, maven stops processing and throws an error.
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 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.
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.
Maven pom.xml file
POM is an acronym for Project Object Model.
The pom.xml file contains information of project and configuration information
for the maven to build the project such as dependencies, build directory,
source directory, test source directory, plugin, goals etc.
Maven reads the pom.xml
file, then executes the goal.
Before maven 2, it was
named as project.xml file. But, since maven 2 (also in maven 3), it is renamed
as pom.xml.
Elements of maven pom.xml file
For creating the simple
pom.xml file, you need to have following elements:
Element
|
Description
|
project
|
It is the root element of pom.xml file.
|
modelVersion
|
It is the sub element of project. It
specifies the modelVersion. It should be set to 4.0.0.
|
groupId
|
It is the sub element of project. It
specifies the id for the project group.
|
artifactId
|
It is the sub element of project. It
specifies the id for the artifact (project). An artifact is something that is
either produced or used by a project. Examples of artifacts produced by Maven
for a project include: JARs, source and binary distributions, and WARs.
|
version
|
It is the sub element of project. It
specifies the version of the artifact under given group.
|
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-app</artifactId>
<version>1</version>
</project>
|
Maven pom.xml file with additional elements
Here, we are going to
add other elements in pom.xml file such as:
Element
|
Description
|
packaging
|
defines packaging type such as jar, war
etc.
|
name
|
defines name of the maven project.
|
url
|
defines url of the project.
|
dependencies
|
defines dependencies for this project.
|
dependency
|
defines a dependency. It is used inside
dependencies.
|
scope
|
defines scope for this maven project. It
can be compile, provided, runtime, test and system.
|
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>
|
Maven Example
We
can create a simple maven example by executing the archetype:generate command
of mvn tool.
To
create a simple java project using maven, you need to open command prompt and
run the archetype:generate command of mvn tool.
Syntax
The syntax to
generate the project architecture is given below:
mvn archetype:generate -DgroupId=groupid -DartifactId=artifactid
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=booleanValue
|
Example
The example to
generate the project architecture is given below:
mvn archetype:generate -DgroupId=com.javatpoint -DartifactId=CubeGenerator
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
|
Compile
the Maven Java Project
To
compile the project, go to the project directory,
for
example: /home/Ubuntu/CubeGenerator and write the following
command on the
command prompt:
command prompt:
mvn clean compile
|
Now, you will see a lot of
execution on the command prompt. If you check your project directory, target
directory is created that contains the class files.
Run
the Maven Java Project
To run the project, go to the project directory\target\classes,
for
example: C:\Users\SSS IT\CubeGenerator\target\classes and
write the following
command on the command prompt:
command on the command prompt:
java com.javatpoint.App
|
Now, you will see the output on the command prompt:
Output of the maven example
Hello World!
DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.
ReplyDeleteGood to learn about DevOps at this time.
devops training in chennai with placement |
best devops training institute in chennai |
best devops training in chennai quora |
devops training in Velachery | devops institute in chennai
I get a lot of great information from this blog. Thank you for your sharing this informative blog. Just now I have completed Devops Training in Gurgaonat iClass Gyansetu in Gurgaon .
ReplyDelete