Maven Commands Cheat Sheet

Maven is a robust project management tool that is widely used in the Java development community. It simplifies the build process, dependency management, and project configuration. Here is a cheat sheet of essential Maven commands to help you quickly navigate and utilize Maven in your projects.

Maven Setup

Check Maven Version

mvn -version

Ensures Maven is installed and checks the version along with Java details.

Project Creation

Create a New Maven Project

mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Generates a simple Maven project with the specified group ID and artifact ID.

Build Lifecycle

Compile Source Code

mvn compile

Compiles the project's source code into the target directory.

Package the Compiled Code

mvn package

Packages the compiled code into a JAR or WAR file based on the project's packaging type.

Clean the Project

mvn clean

Removes all files generated by the previous build.

Install the Project Locally

mvn install

Installs the built project into your local Maven repository for use as a dependency in other projects.

Testing

Run Unit Tests

mvn test

Executes the unit tests for the project.

Skip Tests

mvn install -DskipTests

Skips the execution of tests but still compiles them.

Running Applications

Run a Java Application

mvn exec:java -Dexec.mainClass="com.example.App"

Runs a Java application by specifying the main class.

Documentation

Generate Project Documentation

mvn site

Generates a site with reports and project information.

Dependency Management

Display Dependency Tree

mvn dependency:tree

Shows the project's dependency hierarchy.

Multi-Module Projects

Create a Parent Project

mvn archetype:generate -DgroupId=com.example -DartifactId=my-multi-module -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Generates a parent project for multi-module configuration.

Add Modules to the Parent Project

cd my-multi-module
mvn archetype:generate -DgroupId=com.example -DartifactId=module1 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
mvn archetype:generate -DgroupId=com.example -DartifactId=module2 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Generates sub-modules for the parent project.

Advanced Commands

Run Maven in Batch Mode

mvn -B install

Runs Maven without any interactive input, useful for automated builds.

Debug Maven Builds

mvn clean install -X

Provides detailed output for debugging Maven builds.

Force Update of Snapshots and Releases

mvn clean install -U

Updates snapshots and releases during the build process.

Check Effective POM

mvn help:effective-pom

Displays the effective POM configuration after inheritance and interpolation.

Generate Source JAR

mvn source:jar

Generates a source JAR file for the project.

Create a Distributable Assembly

mvn assembly:single

Creates a ZIP or TAR file assembly for distribution.

Useful Plugins

Apache Maven Compiler Plugin

Used to compile the source code of the project.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

Maven Surefire Plugin

Used for running unit tests.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
</plugin>

Maven Failsafe Plugin

Used for running integration tests.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.22.2</version>
</plugin>

Apache Maven Assembly Plugin

Used to create distributable archives like ZIP and TAR files.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.3.0</version>
    <configuration>
        <descriptors>
            <descriptor>src/main/assembly/assembly.xml</descriptor>
        </descriptors>
    </configuration>
</plugin>

Summary of Commands

Command Description
mvn -version Checks Maven installation and version.
mvn archetype:generate ... Creates a new Maven project.
mvn compile Compiles the project's source code.
mvn package Packages the compiled code into a JAR/WAR file.
mvn clean Cleans up the target directory.
mvn install Installs the project into the local repository.
mvn test Runs unit tests.
mvn install -DskipTests Skips the tests during installation.
mvn exec:java -Dexec.mainClass="MainClass" Runs a specified Java application.
mvn site Generates project documentation.
mvn dependency:tree Displays the project's dependency tree.
mvn -B install Runs Maven in batch mode.
mvn clean install -X Debugs Maven builds with detailed output.
mvn clean install -U Forces updates of snapshots and releases.
mvn help:effective-pom Displays the effective POM.
mvn source:jar Generates a source JAR file.
mvn assembly:single Creates a distributable assembly like ZIP/TAR.

Learn More About Plugins

This cheat sheet covers the essential Maven commands you need for Java development, including creating projects, managing dependencies, running tests, and building applications. Use this guide as a quick reference to streamline your Maven usage.

Comments