MAVEN
INTRODUCTION
Maven is a build automation and dependency management tool
for Java projects. It simplifies how you build, manage and
configure Java applications by automating tasks such as
compiling code, running tests and managing external libraries
(dependencies).
Think of Maven as a project manager for your Java
application:
It knows where to find the tools and libraries
(dependencies) your project needs.
It organizes tasks like compiling, testing, and packaging,
so you don’t have to do them manually.
It ensures that everything fits together seamlessly.
Maven makes it easy to download 3rd party dependencies for
your project.
Even gives your project some structure through conventions
and allows you to have plug ins to quality check your code and
deploy your code later on.
We have to create a “pom.xml” file in our project whenever
we’re using maven. It contains it’s project structure,
dependencies and build processes.
It also offers a wide repository of open source libraries and
plugins that can easily be integrated into projects.
FEATURES OF MAVEN:
Dependency management
Project Standardization
Build automation
Plug in system
Consistent builds
MAVEN LIFECYCLE
Maven is a powerful project management tool that is
based on POM (Project object model). It is used for project
build, dependency and documentation. It is a tool that is
used for building and managing any Java based project.
The default Maven lifecycle consists of 8 major steps for
compiling, testing,building and installing a given Java
project:
i) Validate: This step validates the project if the project
is correct. It checks if all the dependencies have been
downloaded and are available in the local repository.
ii) Compile: Complies the source code and converts
the .java files to .class files and stores the classes in
the targets/classes folder.
iii) Test: It runs unit test for the project.
iv) Package: Packages compiled code in distributable
format like JAR or WAR.
v) Integration test
vi) Verify: Verifies that the project is valid and meets the
quality standards
vii) Install: Installs packaged code to the local maven
repository.
viii) Deploy: Copies the packaged code to the remote
repository to share with other developers.
MAVEN COMMANDS
i) mvn clean: Cleans the project and removes all files
generated by previous build.
ii) mvn compile: Compiles source code of project.
iii) mvn test-compile: Complies test source code
iv) mvn test: Runs tests for the project.
v) mvn package: Creates JAR or WAR files for the project
to convert into a distributable format.
vi) mvn install: deploys packaged JAR/WAR file to local
repository.
vii) mvn site: generates project documentation.
viii) mvn validate: validates the project’s POM and
configuration.
ix) mvn idea:idea : generates project files for Intellij IDEA
x) mvn release:perform: Performs a release build
xi) mvn deploy: Copies the packaged JAR/WAR files to
the remote repository after compiling.
xii) mvn archetype:generate: Used to generate new
project from an archetype which is a template for a
project. Typically used to create new projects based
on a specific structure.
xiii) mvn dependency:tree : Used to display the
dependencies of the project in a tree format. Typically
this command is used to understand the
dependencies of the project and troubleshoot any
issues.
DIFFERENT TYPES OF DEPENDENCIES IN
MAVEN
Maven simplifies the creation and
management of dependencies by using a
central repository that contains a vast
collection of open source libraries and
frameworks. To create and manage
dependencies in Maven follow these steps:
STEP 1:
Define dependencies: The first step is to
define dependencies in the project’s
“pom.xml” file. This file is an XML file that
contains the project’s configuration
information including the dependencies. To
define a dependency you need to specify the
groupID, artifactID and version of the
dependencies.
Eg:
If you wanna add Spring framework dependency
to your project:
STEP 2:
Resolve dependencies: Once the
dependencies are defined in the pom.xml file,
Maven will automatically download and
resolve the dependencies from the central
repository. Maven checks if the dependencies
are already there in the local repository and if
not it downloads it from the central
repository.
STEP 3:
Dependency scope: Dependencies in Maven
can have different scopes which determine
when and where that dependency is used.
The 4 main scopes are: compile, provided,
runtime and test. The scope can be specified
in the dependency declaration.
Eg: To specify that a dependency is for
testing we add to pom.xml:
STEP 4:
Managing dependencies: Adding, removing,
updating dependencies. For example to
update a dependency you can use the
following command:
mvn versions: use-latest-versions
This will update all the project’s
dependencies to the latest version available
in the central repository.
PROJECT
MAVEN PLUGINS
Maven plugins are central part of maven
framework.
2 types of Maven plugins:
i) Build plugins- executed at the time of
build
ii) Reporting Plugins- executed at the time
of project generation
CORE PLUGINS:
CREATING AND MANAGING MULTI
MODULE PROJECTS
We are creating modules of our project inside
our big project.
mysite
-util
-core
-UI
HOW TO CREATE A WEB APPLICATION
WITH MAVEN
Basically create a new project, under generators
select Maven archetype. Catalog must be
internal, archetype should be web app (last
option) since you’re creating a web app.