Common Maven Commands
Fast recall
Quick revision
Core answer
Practical hands-on Maven knowledge — these are commands used daily in real development work
Not knowing that mvn package also compiles and runs tests first — thinking you need to run three separate commands
Mention mvn dependency:tree as a diagnostic command to see all dependencies including transitive ones, showing practical troubleshooting knowledge
Interview-ready guide
Interview answer
The most commonly used Maven commands are mvn clean, mvn compile, mvn test, mvn package, mvn install, and mvn clean package.
Let me explain what each does. Clean removes the target directory where all your compiled code and build artifacts live, so you start fresh. Compile just compiles your Java source code into class files but doesn't run tests or create a JAR.
Test compiles your code and runs your unit tests — if any test fails, the build stops right there. Package is probably the command you'll use most often because it does everything: compiles your code, runs your tests, and then creates a JAR or WAR file in the target directory.
Install takes it one step further by copying that JAR to your local dot m2 repository, which is useful when you're working on multi-module projects where one module depends on another. And clean package combines clean and package into one command, which is what you use before important builds to make absolutely sure there's no leftover artifacts from previous builds that could cause problems.
One really important thing that trips up freshers is that you don't need to run these commands sequentially. If you run mvn package, Maven automatically runs compile and test first because those are earlier phases in the lifecycle. You don't need to type mvn compile then mvn test then mvn package as three separate commands.
A bonus command that shows you know your stuff is mvn dependency colon tree. This isn't a lifecycle phase, it's a plugin goal that displays your entire dependency tree including all the transitive dependencies. This is super useful when you're debugging dependency conflicts or trying to figure out where a specific JAR is coming from.
Common Maven Commands
Direct Answer
The most common commands are: mvn clean (removes target directory), mvn compile (compiles source), mvn test (runs tests), mvn package (creates JAR or WAR), mvn install (installs to local ~/.m2 repo), and mvn clean package (full clean build).
Code Example
// Project structure for examples
// order-service/
// src/main/java/com/example/OrderService.java
// src/test/java/com/example/OrderServiceTest.java
// pom.xml
// Command 1: Clean the project
$ mvn clean
// Output: Deletes target/ directory
// [INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ order-service ---
// [INFO] Deleting /path/to/order-service/target
// Command 2: Compile source code only
$ mvn compile
// Output: Creates target/classes/ with .class files
// [INFO] --- maven-compiler-plugin:3.11.0:compile (default-compile) @ order-service ---
// [INFO] Compiling 5 source files to /path/to/order-service/target/classes
// Command 3: Run tests (also compiles if needed)
$ mvn test
// Runs validate → compile → test-compile → test
// [INFO] --- maven-surefire-plugin:3.0.0:test (default-test) @ order-service ---
// [INFO] Running com.example.OrderServiceTest
// [INFO] Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
// Command 4: Create JAR (most common for development)
$ mvn package
// Runs all phases through package, creates target/order-service-1.0.0.jar
// Command 5: Install to local repo for other modules to use
$ mvn install
// Copies JAR to ~/.m2/repository/com/example/order-service/1.0.0/
// Command 6: Diagnostic command for dependency issues
$ mvn dependency:tree
// Shows all dependencies including transitive ones
// [INFO] com.example:order-service:jar:1.0.0
// [INFO] +- org.springframework.boot:spring-boot-starter-web:jar:3.2.0
// [INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:3.2.0
// [INFO] | | \- com.fasterxml.jackson.core:jackson-databind:jar:2.15.3
Common Mistakes & Pitfalls
- Running unnecessary sequential commands: Freshers often run mvn compile, then mvn test, then mvn package as three separate commands, not realizing that mvn package automatically runs compile and test. This wastes time and shows you don't understand Maven's phase chaining. Just run mvn package once.
- Always using clean: Some developers habitually run mvn clean package every single time, even during rapid development iterations. Clean deletes everything and forces a full rebuild from scratch, which is slow. During active development, just use mvn package — Maven's incremental compilation is smart enough to only recompile what changed.
- Not knowing diagnostic commands: Many freshers only know the basic build commands and don't know about mvn dependency:tree for debugging dependency issues, or mvn versions:display-dependency-updates to check for outdated dependencies. Learning these diagnostic commands makes you much more effective at troubleshooting real problems.
Check your understanding
Answer these without looking back at the article.
- How do you run a single test with Maven?
- What is mvn verify vs mvn package?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.