← 返回命令列表

Linux command

mvn 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Build project

mvn package

Clean and build, install to local repository

mvn clean install

Run tests

mvn test

Skip tests during build

mvn install -DskipTests

Generate a new project from archetype

mvn archetype:generate

Deploy artifact to remote repository

mvn deploy

Show dependency tree

mvn dependency:tree

Check for dependency updates

mvn versions:display-dependency-updates

Run with a specific profile

mvn clean install -P [profile-name]

Build offline without downloading dependencies

mvn package -o

说明

mvn is the command-line interface for Apache Maven, a build automation and dependency management tool primarily used for Java projects. It uses a Project Object Model (pom.xml) to describe the project, its dependencies, plugins, and build lifecycle. Maven follows a convention-over-configuration approach with a standard project structure (src/main/java, src/test/java, etc.). The build lifecycle consists of ordered phases: validate, compile, test, package, verify, install, deploy. Specifying a phase executes all preceding phases. Dependencies are resolved from remote repositories (Maven Central by default) and cached in the local repository at ~/.m2/repository. Maven supports multi-module projects, build profiles for environment-specific configurations, and an extensive plugin ecosystem.

参数

clean
Remove target directory with compiled output.
validate
Validate project is correct and all information is available.
compile
Compile project sources.
test
Run unit tests.
package
Package compiled code (JAR, WAR, etc.).
verify
Run integration tests and checks.
install
Install package to local repository (~/.m2/repository).
deploy
Deploy package to remote repository.
-D _PROPERTY=VALUE_
Set a system property (e.g., -DskipTests, -Dmaven.test.skip=true).
-P _PROFILE_
Activate a build profile defined in pom.xml.
-pl _MODULES_, --projects _MODULES_
Build specific modules in a multi-module project.
-am, --also-make
Build required dependent modules when using -pl.
-f _FILE_, --file _FILE_
Use an alternative POM file.
-o, --offline
Work offline without downloading dependencies.
-U, --update-snapshots
Force update of snapshot dependencies.
-T _THREADS_
Thread count for parallel builds (e.g., -T 4, -T 1C for 1 per CPU core).
-X, --debug
Produce debug output.
-q, --quiet
Quiet output, only show errors.
-e, --errors
Produce execution error messages.
-B, --batch-mode
Non-interactive batch mode (recommended in CI environments).
-N, --non-recursive
Do not recurse into sub-projects (build reactor root only).
-s _FILE_, --settings _FILE_
Use alternate user settings file.
-gs _FILE_, --global-settings _FILE_
Use alternate global settings file.
--help
Display help information.

FAQ

What is the mvn command used for?

mvn is the command-line interface for Apache Maven, a build automation and dependency management tool primarily used for Java projects. It uses a Project Object Model (pom.xml) to describe the project, its dependencies, plugins, and build lifecycle. Maven follows a convention-over-configuration approach with a standard project structure (src/main/java, src/test/java, etc.). The build lifecycle consists of ordered phases: validate, compile, test, package, verify, install, deploy. Specifying a phase executes all preceding phases. Dependencies are resolved from remote repositories (Maven Central by default) and cached in the local repository at ~/.m2/repository. Maven supports multi-module projects, build profiles for environment-specific configurations, and an extensive plugin ecosystem.

How do I run a basic mvn example?

Run `mvn package` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does clean do in mvn?

Remove target directory with compiled output.