{

Top 20 Maven Commands list | cheatsheet


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. It uses the pom.xml file to manage build dependencies. pom.xml is a project object model that contains dependencies, java version, and plugin information. it is used to compile and build the project. Maven execution runs with the build life cycle such as compiling, executing, testing and packaging java projects.

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. First, Please make sure that you install maven on your system

Frequently used Maven commands

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, WEB-INF, lib, classes folders, and web.xml.

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?

Some times,

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 target directory.

The following command deletes all the contents in the target folder.

mvn  clean

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

mvn  compile

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.

mvn package  

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 jar, or war file.

The final output for this command is the jar/war of your project located in the target folder.

Deploy/install project

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 //.m2//repositories//groupid//.
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

mvn test

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 maven.test.skip or skipTests to true.

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

maven javadoc:javadoc

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.

Debug Maven

mvn -X 

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.

dependency commands

We have many commands to list out a dependency tree that tells about the direct and indirect dependencies of a maven project.

mvn dependency:tree

There is also a command to download all dependencies without doing anything

mvn dependency:resolve

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 profiles

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

For production

mvn clean install -Denvironment=prod

For running with development configuration

mvn clean install -Denvironment=dev

Another way to execute profiles using the -P command

mvn -PprofileName install  

Generate site

site is inbuilt in goal which generates documentation for an artifact.

mvn site 

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 -o

mvn -o install  

Conclusion

We learned frequently used command cheatsheets with all use cases covered.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.