Maven Tutorial
Maven Tutorial
B +91-8884807341
Maven Tutorial
What is Maven?
Maven is a software project management and build tool primarily used with Java-based
projects but that can also be used to manage projects in other programming languages like C#
and Ruby. Maven helps manage builds, documentation, reporting, dependencies, software
configuration management (SCM), releases and distribution.
It simplifies the build process like ANT. But it is too much advanced than ANT.
ANT
Gradle
There are many problems that we face during the project development. They are..
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 ,spring ..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.
Maven simplifies the above mentioned problems. It does mainly following tasks.
Builds
Documentation
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
Reporting
SCMs
Releases
Distribution
ANT Maven
1. Ant doesn't has formal conventions, so we 1. Maven has a convention to place source
need to provide information of the project code, compiled code etc. So we don't need to
structure in build.xml file. provide information about the project
structure in pom.xml file.
2. Ant is procedural, you need to provide 2. Maven is declarative, everything you define
information about what to do and when to do in the pom.xml file.
through code. You need to provide order.
6. The ant scripts are not reusable. 6. The maven plug-in are reusable.
Maven Architecture
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
If dependency is not found in these repositories, maven stops processing and throws an error.
1. Local Repository:
Maven local repository is located in your local system. It is created by the maven when you run
any maven command.
if you want to change local repository we can change using setting.xml (it is available in
C:\Users\<USER_NAME>\.m2 directory ) file under <localRepository> tag.
2. Remote repository:
Maven Remote repository available in your network. we will palace all needed libraries in one
places we can access.
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
3. Central Repository:
Maven central repository is located on the web. It has been created by the apache maven
community itself.
The central repository contains a lot of common libraries that can be viewed by this url
http://search.maven.org/#browse.
https://maven.apache.org/download.cgi
Download the zip file and extract. Now we need to set the JAVA_HOME and MAVEN_HOME
and path of maven.
If not set the path if you try to run any mvn commands will give mvn command is not found like
below.
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
My computer --> right click Properties --> Advance System Settings --> Environment variables
--> system variables --> new... --> enter JAVA_HOME is key and value is Home directory of java.
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
My computer --> right click Properties --> Advance System Settings --> Environment variables
--> system variables --> new... --> enter MAVEN_HOME is key and value is Home directory of
maven.
add the path of maven directories. edit the path of system variable..add the up-to bin folder of
maven.
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
Now lets confirm the maven is configured properly or not.... open the new command prompt
and run below command.
mvn --version
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
For the person building a project, this means that it is only necessary to learn a small set of
commands to build any Maven project, and the POM will ensure they get the results they
desired.
There are three built-in build lifecycles: default, clean and site. The default lifecycle handles
your project deployment, the clean lifecycle handles project cleaning, while the site lifecycle
handles the creation of your project's site documentation.
Each of these build lifecycles is defined by a different list of build phases, wherein a build phase
represents a stage in the lifecycle.
For example, the default lifecycle comprises of the following phases (for a complete list of the
lifecycle phases, refer to the Lifecycle Reference):
validate - validate the project is correct and all necessary information is available
compile - compile the source code of the project
test - test the compiled source code using a suitable unit testing framework. These
tests should not require the code be packaged or deployed
package - take the compiled code and package it in its distributable format, such as a
JAR.
verify - run any checks on results of integration tests to ensure quality criteria are met
install - install the package into the local repository, for use as a dependency in other
projects locally
deploy - done in the build environment, copies the final package to the remote
repository for sharing with other developers and projects.
These lifecycle phases (plus the other lifecycle phases not shown here) are executed
sequentially to complete the default lifecycle. Given the lifecycle phases above, this means that
when the default lifecycle is used, Maven will first validate the project, then will try to compile
the sources, run those against the tests, package the binaries (e.g. jar), run integration tests
against that package, verify the integration tests, install the verified package to the local
repository, then deploy the installed package to a remote repository.
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
In a development environment, use the following call to build and install artifacts into the local
repository.
mvn install
This command executes each default life cycle phase in order (validate, compile,test, package,
verify..), before executing install. You only need to call the last build phase to be executed, in
this case, install.
You will need somewhere for your project to reside, create a directory somewhere and start a
shell in that directory. On your command line, execute the following Maven goal:
the output is :
If you have just installed Maven, it may take a while on the first run. This is because Maven is
downloading the most recent artifacts (plugin jars and other files) into your local repository.
You may also need to execute the command a couple of times before it succeeds. This is
because the remote server may time out before your downloads are complete. Don't worry,
there are ways to fix that.
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
You will notice that the generate goal created a directory with the same name given as the
artifactId. let's see the project structure
pom.xml file :
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML
file that contains information about the project and configuration
details(dependencies,plugins ..etc ) used by Maven to build the project. It contains
default values for most projects. Examples for this is the build directory, which is target;
the source directory, which is src/main/java; the test source directory, which is
src/test/java; and so on. When executing a task or goal, Maven looks for the POM in the
current directory. It reads the POM, gets the needed configuration information, then
executes the goal.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>saucelabs-repository</id>
<url>https://repository-
saucelabs.forge.cloudbees.com/release</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
ref : https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341
setting.xml file:
The settings element in the settings.xml file contains elements used to define values which
configure Maven execution in various ways, like the pom.xml, but should not be bundled to any
specific project, or distributed to an audience. These include values such as the local repository
location, alternate remote repository servers, and authentication information.
The former settings.xml are also called global settings, the latter settings.xml are referred to as
user settings. If both files exists, their contents gets merged, with the user-specific settings.xml
being dominant.
<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
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers/>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
ref : https://maven.apache.org/settings.html
JMS Tech Home Madhu Sudhan Reddy Y.B +91-8884807341