Top 20 Maven Commands list | cheatsheet
Maven is a build tool used in Java projects for building and managing tasks.
Many people familiar with Java technologies are aware of
Maven. It relies on a file called
pom.xml to manage build dependencies. This file, known as a
project object model (POM), includes information about
plugins. It plays a role in compiling and building the project.
When Maven is executed, it follows the build life cycle, which includes steps like compiling, executing, testing, and packaging Java projects.
Here is a list of Maven commands with examples that can be very helpful for developers. This compilation is valuable for Java developers in their day-to-day project work.
List down all the Maven command cheat sheets and examples.
- How to create a sample maven standalone project
- Creating a sample web standalone project
- Clean project
- Compile project
- Building web apps
- Deploy project
- Run unit and integration tests
- Ignore test execution
- Generate Java documentation for the project
- Maven debug
- dependency tree and download
- maven profiles
- Generate site
Creating a Maven standalone project
mvn archetype:generate -DgroupId=org.cloudhadoop -DartifactId=myproject
This is the initial step for creating a standalone Java project.
By running this command in the command prompt, a project is created with a specified name, known as the artifact ID(
myproject). The project is located in the group
org.cloudhadoop, identified by the group ID.
Upon the first execution of this command, it attempts to download all necessary artifacts from remote repositories (configured in pom.xml) and saves them to your local repository. Subsequently, it creates the project structure.
The project includes two folders:
src/main/java contains the HelloWorld Java program, and
src/test/java contains the test class for the HelloWorld program.
Creating a web standalone project
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes
Often, we create web-based projects in Eclipse, especially those based on frameworks like Spring or Struts.
With this command, web projects are generated, and have folders like WEB-INF, lib, classes, and web.xml file.
Executing this command in the command prompt results in the creation of a project named
mywebapp, identified by the artifact ID. The project is situated in the group
org.cloudhadoop, specified by the group ID.
Upon the initial execution of this command, it tries to download required artifacts from remote repositories and saves them to your local repository. Subsequently, it creates the project.
The directory structure differs from the first command and adheres to the Maven folder structure for web applications.
How to clean a Java project with Maven?
When you install the project with
maven install the first time, a
target folder is created, containing all compiled classes along with copied JAR and WAR files.
clean is a predefined Maven goal used in the Maven command to erase previously compiled files and assets within the
target directory and remove
The command below is used to delete all contents in the
target folder including the target folder.
compile maven project
The compile goal is a Maven predefined task, and executing this command compiles all Java classes found in the src and test folders of a project. It generates class files stored in the target directory.
Here is the command:
Maven package web application
The package goal is an in-built predefined goal in Maven, forming a part of the build lifecycle for Maven projects. This goal is essential for building Maven applications and is responsible for creating either a war or jar file from the project.
Java projects can be packaged as either a JAR or WAR file.
With this command, the process begins by compiling all Java files (utilizing the compile goal), running all test classes, and copying these files to the target folder. Subsequently, a jar or war file is created.
The ultimate result of executing this command is the jar or war file of your project, which can be found in the target folder.
mvn install or
When we run this command under your project, it will do all the tasks in the
mvn package and create the required jar/war file in the
Maven install goal is used to deploy the project(jar/war) to the local repository. and the local repository location is
Maven deploy goal is used to deploy the project to a remote repository like the nexus. so that other developers can use this artifact in their modules.
The remote repository location is specified in pom.xml.
Run unit and integration tests
The test goal is used to run only the test classes in your project. The test files are located under src/test/java and copy the result to your target/test-classes, target/reports if any.
To run a single test class using Maven
You can provide a single test class using the
mvn clean test -Pintegration-test -Dtest=java-test-class
To run the maven by skipping unit and integration tests**
You can disable test case execution with
mvn clean install -Dmaven.test.skip=true or
mvn clean install -DskipTests=true
or with a package goal
mvn package –DskipTests or
mvn package -Dmaven.test.skip
In 4#, with the maven package command run, tests are also executed. If you want to skip the unit tests, we can use this command. You can use this option with the installation goal also.
Generate Java documentation for the project
This will generate Java documentation for your project. The generated Java doc report can be found in the folder.
This will include API documentation for your Java classes in your project.
This command is used to start the maven goals in debug mode and gives logging information. This command gives more information like what artifact is failing for what reason.
This command can be used when you are not getting any clue about your Maven project execution failure.
We have many commands to list out a dependency tree that tells about the direct and indirect dependencies of a Maven project. It lists out all dependencies with their versions of a project
There is also a command to download all dependencies without doing anything
to download specific artifact dependencies, we have to provide group, artifact id, and version as given below.
mvn dependency:resolve -Dartifact=groupId:artifactId:version
Maven provides the specific profiles based on user/environment and global
Suppose your project wants different settings for Production and Development.
You can define the profiles in pom.xml
You can configure environment properties in each profile, This will be activated with command-line option -D.
The same can be activated with the following commands
mvn clean install -Denvironment=prod
For running with development configuration
mvn clean install -Denvironment=dev
Another way to execute profiles using the
mvn -PprofileName install
The site goal is an in-built goal in Maven that generates documentation for an artifact. It is a predefined goal within the Maven build lifecycle, specifically used for generating site documentation in a formatted style with HTML pages.
How to configure proxies in the command line?
In organizations where proxies are enabled to control access to external networks, it becomes essential to configure proxy settings in the Maven configuration file.
When you execute Maven commands with various goals, the tool attempts to download different dependencies from repositories configured in the XML file.
mvn install command encounters issues in downloading dependent artifacts due to proxy restrictions, consider using the following command with proxy settings enabled.
mvn install -Dhttp.proxyHost="" -Dhttp.proxyPort=""
Another approach to configuring proxy settings globally is by utilizing the settings.xml file in the
<!-- Http Proxy setting configuration-->
<!-- HTTPS Proxy configuration details -->
To run the maven without downloading artifacts from the repositories?
This command executes Maven in
offline mode using the
-o option, which allows the project to run without downloading dependencies from the remote repository.
This option proves valuable in local development scenarios where all the necessary dependencies are available locally. It significantly speeds up the build time, promoting faster development. The
-o option is an alias for
mvn -o install
We have covered frequently used command cheatsheets with all relevant use cases.