TechWriterDev
  • Cloud
    • AWS
      • 00_Doubts
      • CloudPractitioner
        • Cloud Computing
        • AWS Global Infrastructure
        • Introduction to AWS EC2
        • Elastic load balancer(ELB)
        • 04_Messaging_Queuing
        • Aditional Computing Service
        • Accessing AWS resources
        • AWS Networking
        • Storage
        • Amazon Database Solutions
        • Monitoring Tools
        • AWS Security
        • Distributed Denial Of Service Attacks
      • DeveloperAssociate
        • References
        • AWS DVA-C02 Services Index
        • Services
          • 00_IAM
            • Identity and Access Management (IAM)
            • Account Protection Mechanisms
            • Access Mechanism of AWS Resources
            • Security Tools
            • Responsibility Model
            • Advanced Concepts
          • 01_EC2
            • Elastic Compute Cloud (EC2)
            • EC2 Volume Types
            • Amazon Machine Image (AMI)
            • AWS charges for IPv4 address
          • 02_SecurityGroups
            • Security Groups
          • 03_Elastic_LoadBalancing
            • Terminology
            • Elastic load balancer
            • Features
            • Basics
          • 04_AutoScaling
            • Auto Scaling
          • 05_RDS
            • Relational Database Service (RDS)
            • Aurora
            • Security
            • RDS Proxy
          • 06_ElastiCache
            • Cache
            • Cache Offerings
          • 07_Route53
            • Basics of DNS
            • Route 53
          • 08_VPC
            • Virtual Private Cloud (VPC)
          • 09_S3
            • Simple Storage Service (S3)
            • S3 Features
            • S3 Encryption
            • S3 Features
            • S3 Bucket Policy and IAM Policy
          • 10_ECS
            • Elastic Container Service (ECS)
            • Elastic Container Registry (ECR)
            • AWS Copilot
          • 11_EKS
            • Elastic Kubernetes Service (EKS)
          • 12_SDK_CLI_Tips
            • Access AWS Resources
          • 13_CloudFront
            • Cloud Front
          • 14_Messaging
            • Simple Queue Service (SQS)
            • Simple Notification Service (SNS)
            • Fan Out Pattern
            • Kinesis
            • Compare and Contrast
          • 15_ElasticBeanStalk
            • Elastic Beanstalk
          • 16_CloudFormation
            • CloudFormation
            • Dynamic References
          • 17_Monitoring
            • AWS Monitoring
            • AWS CloudWatch
            • CloudWatch Alarms
            • Synthetics Canary
            • Amazon EventBridge (formerly CloudWatch Events)
            • X-Ray
            • OpenTelemetry
            • CloudTrail
          • 18_Lambda
            • Lambda
            • Lambda Integrations
            • Configuring Lambda
            • Lambda Layers
          • 19_API_Gateway
            • API Gateway
            • API Gateway Integrations
          • 20_DynamoDB
            • DynamoDB
            • Operations
            • Indexes
            • DynamoDB Accelerator (DAX)
            • DynamoDB Streams
            • Transactions
            • Integrations
          • 21_CICD
            • CICD
            • CodeCommit
            • CodePipeline
            • CodeBuild
            • CodeDeploy
            • CodeArtifact
            • CloudGuru
          • 22_SAM
            • Serverless Application Model (SAM)
          • 23_CDK
            • Cloud Development Kit (CDK)
          • 24_StepFunctions
            • Step Functions
            • Types of step function
          • 25_AppSync
            • AppSync
          • 26_Amplify
            • Amplify
          • 27_STS
            • Security Token Service (STS)
          • 28_DirectoryService
            • Active Directory
          • 29_KMS
            • Encryption
            • KMS API
            • Features
            • Cloud Hardware Security Module (HSM)
          • 30_SSM_Store
            • SSM Parameter Store
          • 31_SecretsManager
            • Secrets Manager
          • 32_Cognito
            • Cognito
      • Questions
        • AWS_Region
        • EC2
        • IAM
  • Database
    • MongoDb
      • Mongo db Basics
      • Mongo DB Atlas
      • Document
      • Import-Export based on Data Format
      • Mongo Shell Commands
      • Query Operators
      • Indexes
      • Upsert
      • MongoDB Aggregation Framework
      • Aggregation Framework Operators
    • PostgreSQL
      • POSTGRE SQL DataTypes
      • About table
      • Constraints
  • Technologies
    • RabbitMQ
      • RabbitMQ Concepts
      • Introduction to Exchanges
      • Introduction to Queues
    • Terraform
      • 00_Introduction
      • Configuration blocks
      • Commands
      • Variables
      • Terraform Cloud
      • Modules
  • Languages
    • Java
      • Logging
        • Getting Started
      • 00_Core
        • 00_Basics
          • Java Vs C++
          • Object oriented principles
          • Steps to compile a java program
          • JVM Internals
          • Understanding Java Development Kit
          • What is JIT Compiler?
          • Java data types
          • 07_identifiers_type_conversion
          • 08_references_and_packages
          • Steps for attaching scanner
        • Concurrency
          • 00_Threads
            • Threads
          • 01_ExecutorFramework
            • Executor Framework
            • Asynchronous Computation
      • 01_Backend
        • 01_HttpAndWebServerBasics
          • HTTP
          • Content Type
          • Web Server
        • 02_J2EE_Basics
          • J2EE_Basics
          • Why HttpServlet classs is declared as abstract class BUT with 100 % concrete functionality ?
        • 03_TomCatAndSession
          • What is a Session?
          • WebContainer
        • 04_PageNavigation
          • Cookies Additional Information
          • Page Navigation Techniques
        • 05_AboutServlet
          • CGI v/s Servlet
          • Executor Framework
          • Servlet Life cycle
          • SERVLET CONFIG
          • Servlet Context
          • Servlet Listener (web application listener)
        • 08_SpringBoot
          • Spring Boot
          • Some common annotations used in spring eco system
        • 09_SpringDataJPA
          • Spring Data JPA
        • Java_Language_Changes
          • JDK enhancement tracking reference
        • 06_ORM_Hibernate
          • readmes
            • Hibernate
            • Advantages of Hibernate
            • Hibernate Caching
            • Hibernate API
            • Hibernate Query API
            • Hibernate Annotations and JPQL
            • Entity and Value Type
        • 07_SpringFramework
          • bean_validation
            • Bean Validation
          • core
            • readme
              • Spring
              • Spring Framework Modules
              • Spring MVC Request flow
              • Dependency Injection
              • Spring Beans
              • 06_Spring_Framework_Annotations
      • 03_Tools
        • Maven
          • Maven
  • SoftwareEngineering
    • DesignPatterns
      • Notes
        • Basics
        • OOP
        • SOLID Principles
        • 03_Creational
          • Abstract Factory (aka Kit)
          • Builder
          • Factory Method (aka Virtual constructor)
          • Prototype
          • Singleton
        • 04_Structural
          • Adapter (aka Wrapper)
          • Bridge (aka Handle | Body)
          • Composite
          • Decorator (aka Wrapper)
          • Facade
          • Flyweight
          • Proxy (aka Surrogate)
        • 05_Behavioral
          • Chain of Responsibility
          • Command (aka Action | Transaction)
          • Iterator (aka Cursor)
          • Observer (aka Publish-Subscribe | Dependents)
          • Strategy (aka Policy)
    • Principles
      • REST
        • REST
  • Tools
    • Containers
      • Docker
        • Docker
        • Docker Image
        • Commands
        • Compose
        • Best Practices
      • Kubernetes
        • Kubernetes
    • VCS
      • Git
        • Quick reference of useful Git commands
Powered by GitBook
On this page
  • Maven's objective
  • Maven's purpose
  • Why Maven?
  • Working of Maven
  • POM
  • Maven coordinates
  • Lifecycle
  • Standard Lifecycle
  • Phases
  • Maven Hierarchies
  • Central Repositories
  1. Languages
  2. Java
  3. 03_Tools
  4. Maven

Maven

PreviousMavenNextSoftwareEngineering

Last updated 4 months ago

  • Maven, is an yiddish word meaning accumulator of knowledge.

  • It was a by product of Jakarta Turbine project built under Apache.

  • It's an open source project written in java.

  • It is a build automation tool for overall project management.

  • It helps in

    1. Checking a build status

    2. Generating reports (basically javadocs).

    3. Setting up the automated build process and monitors the same.

  • It follows philosophy.

Maven's objective

  • Making the build process easy.

  • Providing a uniform build system.

  • Providing quality project information.

  • Encouraging better development practices.

Maven's purpose

  • Maven provides following for a project,

    • Build tooling

    • Version management

    • Re-usability

    • Maintainability

    • Comprehensibility

    • Inheritance

  • Maven also provides following capabilities,

    • Produce different build target and result builds via profiles

    • Test code

    • Generate documents

    • Furnish metrics

    • Deploy build artifacts

Why Maven?

  • It eases out source code compilation, distribution, documentation, collaboration with different teams.

  • Maven tries to describe

    1. How a software is built.

    2. The dependencies, plug-ins & profiles that the project is associated in a standalone or a distributed environment.

Working of Maven

  • Maven uses a Project Object Model (POM) to manage a project.

  • Maven commands execute parts of its Project Object Model.

POM

  • A POM is usually described in XML document. (Other formats can be used to describe the Project Object Model, however, XML was the first format used.)

  • It describes build management needs for a project. Some of them are listed below

    Project Needs
    Description

    project coordinates

    Uniquely identifiable set of properties by which the project artifacts can be consumed elsewhere.

    dependencies

    Libraries and code needed to execute the project build management

    plugins

    Helper tools that execute build and build management aspects

    properties

    Common and extracted values used in the project

    inheritance details

    The ability to create a hierarchy of re-usable POM components

    profiles

    Alternate execution pathways that can be activated on a per-execution basis

  • Some elements (xml tags) of maven pom.xml file

    Tags
    Description

    project

    It is the root element of pom.xml file.

    modelVersion

    It is the sub element of project. It specifies the modelVersion.

    groupId

    It is the sub element of project. It specifies the id for the project group (typically organization name).

    artifactId

    It is the sub element of project. It specifies the id for the artifact (project). An artifact is something that is either produced or used by a project. Examples of artifacts produced by Maven for a project include JAR, WAR etc.

    version

    It is the sub element of project. It specifies the version of the artifact under given group.

    packaging

    Defines packaging type such as jar, war etc.

    name

    Defines name of the maven project.

    plugins

    Compiler plugins, eclipse plugins.

    dependencies

    Collection of dependencies for this project.

    scope

    Defines scope for this maven project. It can be compile, provided, runtime, test and system.

  • Maven utilizes content in the POM for its build management. However, maven also has convention-based defaults. Maven thus has the onus of amalgamating defaults and applying overrides and additions discovered in the project’s pom file (typically a pom.xml).

  • This amalgamation of defaults and applying overrides and additions results in an effective POM.

Effective POM

  • An assembly of execution steps, properties and profiles.

  • The content that maven can execute for the project.

  • An exhaustive set of dependencies and plugins needed for such an execution.

  • Determination of any transitive dependencies (dependencies of dependencies, full depth imaginable).

  • Any conflict resolution in terms of dependency versions.

  • An effective POM is a combination of following.

Maven coordinates

  • A way to uniquely identify an artifact. There are three primary coordinates that are used to identify an artifact.

    • groupId

    • artifactId

    • version

Lifecycle

  • A lifecycle groups a sequence of activities pertaining to a specific type of build-management.

  • There are 3 basic lifecycles for its standard build management however, more lifecycles can be created as needed.

  • Lifecycle comprises of phases and maven commands are typically execution of phases.

Standard Lifecycle

Life cycle
Description

clean

Intended for clean-up of any prior build-managed outputs and artifacts.

default (build)

Intended for project build, test and deployment of artifacts.

site

Intended for project site documentation.

Phases

  • Phases are executable blocks.

  • Phases follow an ordered sequence within a given lifecycle.

Goals

  • They are bound to phases and are unit of work.

  • This is aka a binding.

  • A goal performs a task that is considered relevant for the given lifecycle and phase. Maven provides some built in goals. Goals are defined in plugins.

  • Every packaging comes with a predefined set of goals bound to phases of the default lifecycle.

  • Some goals may not be bound to anything and can be invoked directly without a phase-binding.

Plugins

  • Plugins are maven’s way of defining goals and providing connectors for the goals to phases.

  • Plugins are developed as MOJOs _(Maven’s plain Old Java Objects). The plugins define goals and supply logic to deliver the goals.

  • Goals are usually bound to phases in either the maven built-in configurations or the project POM file.

Maven Hierarchies

  • Three types of Maven hierarchies are supported,

    • Parentage

      • A parent POM is a POM from which the current project POM can inherit content.

      • The project POM can depend on exactly one parent POM. This single-parent inheritance is one-way. The parent POM is unaware of the POM that inherits from it.

      • The child POM declares the parentage in its own pom.xml.

    • Aggregation

      • An aggregator POM (also known as a reactor POM) is a POM that can sequence the builds of many projects.

      • An aggregate POM specifies all the projects that can be build-managed together.

      • The child POM(s) remain unaware of the aggregator POM that invokes it. A child POM can be a listed in more than one aggregator POM.

      • The aggregator POM lists the child POM by name in it’s own pom.xml as a module. As the declared module suggests, this pattern is for modular builds of projects. There is no inheritance of any content from the aggregator POM.

    • Bill of Materials

      • A bill-of-materials POM is a POM that can declare bundles of dependencies that have been tested to work well together.

      • The abundance of artifacts and versions of each can, at times, lead to confusion and needs for trial-and-error mechanisms to determine compatibility and/or right functionality. A bill-of-materials POM reduces that overhead.

      • A bill-of-materials POM is a means of multiple inheritance too, since a project POM can import multiple bill-of-material POMs.

Central Repositories

  • A server where maven hosts resuable dependency (library) artifacts

  • Maven's primary repository is called Maven Central.


Ant disadvantages

  1. While using ant , project structure had to be defined in build.xml. Maven has a convention to place source code, compiled code etc. So no need to provide information about the project structure in pom.xml file.

Maven is declarative, everything you define in the pom.xml file. No such support in ant.

There is no life cycle in Ant, where as life cycle exists in Maven.

Maven advantages

  1. Managing dependencies

  2. Uses Convention over configuration - configuration is very minimal

  3. Multiple/Repeated builds can be achieved.

  4. Plugin management.

  5. Testing - ability to run JUnit and other integration test suites.

What is POM? (Project Object Model)

It is the core element of any maven project. Any maven project consists of one configuration file called pom.xml. Location --In the root directory of any maven project.

It contains the details of the build life cycle of a project.

Contents Dependencies used in the projects (Jar files) Plugins used Project version Developers involved in the project Build profiles etc.

Maven reads the pom.xml file, then executes the goal.

Goals in Maven Goal in maven is nothing but a particular task which leads to the compiling, building and managing of a project. A goal in maven can be associated to zero or more build phases. Only thing that matters is the order of the goals defined for a given project in pom.xml. Because, the order of execution is completely dependent on the order of the goals defined. eg : clean , build ,install ,test

What is a Maven Repository

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

Local Repository
Central Repository
Remote Repository

Maven searches for the dependencies in the following order:

Local repository then Central repository then Remote repository. maven repositories

If dependency is not found in these repositories, maven stops processing and throws an error.

  1. Maven Local Repository

Maven local repository is located in the file local system. It is created by the maven when you run any maven command.

By default, maven local repository is HOME / .m2 directory. (Can be updated by changing the MAVEN_HOME/conf/settings.xml)

  1. Maven Central Repository

Maven central repository is located on the web(Created by the apache maven community)

The path of central repository is: https://mvnrepository.com/repos/central

  1. Maven Remote Repository

Maven remote repository is also located on the web. Some of libraries that are missing from the central repository eg JBoss library , Oracle driver etc, can be located from remote repository.

Maven Build Life Cycle What is it ? The sequence of steps which is defined in order to execute the tasks and goals of any maven project is known as build life cycle in maven.

Maven comes with 3 built-in build life cycles

Clean - this phase involves cleaning of the project (for a fresh build & deployment) Default - this phase handles the complete deployment of the project Site - this phase handles the generating the java documentation of the project.

Build Profiles in Maven

It is a subset of elements which allows to customize builds for particular environment. Profiles are also portable for different build environments.

Build environment basically means a specific environment set for production and development instances. When developers work on development phase, they use test database from the production instance and for the production phase, the live database will be used.

So, in order to configure these instances maven provides the feature of build profiles. Any no. of build profiles can be configured and also can override any other settings in the pom.xml

eg : profiles can be set for dev, test and production phases.

Installation (w/o IDE)

  1. Download Maven from Apache (version 3.x)

  2. Add MAVEN_HOME as environment variable

  3. Add maven/bin under path (for easy accessibility) 4.Verify maven mvn -- version

OR use m2e plug-in (a standard part of Eclipse for J2EE)

Maven Working
Effective POM Internal

More about layers in the above diagram can be read .

convention over configuration
here
Maven Lifecycles and Phases