mvn-package
Compile and package Maven project
TLDR
Package a project
Package a project while skipping test execution
Package a project and force Maven to update all dependencies
SYNOPSIS
mvn package [-D<property>=<value>] [-P<profile-id>] [-f <pom-file>] [-B] [-s <file>]
PARAMETERS
-D<property>=<value>
Sets a system property, often used to configure plugin behavior or pass runtime parameters (e.g., -DskipTests).
-P<profile-id>
Activates one or more specified build profiles defined in the pom.xml or settings.xml.
-f <pom-file>, --file <pom-file>
Specifies an alternative pom.xml file to use instead of the default pom.xml in the current directory.
-B, --batch-mode
Runs Maven in non-interactive mode, useful for automated scripts and CI/CD environments.
-s <file>, --settings <file>
Specifies an alternative settings.xml file to use for user-specific configurations.
-DskipTests
Skips the compilation and execution of unit tests during the build lifecycle.
-U, --update-snapshots
Forces an update of snapshot dependencies and plugins from remote repositories.
-X, --debug
Produces highly detailed debug output, helpful for troubleshooting build issues.
DESCRIPTION
The mvn package command executes the Maven package lifecycle phase, a fundamental step in building Java projects. This phase is responsible for taking the compiled source code and project resources, and then assembling them into a distributable format. Typically, this results in a JAR, WAR, or EAR file, depending on the project's <packaging> type specified in its pom.xml file.
Before reaching the package phase, Maven automatically executes all preceding default lifecycle phases. These include steps like validate, compile (which compiles source code), and test (which runs unit tests). This ensures that all dependencies are resolved, code is successfully compiled, and tests pass before the final artifact is created. The precise actions performed during the package phase are often managed by Maven plugins, such as the maven-jar-plugin or maven-war-plugin, which bind their goals to this phase. This command is essential for producing deployable artifacts ready for distribution or installation.
CAVEATS
The command requires a pom.xml file in the current directory (or specified via -f). A Java Development Kit (JDK) must be installed and configured correctly. Network access may be necessary to download project dependencies. Skipping tests (via -DskipTests) can lead to packaging untested code, which might introduce bugs into the final artifact. The specific output format and content are entirely dependent on the project's configuration in pom.xml.
MAVEN BUILD LIFECYCLES
Maven defines three primary build lifecycles: default, clean, and site. The package phase belongs to the default lifecycle, which manages the main build process. Understanding the sequence of phases within this lifecycle is key to effective Maven usage.
PACKAGING TYPES
The <packaging> element in the pom.xml dictates the artifact type produced by the package phase. Common types include jar (Java Archive), war (Web Application Archive), ear (Enterprise Archive), and pom (for parent projects). Each type typically involves specific plugins and processes.
MAVEN PLUGINS
Maven's core functionality is extended through plugins. Tasks like compiling, testing, and packaging are executed by specific plugins. For instance, the maven-jar-plugin is responsible for creating JAR files, while the maven-war-plugin handles WAR files. These plugins bind their goals to various lifecycle phases.
HISTORY
Maven was initiated by Jason van Zyl for the Apache Software Foundation, with its first stable release in 2004. From its inception, the concept of a standardized build lifecycle, including the package phase, was central to Maven's design philosophy. This innovation aimed to provide a consistent and repeatable build process, particularly for Java projects. The package phase automated the complex and often error-prone task of assembling project artifacts, significantly streamlining development and deployment workflows.


