mvn
Build and manage Maven-based Java projects
TLDR
Compile a project
Compile and package the compiled code in its distributable format, such as a jar
Compile and package, skipping unit tests
Install the built package in local maven repository. (This will invoke the compile and package commands too)
Delete build artifacts from the target directory
Do a clean and then invoke the package phase
Clean and then package the code with a given build profile
Run a class with a main method
SYNOPSIS
mvn [options] [<goal(s)>] [<phase(s)>]
PARAMETERS
-B, --batch-mode
Runs Maven in non-interactive (batch) mode, useful for CI/CD environments.
-D<property>=<value>
Defines a system property, often used to pass parameters to plugins (e.g., -DskipTests=true).
-f <file>, --file <file>
Specifies an alternative path for the Project Object Model (POM) file instead of the default pom.xml.
-P <arg>, --activate-profiles <arg>
Activates a comma-separated list of profiles defined in the POM or settings file.
-s <file>, --settings <file>
Specifies an alternative path for the user settings file (settings.xml).
-U, --update-snapshots
Forces a check for updated releases and snapshots on remote repositories.
-o, --offline
Works in offline mode, preventing Maven from connecting to remote repositories.
-N, --non-recursive
Builds the project in the current directory only, skipping sub-modules in a multi-module project.
-X, --debug
Produces detailed execution debug output, useful for troubleshooting.
-e, --errors
Produces verbose execution error messages.
-v, --version
Displays the Maven version information.
-h, --help
Displays help information for the Maven command.
-T <arg>, --threads <arg>
Specifies the number of threads for parallel builds (e.g., 2.0C for 2 cores, 4 for 4 threads).
-pl <arg>, --projects <arg>
Builds specific reactor projects instead of all (e.g., -pl project-a,project-b).
DESCRIPTION
The mvn command invokes Apache Maven, a powerful open-source project management and comprehension tool primarily used for Java projects. It is based on the concept of a Project Object Model (POM), an XML file (pom.xml) that contains information about the project and configuration details used by Maven. Maven streamlines the build process by standardizing build phases like compilation, testing, packaging, and deployment.
It provides robust dependency management, automatically downloading and managing project libraries. Its 'convention over configuration' approach encourages a standard project structure, simplifying builds and reducing setup overhead. Maven's functionality is highly extensible through a rich plugin architecture, allowing it to handle various tasks beyond basic compilation, such as reporting, documentation generation, and release management. It aims to make the daily work of a Java developer easier by providing a consistent and comprehensive build system.
CAVEATS
Maven can be resource-intensive, consuming significant memory and CPU, especially for large multi-module projects. Its XML-based configuration, while powerful, can become verbose and complex, leading to a steeper learning curve for new users. Resolving intricate dependency conflicts can also be challenging and time-consuming.
Furthermore, relying heavily on remote repositories means build times can be affected by network latency or repository availability.
PROJECT OBJECT MODEL (POM)
The pom.xml file is the fundamental unit of work in Maven. It contains information about the project, its build configuration, dependencies, plugins, and profiles. Understanding the POM is crucial for configuring and customizing Maven builds.
BUILD LIFECYCLE
Maven defines a set of standard build lifecycles (e.g., clean, default, site). Each lifecycle consists of phases (e.g., validate, compile, test, package, install, deploy). Executing a phase means executing all preceding phases in that lifecycle. For example, mvn install will execute all phases up to install in the default lifecycle.
PLUGINS
Maven's core functionality is implemented as plugins. Plugins provide goals that execute specific tasks during a build. Maven's extensibility comes from its vast array of available plugins for compilation, testing, reporting, code analysis, and much more. Developers can also create custom plugins.
HISTORY
Apache Maven originated from the Apache Turbine project in 2001 and was publicly released in 2004. Developed by Jason van Zyl, it aimed to address the complexities of traditional build systems by providing a standard, convention-based approach to project builds. Its innovative dependency management and consistent lifecycle phases quickly led to widespread adoption within the Java community, becoming a de facto standard for Java project management and build automation. Subsequent versions have focused on performance improvements, enhanced plugin capabilities, and better support for modern development paradigms.