📦

Understanding Maven Lifecycle and Components

Jun 4, 2025

Maven Lifecycle, Phases, Goals, Plugins, and Profiles

Introduction

  • Focus on Maven lifecycle, phases, goals, plugins, and profiles.
  • Useful for Java interviews.

Maven Lifecycle

  • Definition: Sequence of phases executed sequentially.
  • Three Lifecycles:
    1. Site: Generates project website.
    2. Clean: Removes target folder from the root directory.
    3. Default: Main lifecycle with 20+ phases.

Running Lifecycles

  • Command example: mvn site to generate a site.
  • Results in a site folder in the target directory.

Default Lifecycle Phases

  • Phase: A step consisting of zero or more goals.
  • Key Phases:
    • Compile: Compiles source code.
    • Test Compile: Compiles test code.
    • Test: Runs tests.
    • Package: Packages files into JAR/WAR.
    • Install: Installs to local repository.
    • Deploy: Copies to remote repository.

Skipping Phases

  • Example command to skip tests: mvn package -DskipTests

Maven Goals

  • Definition: Unit of work, often linked to phases.
  • Goals run with specified plugins.

Example

  • Running a goal separately: mvn plugin:goal
  • Example compile goal: mvn compile

Maven Plugins

  • Definition: Java class running goals, assembled in a special way.
  • Plugins contain specific goals.
  • Official Plugins:
    • Examples: Clean, Compiler, Deploy.

Configuring Plugins

  • Use pom.xml to specify configurations.
  • Example: Compiler plugin with verbose output.
  • Command: mvn compile -Dcompiler.verbose=true

Maven Profiles

  • Definition: Set of configurations for different build requirements.
  • Allows building projects in different environments.

Creating Profiles

  • Defined in pom.xml using <profiles> tag.
  • Example: Production profile using <id> and configuration.

Activating Profiles

  • Command: mvn -Pprofile-id package
  • Automatic Activation: Use <activation> tag for conditions.
    • Activators: property, file, jdk, os

Conclusion

  • Comprehensive overview of Maven lifecycle, goals, plugins, and profiles.
  • Useful for project setups and interviews.
  • Next video: Multi-module project creation and dependency management.