Maven: What , When & Why?

What is Maven?

  • Maven is a project management tool for Java projects.
  • It manages a project's build, reporting, and documentation.

Why Use Maven?

  • Convention over Configuration - a quick project setup
  • Project Modularization - standard directory structure
  • Dependency Management - adds jars/libraries/dependencies automatically
  • Plugin-based architecture - more powerful builds

When to Use Maven?

  • Project Dependency tree is fairly deep and upgrades frequently.
  • There is a need for streamlined continuous build, integration, and testing.
  • Build Portability is required to maintain consistency across different environments.
  • Quality Project Information is required for reporting/documentation purposes.

POM Demystified
 

What is POM?

  • Project Object Model is the Maven project configuration file.
  • It describes the project, declaratively.
  • It defines what the project is all about and how it should be built.
  • It is an XML file located in base directory of a project.

POM is composed of elements and configurations.

  • Elements in POM
  • Project
  • modelVersion
  • groupId
  • artifactId
  • version

A combination of groupId, artifactId and version is called the GAV coordinates, which uniquely identifies a project.

Configurations in POM

  • Dependencies
  • Plugins
  • Goals
  • Build Profiles

A Maven life cycle consists of a sequence of phases, and each phase consists of a sequence of goals.

Frequently Used Maven Commands,

  • mvn clean - Clean a project.
  • mvn compile - Compile a project.
  • mvn test - Run unit tests.
  • mvn package - Build a package.
  • mvn verify - Run integration test.
  • mvn install - Install a package into local repository
  • mvn -DskipTests=true install - Install an artifact into the local repository and skip unit and integration test execution.
  • mvn deploy - Deploy artifact into enterprise repository.
  • mvn dependency:tree - Display project dependencies.
  • mvn help:active-profiles - Display all profiles.

Summary

This article presented basic informaiton about Maven with the most frequently used command to build and deploy.


Similar Articles