Maven is a build automation project management tool in java projects.
Most of the people working on java related technologies know about the maven usage.
I am listing down the list of Maven commands with examples reference which is very helpful for the developer.
This list is helpful for all Java developers for their daily usage in projects.
Please have a look at my previous article maven installation.
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 starting step for any Java project to create.
This command runs at the command prompt and creates a project with a name i.e. artifact id.
This project is located in group “org.cloudhadoop” using groupid.
After running this command for the first time, It tries to download all the required artifacts from remote repositories ( configured in pom.xml) and copy them to your local repository, and after, it creates a project.
This project contains the
src/main/java and src/test/java` folders which contain the HelloWorld java program in the main folder and the test class for the HelloWorld program in the test folder.
Creating a web standalone project
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DgroupId=com.cloudhadoop -DartifactId=mywebapp
Most of the time, we usually create a web-based project in eclipse.
With this command, we can create web projects like spring, struts applications that contain,
classes folders, and
This command runs at the command prompt and creates mywebapp project i.e. artifact id.
This project is located in group “org.cloudhadoop” using groupid.
After running this command for the first time, this also tries to download all the required artifacts downloaded from remote repositories and copy them to your local repository and after that, it creates a project.
The directory structure is different from the #1 command and creates as per the Maven directory structure for web applications.
How to clean a java project with maven?
when you run the project with maven install,
target folder generated and it contains all your compiled classes, as well as the copied jar, war files
Clean is a maven predefined goal used in the maven command to remove the previous compiled files and assets in the
The following command deletes all the contents in the
compile maven project
compile is a maven predefined goal and this command compiles all your java classes which include files in the
src folder in your project.
Here is a command
Maven package web application
package goal is used to build maven applications. It is an In-Built predefined goal.
Java projects can be packed as jar or war.
With this command, first, compile all the java files(using compile goal) and run all your test classes and copy all these files to the
target folder and create a
The final output for this command is the
war of your project located in the
mvn install or mvn deploy
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 target folder.
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 the remote repository like the nexus. so that other developers can use this artifact in their module.
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
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 ```markup 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. And the generated java doc report can be found in the target 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 dependency tree which tells about direct and indirect dependencies of a maven 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
<profiles> <profile> <activation> <property> <name>environment</name> <value>prod</value> </property> </activation> </profile> <profile> <activation> <property> <name>environment</name> <value>dev</value> </property> </activation> </profile> </profiles>
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
site is inbuilt in goal which generates documentation for an artifact.
The predefined goal in maven is used to generate site documentation in a formatted style.
How to configure proxies in the command line?
As we have encountered the usage of maven in java projects, the following is the list of maven commands for your reference. Whenever you run the maven command with different goals, It tries to download the different dependencies from the different repositories configured in the XML file.
If the mvn install command is unable to download dependent artifacts from repositories, then use the following command with proxy settings enabled
mvn install -Dhttps.proxyHost="" -Dhttps.proxyPort=""
To run the maven without downloading artifacts from the repositories?
This is an execution of the maven command in offline mode using
mvn -o install
We learned frequently used command cheatsheets with all use cases covered.