[go: up one dir, main page]

0% found this document useful (0 votes)
19 views30 pages

MAVEN

Apache Maven is a project management tool that simplifies the build process for Java-based projects through a central Project Object Model (POM). It automates tasks such as builds, documentation, and dependency management, allowing developers to focus on coding rather than configuration. Maven's features include a standard directory layout, reusable project structures, and extensive plugin support, enhancing collaboration and efficiency across development teams.

Uploaded by

Akshat Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views30 pages

MAVEN

Apache Maven is a project management tool that simplifies the build process for Java-based projects through a central Project Object Model (POM). It automates tasks such as builds, documentation, and dependency management, allowing developers to focus on coding rather than configuration. Maven's features include a standard directory layout, reusable project structures, and extensive plugin support, enhancing collaboration and efficiency across development teams.

Uploaded by

Akshat Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Apache Maven is a software project management and

comprehension tool. Based on the concept of a project object model (POM), Maven can
manage a project's build, reporting and documentation from a central piece of
information. Using maven we can build and manage any Java based project.

What is Maven?
Maven is a project management and comprehension tool that provides developers a
complete build lifecycle framework. Development team can automate the project's build
infrastructure in almost no time as Maven uses a standard directory layout and a default
build lifecycle.

In case of multiple development teams environment, Maven can set-up the way to work
as per standards in a very short time. As most of the project setups are simple and
reusable, Maven makes life of developer easy while creating reports, checks, build and
testing automation setups.

Maven provides developers ways to manage the following −

• Builds
• Documentation
• Reporting
• Dependencies
• SCMs
• Releases
• Distribution
• Mailing list
To summarize, Maven simplifies and standardizes the project build process. It handles
compilation, distribution, documentation, team collaboration and other tasks
seamlessly. Maven increases reusability and takes care of most of the build related
tasks.

Maven Evolution
Maven was originally designed to simplify building processes in Jakarta Turbine project.
There were several projects and each project contained slightly different ANT build files.
JARs were checked into CVS.
Apache group then developed Maven which can build multiple projects together,
publish projects information, deploy projects, share JARs across several projects and
help in collaboration of teams.

Objective
The primary goal of Maven is to provide developer with the following −

• A comprehensive model for projects, which is reusable, maintainable, and easier


to comprehend.
• Plugins or tools that interact with this declarative model.
Maven project structure and contents are declared in an xml file, pom.xml, referred as
Project Object Model (POM), which is the fundamental unit of the entire Maven system.
In later chapters, we will explain POM in detail.

Convention over Configuration


Maven uses Convention over Configuration, which means developers are not required
to create build process themselves.

Developers do not have to mention each and every configuration detail. Maven provides
sensible default behavior for projects. When a Maven project is created, Maven creates
default project structure. Developer is only required to place files accordingly and he/she
need not to define any configuration in pom.xml.

As an example, following table shows the default values for project source code files,
resource files and other configurations. Assuming, ${basedir} denotes the project
location −

Item Default

source code ${basedir}/src/main/java

Resources ${basedir}/src/main/resources

Tests ${basedir}/src/test

Complied byte code ${basedir}/target

distributable JAR ${basedir}/target/classes

In order to build the project, Maven provides developers with options to mention life-
cycle goals and project dependencies (that rely on Maven plugin capabilities and on its
default conventions). Much of the project management and build related tasks are
maintained by Maven plugins.

Developers can build any given Maven project without the need to understand how the
individual plugins work. We will discuss Maven Plugins in detail in the later chapters.

Features of Maven
• Simple project setup that follows best practices.
• Consistent usage across all projects.
• Dependency management including automatic updating.
• A large and growing repository of libraries.
• Extensible, with the ability to easily write plugins in Java or scripting languages.
• Instant access to new features with little or no extra configuration.
• 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.
POM stands for Project Object Model. It is fundamental unit of work in Maven.

It is an XML file that resides in the base directory of the project as pom.xml.

The POM contains information about the project and various configuration detail used
by Maven to build the project(s).

POM also contains the goals and plugins. While executing a task or goal, Maven looks
for the POM in the current directory. It reads the POM, gets the needed configuration
information, and then executes the goal. Some of the configuration that can be specified
in the POM are following −

• project dependencies
• plugins
• goals
• build profiles
• project version
• developers
• mailing list
Before creating a POM, we should first decide the project group (groupId), its name
(artifactId) and its version as these attributes help in uniquely identifying the project in
repository.
POM Example
<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.companyname.project-group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
</project>

It should be noted that there should be a single POM file for each project.

• All POM files require the project element and three mandatory fields: groupId,
artifactId, version.
• Projects notation in repository is groupId:artifactId:version.
• Minimal requirements for a POM −

Sr.No. Node & Description

1 Project root
This is project root tag. You need to
specify the basic schema settings such as
apache schema and w3.org specification.

2 Model version
Model version should be 4.0.0.

3 groupId
This is an Id of project's group. This is
generally unique amongst an
organization or a project. For example, a
banking group com.company.bank has all
bank related projects.

4 artifactId
This is an Id of the project. This is
generally name of the project. For
example, consumer-banking. Along with
the groupId, the artifactId defines the
artifact's location within the repository.

5 version
This is the version of the project. Along
with the groupId, It is used within an
artifact's repository to separate versions
from each other. For example −
com.company.bank:consumer-
banking:1.0
com.company.bank:consumer-
banking:1.1.

Super POM
The Super POM is Maven’s default POM. All POMs inherit from a parent or default
(despite explicitly defined or not). This base POM is known as the Super POM, and
contains values inherited by default.

Maven use the effective POM (configuration from super pom plus project configuration)
to execute relevant goal. It helps developers to specify minimum configuration detail in
his/her pom.xml. Although configurations can be overridden easily.

An easy way to look at the default configurations of the super POM is by running the
following command: mvn help:effective-pom

Create a pom.xml in any directory on your computer.Use the content of above mentioned
example pom.

In example below, We've created a pom.xml in C:\MVN\project folder.

Now open command console, go the folder containing pom.xml and execute the
following mvn command.
C:\MVN\project>mvn help:effective-pom

Maven will start processing and display the effective-pom.


C:\MVN>mvn help:effective-pom
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.companyname.project-group:project >--------------
--
[INFO] Building project 1.0
[INFO] --------------------------------[ jar ]-------------------------------
--
[INFO]
[INFO] --- maven-help-plugin:3.2.0:effective-pom (default-cli) @ project ---
[INFO]
Effective POMs, after inheritance, interpolation, and profiles are applied:

[INFO] ----------------------------------------------------------------------
--
[INFO] BUILD SUCCESS
[INFO] ----------------------------------------------------------------------
--
[INFO] Total time: 2.261 s
[INFO] Finished at: 2021-12-10T19:54:53+05:30
[INFO] ----------------------------------------------------------------------
--

C:\MVN>

Effective POM displayed as result in console, after inheritance, interpolation, and profiles
are applied.
<?xml version="1.0" encoding="Cp1252"?>
<!-- ====================================================================== -
->
<!-- -
->
<!-- Generated by Maven Help Plugin on 2021-12-10T19:54:52+05:30 -
->
<!-- See: http://maven.apache.org/plugins/maven-help-plugin/ -
->
<!-- -
->
<!-- ====================================================================== -
->
<!-- ====================================================================== -
->
<!-- -
->
<!-- Effective POM for project -
->
<!-- 'com.companyname.project-group:project:jar:1.0' -
->
<!-- -
->
<!-- ====================================================================== -
->
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.companyname.project-group</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<repositories>
<repository>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
</pluginRepository>
</pluginRepositories>
<build>
<sourceDirectory>C:\MVN\src\main\java</sourceDirectory>
<scriptSourceDirectory>C:\MVN\src\main\scripts</scriptSourceDirectory>
<testSourceDirectory>C:\MVN\src\test\java</testSourceDirectory>
<outputDirectory>C:\MVN\target\classes</outputDirectory>
<testOutputDirectory>C:\MVN\target\test-classes</testOutputDirectory>
<resources>
<resource>
<directory>C:\MVN\src\main\resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>C:\MVN\src\test\resources</directory>
</testResource>
</testResources>
<directory>C:\MVN\target</directory>
<finalName>project-1.0</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<id>default-clean</id>
<phase>clean</phase>
<goals>
<goal>clean</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>default-testResources</id>
<phase>process-test-resources</phase>
<goals>
<goal>testResources</goal>
</goals>
</execution>
<execution>
<id>default-resources</id>
<phase>process-resources</phase>
<goals>
<goal>resources</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<executions>
<execution>
<id>default-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>default-testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
<executions>
<execution>
<id>default-site</id>
<phase>site</phase>
<goals>
<goal>site</goal>
</goals>
<configuration>
<outputDirectory>C:\MVN\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-
plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
<execution>
<id>default-deploy</id>
<phase>site-deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<outputDirectory>C:\MVN\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-
plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</execution>
</executions>
<configuration>
<outputDirectory>C:\MVN\target\site</outputDirectory>
<reportPlugins>
<reportPlugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-
plugin</artifactId>
</reportPlugin>
</reportPlugins>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
<outputDirectory>C:\MVN\target\site</outputDirectory>
</reporting>
</project>

In above pom.xml, you can see the default project source folders structure, output
directory, plug-ins required, repositories, reporting directory, which Maven will be using
while executing the desired goals.

Maven pom.xml is also not required to be written manually. Maven provides numerous
archetype plugins to create projects, which in order, create the project structure and
pom.xml

Maven in Eclipse
Eclipse provides an excellent plugin m2eclipse which seamlessly integrates Maven and
Eclipse together.

Some of features of m2eclipse are listed below −

• You can run Maven goals from Eclipse.


• You can view the output of Maven commands inside the Eclipse, using its own
console.
• You can update maven dependencies with IDE.
• You can Launch Maven builds from within Eclipse.
• It does the dependency management for Eclipse build path based on Maven's
pom.xml.
• It resolves Maven dependencies from the Eclipse workspace without installing to
local Maven repository (requires dependency project be in same workspace).
• It automatic downloads the required dependencies and sources from the remote
Maven repositories.
• It provides wizards for creating new Maven projects, pom.xml and to enable
Maven support on existing projects
• It provides quick search for dependencies in remote Maven repositories.
Installing m2eclipse plugin
Use one of the following links to install m2eclipse −

Eclipse URL

Eclipse 3.5 (Gallileo) Installing m2eclipse in Eclipse 3.5


(Gallileo)

Eclipse 3.6 (Helios) Installing m2eclipse in Eclipse 3.6


(Helios)

Following example will help you to leverage benefits of integrating Eclipse and maven.

Import a maven project in Eclipse


• Open Eclipse.
• Select File > Import > option.
• Select Maven Projects Option. Click on Next Button.
• Select Project location, where a project was created using Maven. We've created
a Java Project consumer Banking in the previous chapters. Go to ‘Creating Java
Project’ chapter, to see how to create a project using Maven.
• Click Finish Button.

Now, you can see the maven project in eclipse.


Now, have a look at consumer Banking project properties. You can see that Eclipse has
added Maven dependencies to java build path.
Now, it is time to build this project using maven capability of eclipse.

• Right Click on consumerBanking project to open context menu.


• Select Run as option.
• Then maven package option.
Maven will start building the project. You can see the output in Eclipse Console as
follows −
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------< com.companyname.bank:consumerBanking >--------------
--
[INFO] Building consumerBanking 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]-------------------------------
--
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ consumerBanking ---
[INFO] Deleting C:\MVN\consumerBanking\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @
consumerBanking ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered
resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory
C:\MVN\consumerBanking\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @
consumerBanking ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252,
i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @
consumerBanking ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered
resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory
C:\MVN\consumerBanking\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @
consumerBanking ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252,
i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\MVN\consumerBanking\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ consumerBanking
---
[INFO] Surefire report directory: C:\MVN\consumerBanking\target\surefire-
reports

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.companyname.bank.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.028 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ consumerBanking ---
[INFO] Building jar: C:\MVN\consumerBanking\target\consumerBanking-1.0-
SNAPSHOT.jar
[INFO] ----------------------------------------------------------------------
--
[INFO] BUILD SUCCESS
[INFO] ----------------------------------------------------------------------
--
[INFO] Total time: 4.663 s
[INFO] Finished at: 2021-12-13T17:34:27+05:30
[INFO] ----------------------------------------------------------------------
--

Now, right click on App.java. Select Run As option. Then select Java Application.

You will see the result as follows −


Hello World!
Q 1 - Which of the following is true about Maven?

A - Maven is a project management and comprehension tool.

B - Maven provides developers a complete build lifecycle framework.

C - Both of the above.

D - None of the above.

Q 2 - Which of the following is true about Maven?

A - Development team can automate the project's build infrastructure in almost no time

using Maven.

B - Maven uses a standard directory layout and a default build lifecycle.

C - Both of the above.

D - None of the above.

Q 3 - Which of the following is true about Maven Conventions?

A - Maven uses Convention over Configuration which means developers are not required

to create build process themselves.

B - Developers using maven do not have to mention each and every configuration

details.
C - Both of the above.

D - None of the above.

Q 4 - Which of the following aspects of a project can be managed using Maven?

A - Builds

B - Documentation

C - Reporting

D - All of the above.

Q 5 - Which of the following aspects of a project can be managed using Maven?

A - Dependencies

B - SCMs

C - Releases

D - All of the above.

Q 6 - Which of the following aspects of a project can be managed using Maven?

A - Distribution

B - mailing list
C - Both of the above.

D - None of the above.

Q 7 - Which of the following command can tell the version of maven?

A - mvn --version

B - maven -version

C - mvn version

D - maven --version

Q 8 - What POM stands for?

A - Project Object Mode

B - Project Object Model

C - Project Objective Mode

D - Project Objective Model

Q 9 - What of the following is true about POM?

A - It is fundamental Unit of Work in Maven.

B - It is an XML file.
C - Both of the above.

D - None of the above.

Q 10 - What of the following is true about POM?

A - It always resides in the base directory of the project as pom.xml.

B - It contains information about the project and various configuration details used by

Maven to build the project(s).

C - Both of the above.

D - None of the above.

Q 11 - Which of the following configuration element is present in POM.xml?

A - project dependencies

B - plugins

C - goals

D - All of the above.

Q 12 - Which of the following configuration element is present in POM.xml?

A - build profiles
B - project version

C - Both of the above.

D - None of the above.

Q 13 - Which of the following configuration element is present in POM.xml?

A - developers

B - mailing list

C - Both of the above.

D - None of the above.

Q 14 - Which of the following is true about maven artifact?

A - A maven artifact is a file, usually a JAR that gets deployed to a Maven repository.

B - A Maven build produces one or more artifacts, such as a compiled JAR and a 'sources'

JAR.

C - Both of the above.

D - None of the above.

Q 15 - Which of the following is true about maven artifact?


A - Each artifact has a group ID, an artifact ID (just a name), and a version string.

B - The group ID,artifact ID and version together uniquely identify the artifact.

C - A project's dependencies are specified as artifacts.

D - All of the above.

Q 16 - Which of the following is true about Maven Build Lifecycle?

A - A Build Lifecycle is a well defined sequence of phases which define the order in

which the goals are to be executed.

B - A phase represents a stage in life cycle.

C - Both of the above.

D - None of the above.

Q 17 - Which of the following is true about 'clean' Maven life cycle?

A - It cleans up artifacts created by prior builds.

B - This is used to build the application.

C - This generates site documentation for the project.

D - None of the above.


Q 18 - Which of the following is true about 'build' Maven life cycle?

A - It cleans up artifacts created by prior builds.

B - This is used to build the application.

C - This generates site documentation for the project.

D - None of the above.

Q 19 - Which of the following is true about 'site' Maven life cycle?

A - It cleans up artifacts created by prior builds.

B - This is used to build the application.

C - This generates site documentation for the project.

D - None of the above.

Q 20 - Which of the following command removes the target directory with all the
build data before starting the build process?

A - mvn clean

B - mvn build

C - mvn compile
D - mvn site

Q 21 - Which of the following command quickly builds Maven site?

A - mvn clean

B - mvn build

C - mvn compile

D - mvn site

Q 22 - Which of the following phase in maven life cycle validates that the project is
correct and all necessary information is available?

A - validate

B - compile

C - test

D - package

Q 23 - Which of the following phase in maven life cycle compiles the source code of
the project?

A - validate

B - compile
C - test

D - package

Q 24 - Which of the following phase in maven life cycle tests the compiled source
code using a suitable unit testing framework?

A - validate

B - compile

C - test

D - package

Q 25 - Which of the following phase in maven life cycle takes the compiled code and
package it in its distributable format, such as a JAR?

A - validate

B - compile

C - test

D - package
Question Number Answer Key

1 C

2 C
3 C

4 D

5 D

6 C

7 A

8 B

9 C

10 C

11 D

12 C

13 C

14 C

15 D

16 C

17 A

18 B

19 C

20 A

21 D

22 A

23 B

24 C
25 D

You might also like