Skip to main content

Apache Maven: Management and Lifecycle

Apache Maven is a core tool in the Java ecosystem used for project management, build automation, and software dependency management.

Unlike compiling manually with the javac command or manually managing JAR files, Maven introduces a Project Object Model (POM, defined in the pom.xml file) that standardizes the project structure and automates compilation, testing, and packaging phases.


1. Apache Maven Installation

To use Maven in your local environment, first make sure you have an active installation of the Java Development Kit (JDK).

If you use Scoop or Chocolatey in PowerShell:

# Using Scoop
scoop install maven

# Using Chocolatey
choco install maven

Option B: Manual Installation

  1. Download the binary ZIP file from the official Apache Maven page.
  2. Extract the content to a directory in your system (for example, C:\Program Files\apache-maven-3.9.x).
  3. Add the MAVEN_HOME or M2_HOME system environment variable pointing to that directory.
  4. Add the path C:\Program Files\apache-maven-3.9.x\bin to the PATH environment variable.
  5. Verify the installation in a new terminal by running:
    mvn -version

2. The Maven Lifecycle

The default Maven lifecycle (default lifecycle) executes a strict series of sequential phases. When you run a command indicating a specific phase, Maven automatically executes all preceding phases in order:

validatecompiletestpackageverifyinstalldeploy\text{validate} \rightarrow \mathbf{compile} \rightarrow \text{test} \rightarrow \mathbf{package} \rightarrow \text{verify} \rightarrow \mathbf{install} \rightarrow \text{deploy}

Main Phases Explained:

  • validate: Verifies that the project is correct and all necessary information is available (for example, validating that pom.xml is well-formed).
  • compile: Compiles the project source code (.java files) into bytecode (.class). They are placed in the target/classes directory.
  • test: Runs unit tests (using frameworks like JUnit or TestNG) against compiled code.
  • package: Takes the compiled code and packages it into its distribution format (such as a .jar or .war file), placing it in the target/ directory.
  • verify: Runs additional checks on packages to ensure quality criteria are met.
  • install: Installs the packaged artifact into the local Maven repository (located at ~/.m2/repository), making it available as a dependency for other local projects.
  • deploy: Copies the final package to a remote repository (like Nexus or JFrog Artifactory) to share with other developers or teams.

3. Comparison: mvn compile vs mvn clean package vs mvn clean install

It is very common to prefix commands with clean (for example, mvn clean package). The clean phase belongs to Maven's clean lifecycle and removes the target/ directory from previous builds to ensure a clean build from scratch.

Sequence Diagram and Differences (Mermaid SVG)

Direct Comparison Table

CommandWhat it executesMain Output LocationBest Use Case
mvn compilevalidate \rightarrow compileTarget directory (/target/classes)Quick syntax check and local testing.
mvn clean packageDeletes old build \rightarrow validate \rightarrow compile \rightarrow test \rightarrow packageTarget directory (/target/app.jar)Creating a production-ready file for a standalone app.
mvn clean installDeletes old build \rightarrow executes all phases up to installLocal M2 repository (~/.m2/repository)Building multi-module projects where other local apps rely on this code.

4. Standard Maven Project Structure

Maven promotes the principle of Convention over Configuration. A standard project follows this folder structure:

my-project/
├── pom.xml
└── src/
├── main/
│ ├── java/ # Application source code (.java)
│ └── resources/ # Configuration files (application.properties, etc.)
└── test/
├── java/ # Unit tests (.java)
└── resources/ # Test resources