Skip to content

Maven

Maven is a build automation and project management tool primarily used for Java projects.

Features

  • Dependency Management
  • Standardized Directory Structure
  • Lifecycle Management
  • Plugin System
  • Build Automation

Basic Maven Commands

Command Description
mvn compile Compiles the source code.
mvn test Runs unit tests using a testing framework like JUnit.
mvn package Packages the compiled code into a distributable format, such as a JAR or WAR file.
mvn clean Cleans the working directory by removing files generated by the previous build, similar to make clean.
mvn install Performs the complete build cycle including testing and integration tests, and installs the package into the local repository cache (~/.m2).
mvn install -DskipTests Builds the project and installs it into the local repository without running tests.
mvn install -T 1C -DskipTests Builds and installs the project using one thread per CPU core, without running tests.
mvn site Generates project documentation, though this is rarely used.

General Concepts

Lifecycle

A Lifecycle is a sequence of Phases which get executed sequentially.

Maven Lifecycle Description
default Handles project deployment. This is the main lifecycle.
clean Cleans up artifacts created by prior builds.
site Generates project documentation.

Default Lifecycle Phases

Phase Description
validate Validates the project is correct and all necessary information is available.
initialize Initializes build state, e.g., sets up properties or creates directories.
generate-sources Generates any source code for inclusion in compilation.
process-sources Processes the source code in some manner.
generate-resources Generates resources, like copying extra files for inclusion in the build.
process-resources Processes resources such as filtering.
compile Compiles the source code.
process-classes Processes the compiled files, e.g., post-processing bytecode manipulation.
generate-test-sources Generates any test source code for inclusion in compilation.
process-test-sources Processes the test source code in some manner.
generate-test-resources Creates resources needed for testing.
process-test-resources Copies and processes the resources needed for testing.
test-compile Compiles the test source code.
process-test-classes Processes the compiled test classes.
test Runs tests using a suitable unit testing framework.
prepare-package Prepares the package for distribution.
package Packages the compiled code in its distributable format, e.g., a JAR.
pre-integration-test Executes processes needed before integration tests.
integration-test Executes integration tests if any.
post-integration-test Performs actions required after integration tests have run.
verify Checks if the project meets quality criteria.
install Installs the package into the local repository for use as a dependency.
deploy Copies the final package to the remote repository for sharing.

Clean Lifecycle Phases

Phase Description
pre-clean Executes processes needed before clean.
clean Removes files generated at build time.
post-clean Executes processes needed after clean.

Site Lifecycle Phases

Phase Description
pre-site Executes processes needed before site generation.
site Generates the project's site documentation.
post-site Executes processes needed after site generation.
site-deploy Deploys the generated site documentation.

Phase(s)

A phase is a step in the build lifecycle. If a phase gets executed, everything up to and including the previous phase in the sequence is executed in order beforehand.

Goal(s)

A goal represents a single, specific task that contributes to building and managing a project. Each goal is defined in a Maven plugin.