Table of contents
1.
Introduction  
2.
What is Maven?
3.
What does Apache Maven do?
4.
Basic Maven Command
5.
Intermediate Maven Command
6.
Advanced Maven Commands
7.
Frequently Asked Questions
7.1.
How do I run the Maven test class?
7.2.
What does "super POM" refer to?
7.3.
What is a Maven repository?
7.4.
What are the life cycles for Maven?
7.5.
What is Dependency Exclusion?
8.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Introduction to Maven Command

Author Ayush Mishra
0 upvote

Introduction  

You have probably seen a variety of popular open-source build tools to simultaneously build, publish, and deploy multiple projects at once for better project management. However, are you familiar with Maven?

This blog will introduce you to Maven and its commands in detail. Let's start going!

Introduction to Maven Command

What is Maven?

Maven is the project management tool by Apache Software Foundation, which gives build cycles a complete framework. It is used primarily for Java-based project development and is open-source.

A project object model, or POM, serves as Maven's primary driver. All dependencies are housed in this one location. It is utilized for documentation, dependencies, and project construction.

What does Apache Maven do?

It performs the following task listed below:-

1. It makes building a project simple.

2. It offers a uniform construction process.

3. Moving to new Maven features is simple.

4. It offers project details such as log documents, cross-referenced sources, mailing lists, dependency lists, unit test reports, etc.

Basic Maven Command

Maven command

Let's discuss some of the basic commands in Maven:-

1. mvn --version:- This command enables us to determine the installed version of Maven.

mvn --version

2. mvn site:- This command creates a website using the data from the project's pom.

mvn site

3. mvn test:- Maven offers the option of unit testing specific codes. It executes the tests using the proper testing frameworks.

 mvn test

3. mvn compile:- It is used to compile the source code. Additionally, it compiles any stored classes at a specific target or class.

mvn compile

4. mvn deploy:- It is used to deploy the project. All final packages are copied to the remote repository and made accessible to other developers.

mvn deploy

5. mvn install:- It builds the project specified in your Maven POM file and adds the finished artifact (JAR) to your Maven repository.

mvn install

6. mvn install -Dmaven.test.skip=true:- Installs the finished artifact (JAR) into your local Maven repository without building the project specified by your Maven POM file's unit tests.

mvn install -Dmaven.test.skip=true

7. mvn package:- It completes the project's compilation, verification, and construction tasks. It creates the jar file and saves it under the specified project in the designated folder.

mvn package

8. mvn package -Dmaven.test.skip=true:- It builds the project without running the unit tests, then packages the resulting JAR file into the target directory.

mvn package -Dmaven.test.skip=true

9. mvn clean:-  It removes all files from the target directory where Maven creates your project by default.

mvn clean

10. mvn clean package:- It cleans out the target directory before building the project and putting the finished JAR file into the directory.

mvn clean package

11. mvn clean verify:- It runs each integration test found in the project while also cleaning the target directory.

mvn clean verify

Intermediate Maven Command

Maven command

In this part of “Introduction to Maven Command,” we will discuss some intermediary maven commands:-

12. mvn dependency:analyze:- This command can examine the project's dependencies for new prints, out-of-date dependencies, and other issues.

mvn dependency:analyze

13. mvn dependency:tree:- The command "mvn dependency: tree" prints the entire project's dependencies tree. This helps gather all transitive dependencies and, if any conflicts are present due to the version, gather all of them.

mvn dependency:tree

14. prepare-package:- Using the maven command, all operations necessary to prepare any package are carried out.

prepare-package

15. pre-integration-test:- Pre-integration testing carries out all tasks required for the execution of the integration tests. It may also clean up the environment.

pre-integration-test

16. mvn help:describe:- This command assists in obtaining the plugin's attributes. With this maven command, all plugin-related information can be found.

mvn help:describe

17. mvn help:effective-pom:- This command is to obtain the simplest possible pom.xml for a Maven project.

mvn help:effective-pom

Advanced Maven Commands

In this part of "Introduction to Maven Command," we will discuss some advanced commands related to Maven:

18. mvn dependency:build-classpath: Based on the dependencies set up in the pom.xml file, it prints out the classpath required to run your project (application).

mvn dependency:build-classpath

19.  mvn dependency:tree -Dverbose: It prints out your project's dependency tree based on the dependencies specified in the pom.xml file. contains recurring, transitive dependencies.

mvn dependency:tree -Dverbose

20. mvn dependency:tree -Dincludes=com.fasterxml.jackson.core: It gives a list of your project's dependencies that rely on the com.fasterxml.jackson.core artifact.

mvn dependency:tree -Dincludes=com.fasterxml.jackson.core

21. Creating a Maven Wrapper:- Maven Wrappers can be created in two different ways.

cd {your-project}
mvn -N io.takari: Maven:wrapper
Creating a Maven Wrapper

Read about,pwd command in linux

Frequently Asked Questions

How do I run the Maven test class?

To test classes on Maven, you need the surefire plugin. In setting.xml and pom.xml, review your configurations and set up a "test" property.

What does "super POM" refer to?

The term "Super POM" refers to Maven's default POM. It can be created by default or derived from their parents. To accomplish particular objectives, effective POMs are used.

What is a Maven repository?

A directory of metadata-packed JAR files is referred to in the Maven repository. The POM file connected to each project is referred to in the metadata. 

What are the life cycles for Maven?

Validate, compile, test, package, integration test, verify, install, and deploy are the eight steps of the typical Maven lifecycle.

What is Dependency Exclusion?

Transitive dependencies can be excluded using the "exclusion" element. A can be excluded from C if C depends on B and B depends on A.

Conclusion 

Congratulations on finishing the blog! We have discussed the introduction to maven commands. We further discussed basic, intermediate, and advanced commands in Maven.

We hope this blog has helped you enhance your knowledge of maven commands. Do not stop learning! We recommend you read some of our maven articles: 

1.Maven Interview Questions

2.DropWizard- Setting up using Maven

3.How to install Maven on Windows


Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. For placement preparations, you must look at the problemsinterview experiences, and interview bundles.

We wish you Good Luck! 

Happy Learning!

Live masterclass