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.
mvn -v
mvn archetype:generate -DgroupId=com.soham.app -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
mvn archetype:generate
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.
| 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. |
If dependency is not found in these repositories, maven stops processing and throws an error.
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 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.
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| 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. |
| 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. |
The mvn package command completes the build life cycle of the maven project such as:
Now you will see that a jar file is created inside the project/target directory.
mvn package