Maven中mvn install与mvn verify的区别及clean关键字的作用
Hey there! Let's break down the differences between mvn install and mvn verify, plus how the clean keyword alters their behavior—this is foundational stuff for smooth Maven workflows.
mvn verify vs mvn install First, remember Maven follows a defined build lifecycle. Both commands run sequential phases, but they stop at different points with distinct goals:
mvn verify
- This command executes all phases from
validateup toverifyin the default lifecycle. That means:- Validates project structure and dependencies
- Compiles source code
- Runs unit tests
- Packages code into a jar/war/ear
- Runs integration tests
- Verifies the packaged artifact meets quality standards (like checking test coverage, code compliance, or custom validation rules)
- The key takeaway:
verifyensures your code and build output are valid and meet project requirements, but it doesn't save the artifact to your local Maven repository.
mvn install
- This command goes a step further: it runs all phases up to
install, which includes everythingverifydoes. The extra step?- Installs the packaged artifact (jar/war/etc.) into your local Maven repository (usually located at
~/.m2/repository).
- Installs the packaged artifact (jar/war/etc.) into your local Maven repository (usually located at
- Why this matters: Once installed, other local projects can reference this artifact as a dependency in their
pom.xml—super useful if you're working on multiple related projects.
To put it simply: verify is about "is this build good?"; install is about "this build is good, now let's make it available for other local projects to use."
clean Keyword Modifies These Commands clean is a separate Maven lifecycle that operates independently of the default build lifecycle. When you prepend clean to either command, here's what happens:
- First, Maven runs the entire
cleanlifecycle: This deletes thetargetdirectory in your project root. Thetargetfolder stores all previous build outputs—compiled classes, packaged artifacts, test reports, etc. - Then, it executes the subsequent build command (
verifyorinstall) from scratch.
Let's break down the modified commands:
mvn clean verify: Starts with a fresh slate by wiping old build artifacts, then runs the full validation-to-verification flow. This eliminates any stale files that might cause unexpected behavior (e.g., a leftover compiled class from a failed build overriding your new code).mvn clean install:同样先清空target目录,再执行到install阶段。这保证了你安装到本地仓库的是完全基于最新代码的构件,不会出现"我改了代码但本地仓库还是旧版本"的尴尬情况。
Pro tip: If you ever run into weird build issues (like changes not showing up in the output), adding clean is often the first fix to try—it clears out any cached or outdated build files.
内容的提问来源于stack exchange,提问作者Nicholas

