unit - ii (1)
unit - ii (1)
Introduction
If we choose not to use Maven for building the Java projects then we may encounter
following problems-
4) IDE integration: IDE stands for Integrated Development Environment, There are
many popular IDE such as Eclipse, IntelliJ IDEA, NetBeans and so on. Many popular
IDE provide seamless integration with Maven, allowing us to import, build and
manage Maven projects effortlessly, Without Maven, we need to configure the IDE
manually.
5) Lack of plugins and community support: Maven has support for plugins that can
simplify various tasks such as deployment and documentation generation Without
Maven, lack of Plugins forces the developer in perform deployment and
documentation tasks manually Which can be time consuming and error prone.
Features of Maven
Model-based builds − Maven is able to build any number of projects into predefined
output types such as jar, war, metadata.
Coherent site of project information − Using the same metadata as per the build
process, maven is able to generate a website and a PDF including complete
documentation.
Release management and distribution publication − Without additional
configuration, maven will integrate with your source control system such as CVS and
manages the release of a project.
Backward Compatibility − You can easily port the multiple modules of a project
into Maven 3 from older versions of Maven. It can support the older versions also.
Automatic parent versioning − No need to specify the parent in the sub module for
maintenance.
Parallel builds − It analyzes the project dependency graph and enables you to build
schedule modules in parallel. Using this, you can achieve the performance
improvements of 20-50%.
Better Error and Integrity Reporting − Maven improved error reporting, and it
provides you with a link to the Maven wiki page where you will get full description of
the error.
Installation of Maven
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
Now it will display the version of maven and jdk including the maven home and java
home.
Let's see the output:
POM Files
POM stands for Project Object Model.
It is XML file which is named as pom.xml.
It is a fundamental building blocks for Maven project.
It contains information related to the project and configuration information such as
dependencies,
plugins,
source directory,
goals and so on.
Maven reads pom.xml file to accomplish its configuration and operations,
<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>
Additional Elements
1) Clean: In the Clean lifecycle phase, it performs the cleaning operation in which it
deletes the build directory name target and its contents for a fresh build and
deployment. perform this operation we use command mvn clean.
3) Site: This phase handles the generating the java documentation of the project.
Let us discuss them in detail
1) Clean phase
The purpose of clean phase is to clean up the project and make it ready for fresh
compile and deployment. Clean lifecycle performs 3 types of clean operations -
1. Pre-clean - This phase executes processes needed prior to the actual project
cleaning.
2) Default phase
The default lifecycle is the most commonly used one and is responsible for building,
testing, and packaging the project. It includes the following phases:
• Validate: Validates the project's configuration and dependencies.
• Compile: Compiles the project's source code.
• Test: Executes unit tests for the project.
• Package: Packages the compiled code and resources into a distributable format
(e.g., JAR or WAR).
• Verify: Performs additional checks and generates reports to ensure code quality.
• Install: Installs the project's artifact in the local Maven repository.
• Deploy: Deploys the project's artifact to a remote repository (if needed).
3) Site phase
The Maven site plugin handles the project site's documentation, which is used to
create reports, deploy sites, etc.
The phase has four different stages:
• Pre-site: Executes tasks before generating the site documentation.
• Site: Generates project documentation and reports.
• Post-site: Executes tasks after generating the site documentation.
• Site-deploy: Deploys the generated site documentation to a remote server (if needed).
Maven Profile
Profiles are portable for different build environments. The build environment
means a specific environment for production, testing or development
environment instances. So in order to configure these instances Maven provides
the various features for the build using profile.
The profiles are specified in a pom.xml file. Hence using a single pom.xml file, it
possible to work in different environments such as production or development
with the help of profiles.
By defining the profiles it is possible to modify the pom.xml during the build
time. That means we can configure separate environments for development and
production instances. So, based on the parameters passed, the corresponding
profiles are activated accordingly,
Maven Repository
Maven repository is a directory where all the project jars, library jar, plugins or any
other project specific artifacts are stored and can be used by Maven easily.
There are three types of maven repositories:-
Maven local repository is a folder location on your machine. It gets created when
you run any maven command for the first time.
Maven local repository keeps your project's all dependencies (library jars, plugin
jars etc.). When you run a Maven build, then Maven automatically downloads all
the dependency jars into the local repository.
To override the default location, mention another path in Maven settings.xml file
available at %M2_HOME%\conf directory.
Example:
<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
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>C:/MyLocalRepository</localRepository></settings>
Central Repository:
This is the repository provided by maven community.
This repository contains large set of commonly used/required
libraries for any java project. Basically, internet connection is required if
developers want to make use of this central repository.
But, no configuration is required for accessing this central repository.
Key concepts of Central repository are as follows −
This repository is managed by Maven community.
It is not required to be configured.
It requires internet access to be searched.
Example:
<repositories>
<repository>
<id>central.repository</id>
<url>http://repo1.maven.org/maven2/</url>
</repository>
</repositories>
Remote Repository
Maven does not find a mentioned dependency in central repository as well.
It then stops the build process and output error message to console.
Example:
<repositories>
<repository>
<id>remote.repository</id>
<url>http://download.ogrname.com/maven2/</url>
</repository>
</repositories>
Maven Plugins:
Maven. Plugin is a central part of Maven framework which is used to perform specific
goal.
1. Build plugin: These plugins are declared inside <build> element. These plugins
are executed at the time of build.
2. Reporting plugin: These plugins are declared inside the element <reporting. These
plugins are executed at the time of site generation.
resources: Copy the resources to the output directory for including in the JAR.
verifier: Useful for integration tests it verifies the existence of certain conditions.
Example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source> <!-- Java source version -->
<target>1.8</target> <!-- Java target version -->
</configuration>
</plugin>
</plugins>
</build>
Benefits of Maven Plugins: • More detailed and well architecture. • A managed life
cycle • Implementation of multiple languages • Reusability of the common build
logic. • Ability to write maven plugins completely in java
These project dependencies are collectively specified in the pom.xml file, inside of a
<dependencies> tag.
When we run a maven build or execute a maven goal, these dependencies are resolved
and then loaded from the local repository.
If these dependencies are not present in the local repository, then Maven will
download them from a remote repository and cache them in the local repository.
We have to be sure that all these components are compatible for building our product.
This task can be taken care by dependency management.
Example of dependency
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5 RELEASE</version>
</dependency>
<dependencies>
Dependency Scope:
Dependency scope in maven is classified into 5 different types. Each of these scope
controls the dependencies that are available in the class path and dependencies that
are included in the application.
Sr.No
Scope & Description
.
compile
1 This scope indicates that dependency is available in classpath of project.
It is default scope.
provided
2 This scope indicates that dependency is to be provided by JDK or web-
Server/Container at runtime.
3 runtime
This scope indicates that dependency is not required for compilation, but
is required during execution.
test
4 This scope indicates that the dependency is only available for the test
compilation and execution phases.
system
5
This scope indicates that you have to provide the system path.
import
This scope is only used when dependency is of type pom. This scope
6
indicates that the specified POM should be replaced with the
dependencies in that POM's <dependencyManagement> section.