All Courses
Maven Tutorial - eMexo Technologies

The Maven Tutorial provides basic and advanced concepts for Apache Maven technology. Maven tutorials are aimed at both beginners and professionals. Apache Maven is a software project management and understanding tool. Based on the concept of the Project Object Model (POM), Maven can manage project construction, reporting, and documentation from a central point of information. Maven allows you to create and manage Java-based projects. In this tutorial, you will learn how to use Maven in your daily life when developing projects.

What is Maven?

Maven is a software project management and builds tool primarily used based on Java.
Although it’s a project, it can also be used to manage projects in other programming languages ​​such as C# and Ruby. Maven helps you manage builds, documents, reports, dependencies, and software
Configuration management (SCM), release, and distribution.
Simplifies the build process like ANT. But it’s more advanced than ANT.

Alternative build tools for maven projects?

  • ANT
  • Gradle

Understand the problem without a maven?

There are many problems that we face during the project development. They are:

1) Add a set of glasses to each project:

  • Required for struts, springs, and hibernate frameworks to add a set of JAR files to each project.
  • You also need to include the dependencies on the jar.

2) Creating the appropriate project structure:

  • You need to create the appropriate project structure in the Servlet.
  • It will not be executed in other cases such as struts and springs.

3) Build and deploy the project:

  • You need to build and deploy the project.
  • It may work.

What do Build tools will do for us?

Maven simplifies the above-mentioned problems. It does mainly following tasks.

  • First, It makes a project easy to build
  • Second, It provides a uniform build process (the maven project can be shared by all the maven projects)
  • Third, It provides project information (log document, cross-referenced sources, mailing list, dependency list, unit test reports, etc.)
  • Finally, It is easy to migrate to new features of Maven

Apache Maven helps to manage

  • Builds
  • Documentation
  • Reporting
  • SCMs
  • Releases
  • Distribution

what is the difference between ANT and MAVEN?

ANT:

  1. It has no formal rules, so Provide information about the project and the structure of the build.xml file.
  2. Ant is procedural and must be specified Information about what to do and when to do it through the code. You need to provide the order.
  3. Ant has no life cycle.
  4. It is a tool kit.
  5. This is primarily a build tool.
  6. Ant scripts cannot be reused.
  7. Not as good as Maven.

Maven:

  1. Maven has a convention to place source code, compiled code, etc. So we don’t need it
    Provide information about the project. The structure of the pom.xml file.
  2. Maven is declarative and whatever you define In the pom.xml file.
  3. Maven has a life cycle.
  4. It is a framework.
  5. This is primarily a project management tool.
  6. The Maven plugin is reusable.
  7. It has priority over Ant.

Maven Architecture

Maven Tutorial - eMexo Technologies

A maven repository is a directory of packaged JAR files with pom.xml files. Maven searches for
dependencies in the repositories. There are 3 types of maven repositories:

  1. Local Repository
  2. Central Repository
  3. Remote Repository

Maven searches for the dependencies in the following order:

  • The local repository then the Remote repository then the Central repository.
  • If the 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.
  • By default, the maven local repository is the %USER_HOME%/.m2 directory. For example:
  • C:\Users\MADHU.m2
  • if you want to change the local repository we can change using setting.xml (it is available in
  • C:\Users\.m2 directory ) file under tag.

2. Remote repository:

  • Maven Remote repository was available in your network. we will palace all needed libraries in one
    place we can access.
  • we need to configure the remote repository URL in pom.xml

3. Central Repository:

  • Maven’s central repository is located on the web. It has been created by the apache maven
  • community itself.
  • The path of the 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.
  • The default repository is a central repository.

Maven Installation in windows

download maven from the below link.
https://maven.apache.org/download.cgi

Download and unzip the ZIP file. Next, you need to set JAVA_HOME and MAVEN_HOME
And Maven’s path.
When you try to run the MVN command without setting the path, you will notice that the MVNn command does not exist.

set the JAVA_HOME path if not set already.
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.

Maven Tutorial - eMexo Technologies
Maven Tutorial - eMexo Technologies

Now add the MAVEN_HOME too..
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.

Maven Tutorial - eMexo Technologies

Add the path to the Maven directory. Edit the system variable path. Add the up-to-Maven bin folder.

Maven Tutorial - eMexo Technologies

Now let us confirm whether the maven is configured properly or not…. open the new command prompt
and run the below command.
MVN –version

Maven Tutorial - eMexo Technologies

Done…we have successfully configured maven in our system..let’s do further.

The life cycle of Maven

  • Build life cycles are at the core of Maven’s concept.
  • In short, The process of creating and distributing a particular artifact (project) is well defined.
  • For those who create projects, this means they have little to learn.
  • The command to build the Maven project and POM ensures that you get the results that are desirable.
  • There are three built-in build lifecycles: standard, clean, and site. Default lifecycle handle.
  • Project Deployment, Cleanup Lifecycle handles project cleanup during the site lifecycle.
  • Responsible for creating site documentation for your project.

A Build Lifecycle is Made Up of Phases

At first, Each of these build lifecycles is defined by a different list of build phases, including build phases.
Then it Represents a phase of the life cycle.
For example, the standard life cycle includes the following phases (For lifecycle stages, see Lifecycle Reference) :

  • Verification-Verify that the project is correct and that all required information is available
  • Compile-Compile the project source code
  • Testing-Test the compiled source code using the appropriate unit testing framework. This testing does not require code to be packaged or deployed
  • Package-Get the compiled code and package it into a redistributable format like a JAR.
  • Validation-Perform all validations of integration test results to ensure that quality standards are met.
  • Install-installs a package in a local repository for use as a dependency of others On-site project
  • Deploy-Run in a build environment and copy the final package remotely and a repository for sharing with other developers and projects.

These life cycle phases (and other life cycle phases not shown here) are executed
Complete the default life cycle, one at a time. Given the life cycle stages above, this means:
If you use the default lifecycle, Maven first validates the project and then tries to compile it.
Run against sources, tests, package binaries (for example jar), and run integration tests
Check the integration test for this package and install the confirmed package locally
Create a repository and then deploy the installed package to a remote repository.

Command Line calls:

In your development environment, use the following call to create an artifact and install it in your local repository.

MVN install

This command runs each standard lifecycle phase in turn (validation, compilation, testing, packaging,
Confirm ..) Before running the installation. You need to call the last build stage to execute in this case, install.

How to create a maven project through the command line?

Save the project, create a directory somewhere, and start the shell of this directory. At the command line, run the following Maven target:

MVN archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app –
DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 –
DinteractiveMode=false

Maven Tutorial - eMexo Technologies

The Output is:

Maven Tutorial - eMexo Technologies
  • If you just installed Maven, it may take some time to start for the first time.
  • This is because Maven Downloads the latest artifacts (plugin jars and other files) to your local repository.
  • You may also need to execute the command several times before it succeeds.
  • This is because the remote server may time out before the download is complete.
  • Do not worry, there is a way to fix it.
  • You will notice that the general goal created a directory with the same name given as the artifactId. let’s see the project structure
Maven Tutorial - eMexo Technologies

In maven 2 files are most important 1. pom.xml 2. setting.xml

pom.xml file :

The Project Object Model (POM) is the basic unit of work for Maven. XML
A file containing information about the project and configuration details that Maven uses to build the project (dependencies, plugins, etc.). including default value for most projects.

Examples of this are the build directory, which is a 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.

Sample pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<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.saucelabs</groupId>
<artifactId>sauce_appium_junit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sauce_appium_junit</name>
<description>Sample Appium tests using JUnit</description>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<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>

setting.xml file:

The settings element in the settings.xml file contains elements used to define values that configure Maven execution in various ways, like the pom.xml, but should not be bundled with any specific project, or distributed to an audience. These include values such as the local repository. location, alternate remote repository servers, and authentication information.

There are two locations where a settings.xml file may live:

  • The Maven install: ${maven.home}/conf/settings.xml
  • A user’s install: ${user.home}/.m2/settings.xml

The former settings.xml are also called global settings, the latter settings.xml are referred to as user settings. If both files exist, their contents get 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>