LinuxCommandLibrary

mvn

Build and manage Maven-based Java projects

TLDR

Compile a project

$ mvn compile
copy

Compile and package the compiled code in its distributable format, such as a jar
$ mvn package
copy

Compile and package, skipping unit tests
$ mvn package -DskipTests
copy

Install the built package in local maven repository. (This will invoke the compile and package commands too)
$ mvn install
copy

Delete build artifacts from the target directory
$ mvn clean
copy

Do a clean and then invoke the package phase
$ mvn clean package
copy

Clean and then package the code with a given build profile
$ mvn clean -P [profile] package
copy

Run a class with a main method
$ mvn exec:java -Dexec.mainClass="[com.example.Main]" -Dexec.args="[argument1 argument2 ...]"
copy

SYNOPSIS

mvn [options] [

PARAMETERS

-v, --version
    Display version information.

-h, --help
    Display help information.

-q, --quiet
    Quiet output - only show errors.

-e, --errors
    Produce stack trace output.

-X, --debug
    Produce debug output.

-B, --batch-mode
    Run in non-interactive (batch) mode.

-f, --file
    Force the use of an alternate POM file.

-D=
    Define a system property.

-s, --settings
    Use an alternate settings file.

-o, --offline
    Work offline.

-nsu, --no-snapshot-updates
    Suppress SNAPSHOT updates.

-up, --update-plugins
    Forces a check for updated plugins.

-U, --update-snapshots
    Forces a check for updated snapshots.


    Specify the Maven goal(s) to execute (e.g., clean install). Multiple goals can be specified, separated by spaces.

DESCRIPTION

mvn is the command-line interface to Apache Maven, a powerful project management tool primarily used for Java projects, but applicable to other languages as well.

Maven simplifies the build process by providing a standardized way to define project structure, dependencies, and build configurations. It uses a Project Object Model (POM), defined in an XML file (pom.xml), to describe the project's metadata, dependencies, and build processes.

Using mvn, you can compile source code, run tests, package applications, and deploy artifacts to repositories. It automatically manages dependencies, downloads required libraries, and handles version conflicts, reducing the complexity of managing large projects. Maven central repository is where most of the dependencies are located.

CAVEATS

Maven requires a valid Java Development Kit (JDK) to be installed and configured correctly. Configuration errors in pom.xml can lead to build failures. Ensure proper network connectivity when working online, as Maven needs to download dependencies from repositories.

GOALS AND PHASES

Maven executes goals. Goals are grouped into phases that represent different stages of a build lifecycle. Common phases include: clean, compile, test, package, install, and deploy. Running mvn install, for example, will execute all phases up to and including install.

PLUGINS

Maven's functionality is extended via plugins. Plugins provide goals for specific tasks such as compiling code (compiler plugin), creating JAR/WAR archives (jar/war plugin), running tests (surefire plugin), and generating documentation (javadoc plugin).

REPOSITORIES

Maven uses repositories to store artifacts (JAR files, POM files, etc.). There are three types of repositories: local (on the user's machine), central (a public repository managed by Apache), and remote (repositories hosted on a network, either public or private). Artifacts not found in the local repository are automatically downloaded from remote repositories as needed.

HISTORY

Maven was initially created by Jason van Zyl at Sonatype in the early 2000s as a way to simplify the build process for the Apache Turbine project. It quickly gained popularity within the Java community due to its standardized project structure and dependency management capabilities. Over time, Maven has evolved into a widely used build tool, with extensive plugin support and a large community. Its declarative approach to build definition has significantly improved the consistency and reproducibility of Java builds.

SEE ALSO

java(1), javac(1), jar(1)

Copied to clipboard