Skip to content

Instantly share code, notes, and snippets.

@adojos
Last active March 26, 2024 10:44
Show Gist options
  • Save adojos/f51a3e908b0fe65340b4e99ce3bf3b8e to your computer and use it in GitHub Desktop.
Save adojos/f51a3e908b0fe65340b4e99ce3bf3b8e to your computer and use it in GitHub Desktop.
Maven: Common Maven Commands Reference #maven

Maven Commands Reference

Maven offers a good set of commands and CLI Options to carry out wide range of Dev tasks. Most of these commands are in fact Maven build life cycles, phases and goals.

Here is a list of common Maven commands along with explanation. But before we go through the Maven commands, it is good idea to understand the structure or syntax of Maven commands along with a brief about the Maven lifecycle, phases and goals.


Maven Commands Syntax

Almost every Maven command typically consists of following elements:

  1. Mandatory keyword: mvn
  2. May contain: phases or goals (e.g., compile, package)
  3. May also contain: CLI options/switches (e.g. -o, -q, -f)

For example, the below command executes Maven build phase called 'Compile' in quiet (no output except errors) and offline mode (using local repo cache).

mvn -q -o compile

👉 NOTE: As per best practices, all Maven commands should be executed in the directory which contains the relevant pom file.


Common Maven CLI Options & Properties

Commands Description
mvn --version or mvn -v Prints out the Maven version information
mvn --help Prints Maven help information (manual pages)
mvn -X <build_phase or plugin:goal> Runs the build in the debug mode
mvn -q <build_phase or plugin:goal> Runs the build in the quiet mode, only test cases results and errors are displayed
mvn -o <build_phase or plugin:goal> Runs the build in the offline mode and uses only local repository
mvn -DskipTests <build_phase or plugin:goal> Used to skip the unit test case execution from the build cycle
mvn <build_phase or plugin:goal> -Dmaven.test.skip=true Same as above, used to skip the unit test case execution from the build cycle
mvn <build_phase or plugin:goal> -Denvironment=your_env_name Runs the build with environment parameter as specified in properties in each profile
mvn -PprofileName <build_phase or plugin:goal> Runs the build using the profile parameter as specified in your pom

Maven Build Phases Commands

Commands Description
mvn clean Cleans the maven project by deleting the target directory (files generated by previous build)
mvn validate Validates if the project structure is correct. Also makes sure all dependencies have been downloaded and are available in the local repository
mvn initialize Initialize build state, e.g. set properties or create directories
mvn generate-sources Generate any source code for inclusion in compilation
mvn generate-resources Generate resources for inclusion in the package
mvn compile Compiles all the java source classes of your maven project
mvn generate-test-sources Generate any test source code for inclusion in compilation
mvn generate-test-resources Create resources for testing
mvn test-compile Compile the test source code into the test destination directory
mvn test Runs unit tests against compiled source code using a suitable unit testing framework if available
mvn prepare-package Perform operations necessary to prepare a package before the actual packaging
mvn package Builds the project and packages the results into JAR, WAR etc. in target directory
mvn integration-test Takes the packaged result and executes additional tests such as integration tests for the project
mvn verify Builds the project, runs all test cases as well as checks on the results of the integration tests
mvn install Builds the project and installs resulting artifacts (JAR, WAR, etc) into your local Maven repository
mvn deploy Done in an integration or release environment, copies the final package to the remote repository
mvn site-deploy Deploy the generated site documentation to the specified web server

👉 NOTE: The phases named with hyphenated-words (pre-, post-, or process-*) are not usually directly called from the command line. These phases sequence the build, producing intermediate results that are not useful outside the build.


Maven Build Goals Commands

Commands Description
mvn compiler:compile Calls the compile goal of compiler plugin to compile the java source classes of your maven project
mvn compiler:testCompile Calls the testCompile goal of compiler plugin to compile test classes of the maven project.
mvn archetype:generate Generate a skeleton maven project using archetype plugin's (project templating toolkit) 'generate' goal
mvn site:site Generate project documentation site for the project in 'site' directory inside target
mvn javadoc:javadoc Generate java documentation for your project in 'site' directory inside target
mvn dependency:tree Prints the dependency tree for your Maven project based on the pom.xml file
mvn dependency:analyze Analyzes the maven project to identify & print the unused declared and used undeclared dependencies
mvn dependency:tree -Dverbose Prints the dependency tree for your project based on the pom.xml file. Includes repeated, transitive dependencies
mvn dependency:build-classpath Prints the classpath needed to run your project based on the pom.xml file
mvn dependency:copy-dependencies Copies dependencies from remote Maven repositories to your local Maven repository
mvn dependency:resolve -Dartifact=groupId:artifactId:version Downloads specific artifact dependencies, as specified by the supplied group, artifact id, and version

Reference Links

Apache Maven Build Lifecycle


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment