Maven common operation commands



Recently, when building an automated release pipeline based on maven, I found that when there are many projects and there are many dependencies between projects, understanding Maven’s command use is really a powerful addition. Specially arranged a set of Maven common commands and tips for you. It is recommended to collect or even print it out and put it on your desk for quick use.

No more nonsense, just dry goods.

Maven common command usage

Project directory structure:

--P (parent project POM)
    --A (subproject - common)
    --B (subproject - Biz dependent common)
    --C (subproject - Web dependency B, indirect dependency a)

Create class Command

Create a normal Maven project
mvn archetype:create -DgroupId=packageName -DartifactId=projectName

After creation, you can import the project with IDE.

Create web Maven project
mvn archetype:create -DgroupId=packageName -DartifactId=webappName -DarchetypeArtifactId=maven-archetype-webapp

After creation, you can import the project with IDE.

Publish third-party jars to local libraries
mvn install:install-file -DgroupId=xxx. xx. XXXX - dartifactid = XXX - dversion = 0.0.1 - dpackaging = Jar - dfile = path / xxx-0.0.1 jar

Build package class Command

Compile source code
mvn compile
Compile test code
mvn test-compile
Compile test code
mvn test
Package project
mvn package
Clear packaged items
mvn clean

Package, install, deploy and other commands are often used together with this command.

Clear history and repack
mvn clean package

One of the most commonly used commands.

Clear and install to local
mvn clean install
View the dependencies of the current project that have been resolved
mvn dependency:list

It is often used for jar package dependency conflict troubleshooting, etc.

Upload to private server
mvn deploy

Complex combination command

Force package after check update
mvn clean package -U

-U parameter, which is used to force the check of updates and prevent the use of history dependency due to cache and other reasons. It is often used in automatic release scripts.

Package exclusion unit test
mvn clean package -Dmaven.test.skip=true
Package and specify environment
mvn clean package -P test

Test is the name of different packaging environments configured in Maven.

Package specified child and parent projects
mvn clean install -pl C -am -Dmaven.test.skip=true

Note: execute MVN clean install on parent project P, sub module C, and modules B and a that sub module C depends on. All involved modules P, a, B and C are installed locally.

This group of commands is usually used during automatic construction. For the basic module, replace the install command with the deploy command and publish it to the private server.

Package parent project only
mvn clean install -N

Only the parent project P is packaged, and the sub module will not perform relevant operations. When a parent-child project is built and uploaded to a private server, it is often necessary to build and upload the parent project (POM project) first.

Only package project a
mvn clean install -pl A -am

Since project a does not rely on other modules, only MVN clean install is performed on P and a.

Packaged by dependent projects
mvn clean install -pl A -amd

Execute MVN clean install on parent project P, sub module a and modules B and C that depend on module a. Usage scenario: the basic common module is changed, and other modules that depend on common will also be MVN clean installed again through this command.

Jar package release
mvn clean deploy -Dmaven.test.skip=true

The above command can package the jar package and publish it to the warehouse. At the same time, it needs to be in POM Configure the private warehouse address information to be published through the DistributionManagement element in XML. The password and other information of the private warehouse are in setting XML file.

Specify setting XML file
mvn clean package -s settings.xml
Omnipotent command

Finally, there is a universal command, help command:

MVN - help or MVN - H

With this command, you can view more command operations supported by Maven.

Maven operation in idea

Some operating skills and functions of Maven in idea.

1. Execute commands through terminal

Open the terminal command window of idea. The default is to open the root directory of the current project. You can directly execute the MVN command. It can also be executed by switching directories through commands such as CD.

Maven common operation commands

The advantage of this method is that it is much faster than ide automatic parsing and downloading jar package dependency, and it is not easy to get stuck in half of the download.

2. Maven GUI execution

It can be executed through Maven graphical interface provided by idea:

Maven common operation commands

Double click the corresponding command to execute it. The two circles in the upper left corner can refresh the jar package dependency.

Disable unit tests

When performing a packaged build, you can select the lightning bolt symbol if you do not need to perform unit tests.

Maven common operation commands

ALT or command select multiple commands to execute at the same time:

Maven common operation commands


This article will sort out the common commands and operations when using Maven. I hope you can help. Of course, if you have other black technology commands, you can also interact.

About the blogger: the author of the technical book “inside of springboot technology”, loves to study technology and write technical dry goods articles.

The official account: “new horizon of procedures”, the official account of bloggers, welcome the attention.

Technical exchange: please contact blogger wechat: zhuan2quan