Professional Documents
Culture Documents
Patil Pratishthan’s
D. Y. Patil Institute of Master of Computer Applications and Management
(Approved by AICTE, New Delhi & Affiliated to Savitribai Phule Pune University)
Dr. D. Y. Patil Educational Complex, Sector 29, Pradhikaran, Akurdi, Pune – 411 044
Tel No: (020)27640998, Website: www.dypimca.ac.in, E-mail : director@dypimca.ac.in
----------------------------------------------------------------------------------------------------------------------
MONOGRAPH
• 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
Resources ${basedir}/src/main/resources
Tests ${basedir}/src/test
2. All Maven-related configuration settings are found in the POM. You can edit and configure plug-
ins in the <plugins> tag of a pom.xml file.
3. Maven provides default settings for configurations, so you don’t have to add every configuration
into the pom.xml file.
5.3. Maven POM Builds (pom.xml)
• The pom.xml file contains information about the project and configuration information to build
the project.
• It contains dependencies, build directory, source directory, test source directory, plugin, etc.
Maven goes through the pom.xml file, then executes the goal.
• Contents of pom.xml file:
• project : Root element of a pom.xml file.
• groupId: Sub element of the project. It describes the Id for the project.
• modelVersion: Sub element of the project. It tells you the model version.
• artifactId: Sub element of the project. It specifies the id for the project. An artifact is
either produced or used by a project. Examples of artifacts: JARs, source and binary
distributions, and WARs.
• Version: Sub element of the project. It tells you the version of the artifact under given
group.
There are some additional elements in pom.xml file:
• Packaging: defines the packaging type such as war, jar etc.
• Scope: defines the scope for the maven project.
• Url: specifies the URL of the project.
• Name: defines the name of the maven project.
• Dependency: defines a dependency. It is used inside dependencies.
Sample a pom.xml file
<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.edureka.application1</groupId>
<artifactId>my-application1</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>Maven Quick Start Archetype</name>
<ur>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
A Build Lifecycle is a well-defined sequence of phases, which define the order in which the goals
are to be executed. Here phase represents a stage in life cycle.
A typical Maven Build Lifecycle consists of the following sequence of phases.
validate Validating the Validates if the project is correct and if all necessary
information information is available.
Test Testing Tests the compiled source code suitable for testing framework.
package packaging This phase creates the JAR/WAR package as mentioned in the
packaging in POM.xml.
clean
default(or build)
site
1. Clean Lifecycle
When we execute mvn post-clean command, Maven invokes the clean lifecycle consisting of the
following phases.
pre-clean
clean
post-clean
Maven clean goal (clean:clean) is bound to the clean phase in the clean lifecycle.
Its clean:cleangoal deletes the output of a build by deleting the build directory. Thus, when mvn
clean command executes, Maven deletes the build directory.
process-sources Process the source code, for example, filter any value.
process-resources Copy and process the resources into the destination directory, ready for
packaging phase.
process-test-sources Process the test source code, for example, filter any values.
test-compile Compile the test source code into the test destination directory.
process-test-classes Process the generated files from test code file compilation.
test Run tests using a suitable unit testing framework (Junit is one).
prepare-package Perform any operations necessary to prepare a package before the actual
packaging.
package Take the compiled code and package it in its distributable format, such as a
JAR, WAR, or EAR file.
pre-integration-test Perform actions required before integration tests are executed. For example,
setting up the required environment.
integration-test Process and deploy the package if necessary into an environment where
integration tests can be run.
post-integration-test Perform actions required after integration tests have been executed. For
example, cleaning up the environment.
Verify Run any check-ups to verify the package is valid and meets quality criteria.
install Install the package into the local repository, which can be used as a
dependency in other projects locally.
deploy Copies the final package to the remote repository for sharing with other
developers and projects.
3. Site Lifecycle
Maven Site plugin is generally used to create fresh documentation to create reports, deploy site, etc. It
has the following phases −
pre-site
site
post-site
site-deploy
Maven Repository
• A repository is simply a directory on your machine.
• The project jars, plugins or any other project-related materials are stored here.
• Different types of repositories:
1. Local Repository
2. Central Repository
3. Remote Repository
• Whenever any dependency has to be searched, it is done in repositories. Maven initializes its
search from Local repository, then Central repository and finally in Remote repository.
5.5. Maven Local Repository (.m2)
The local repository of Maven is a folder location on your machine, where all the project
related elements are stored. As soon as the Maven build is executed, Maven automatically
downloads all the dependency jars into the local repository.
By default, maven local repository is user_home/m2 directory.
Artifact ID
The artifactId is the id of the project. It specifies the name of the project.
The groupId is an XML element in the POM.XML file of a Maven project that specifies the id of
the project group.
In contrast, artifactId is an XML element in the POM.XML of a Maven project that specifies the
id of the project (artifact). Thus, this is the main difference between groupId and artifactId in
Maven.
Dependency Mediation
This determines what version of an artifact will be chosen when multiple versions are encountered as
dependencies. Maven picks the "nearest definition".
That is, it uses the version of the closest dependency to your project in the tree of dependencies.
A
├── B
│ └── C
│ └── D 2.0
└── E
└── D 1.0
Dependency Management
• This allows project authors to directly specify the versions of artifacts to be used when they are
encountered in transitive dependencies or in dependencies where no version has been specified.
• In the example in the preceding section a dependency was directly added to A even though it is
not directly used by A. Instead, A can include D as a dependency in its
dependencyManagement section and directly control which version of D is used when, or if, it
is ever referenced.
Dependency Scope
• This allows you to only include dependencies appropriate for the current stage of the build.
• compile
This is the default scope, used if none is specified. Compile dependencies are available in all
classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
• provided
This is much like compile, but indicates you expect the JDK or a container to provide the
dependency at runtime. For example, when building a web application for the Java Enterprise
Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope
provided because the web container provides those classes. A dependency with this scope is
added to the classpath used for compilation and test, but not the runtime classpath. It is not
transitive.
• runtime
This scope indicates that the dependency is not required for compilation, but is for execution.
Maven includes a dependency with this scope in the runtime and test classpaths, but not the
compile classpath.
• test
This scope indicates that the dependency is not required for normal use of the application, and
is only available for the test compilation and execution phases. This scope is not transitive.
Typically this scope is used for test libraries such as JUnit and Mockito. It is also used for non-
test libraries such as Apache Commons IO if those libraries are used in unit tests (src/test/java)
but not in the model code (src/main/java).
• system
This scope is similar to provided except that you have to provide the JAR which contains it
explicitly. The artifact is always available and is not looked up in a repository.
• import
This scope is only supported on a dependency of type pom in the <dependencyManagement>
section. It indicates the dependency is to be replaced with the effective list of dependencies in
the specified POM's <dependencyManagement> section. Since they are replaced, dependencies
with a scope of import do not actually participate in limiting the transitivity of a dependency.
Excluded Dependencies
• If project X depends on project Y, and project Y depends on project Z, the owner of project X
can explicitly exclude project Z as a dependency, using the "exclusion" element.
Optional Dependencies
If project Y depends on project Z, the owner of project Y can mark project Z as an optional
dependency, using the "optional" element. When project X depends on project Y, X will depend only
on Y and not on Y's optional dependency Z. The owner of project X may then explicitly add a
dependency on Z.
5.9. Maven Plugins
• "Maven" is really just a core framework for a collection of Maven Plugins.
• Almost any action that performed on a project is implemented as a Maven plugin.
• Eg. plugins are used to: create jar files, create war files, compile code, unit test code, create
project documentation.
• Plugins are the central feature of Maven that allow for the reuse of common build logic across
multiple projects.
• Build plugins
They execute during the build process and should be configured in the <build/> element of
pom.xml.
• Reporting plugins
They execute during the site generation process and they should be configured in the
<reporting/> element of the pom.xml.
Plugin Description
Clean Cleans up target after the build. Deletes the target directory.
Antrun Runs a set of ant tasks from any phase mentioned of the build.
Demo - Maven project in Eclipse
After opening Eclipse, choose the workspace you want to use.
Complete the following steps:
Go to the File option
In the drop-down menu, select New
Select the Project option
If you want to create a Java project, you can select the “Java Project” option. Since we are not creating
a Java project specifically, we have chosen the “Project” option.
The dialog box that appears on the screen will display different types of projects.
Select the Maven Project option
Click on Next
A dialog box will appear. Select the default workspace.
Click on “Next”
Several Group IDs, Artifact IDs, and Versions will then appear.
Select a plugin there and click on “Next”
In the next dialog box that appears, you’ll complete the following steps:
Enter the Group ID
“com.simplilearn”
Enter the Artifact ID
“mavenproject”
The version will appear on the screen
These items can all be modified at a later time if needed.
Click on “Finish”
The project is now created.
Open the pom.xml file
You can see all the basic information that you have entered on the screen, such as the Artifact ID,
Group ID, etc.
You can see the junit dependencies have been added.
This process takes place by default in Eclipse. There will also be some by default test cases.
Return to the previous screen and undo the deletion. The errors that occurred will disappear.
The demo shows the relationship between the dependencies and the Eclipse. When a Maven project is
selected, all such dependencies are automatically downloaded. If any dependencies are not present,
Eclipse will show errors.
Advantages of Maven:
Helps manage all the processes, such as building, documentation, releasing, and distribution in
project management
The task of downloading Jar files and other dependencies is done automatically
Makes it easy for the developer to build a project in different environments without worrying about
the dependencies, processes, etc.
In Maven, it’s easy to add new dependencies by writing the dependency code in the pom file
Drawbacks of Maven:
Maven requires installation in the working system and the Maven plug-in for the IDE.
If the Maven code for an existing dependency is unavailable, you cannot add that dependency using
Maven itself