Apache Maven is more than just mvn clean install.
Understanding its build lifecycle and the key commands lets you control every stage of compilation, testing, and packaging for projects like Spring Boot apps.
  
  
  1️⃣ Maven Build Lifecycles at a Glance
Maven has three standard lifecycles.
Each consists of a series of ordered phases—invoking a later phase runs all previous phases automatically.
| Lifecycle | 
Purpose | 
Key Phases | 
| default | 
Build, test, and package the project. | 
validate → compile → test → package → verify → install → deploy | 
| clean | 
Remove previous build output. | 
pre-clean → clean → post-clean | 
| site | 
Generate project documentation. | 
pre-site → site → post-site → site-deploy | 
 
💡 Example: mvn package runs all phases up to and including package (validate, compile, test, then package).
  
  
  2️⃣ Most-Used Maven Commands
Below are the core commands you’ll reach for every day, grouped by purpose.
  
  
  🧹 Cleaning
| Command | 
What it Does | 
mvn clean | 
Deletes the target/ directory to ensure a fresh build. | 
mvn clean compile | 
Cleans and compiles the code. | 
mvn clean install | 
Cleans, builds, runs tests, and installs the artifact to your local ~/.m2 repository. | 
 
  
  
  💻 Compilation & Testing
| Command | 
Action | 
mvn compile | 
Compiles main source (src/main/java) to target/classes. | 
mvn test-compile | 
Compiles test sources (src/test/java). | 
mvn test | 
Runs unit tests with Surefire after compiling. | 
mvn verify | 
Runs integration checks (e.g., failsafe plugin). | 
 
  
  
  📦 Packaging & Installation
| Command | 
Action | 
mvn package | 
Packages code into the format defined in pom.xml (jar or war) inside target/. | 
mvn install | 
Installs the packaged artifact into your local Maven repo so other local projects can depend on it. | 
mvn deploy | 
Uploads the built artifact to a remote repository (e.g., Nexus, Artifactory). | 
 
  
  
  ⚙️ Running Applications (Spring Boot)
| Command | 
Action | 
mvn spring-boot:run | 
Compiles and launches the app directly from source without creating a jar. | 
mvn spring-boot:repackage | 
Creates an executable fat jar with all dependencies. | 
 
  
  
  📚 Project Metadata & Utilities
| Command | 
Action | 
mvn validate | 
Checks that the project structure and pom.xml are correct. | 
mvn dependency:tree | 
Shows the full dependency graph (great for conflict analysis). | 
mvn dependency:analyze | 
Detects unused or undeclared dependencies. | 
mvn help:effective-pom | 
Displays the fully merged POM after inheritance and profiles. | 
mvn versions:display-dependency-updates | 
Lists newer versions of your dependencies. | 
mvn clean site | 
Generates project reports under target/site. | 
 
  
  
  3️⃣ Typical Daily Workflows
| Goal | 
Recommended Command | 
| Fresh local build + run tests | 
mvn clean install | 
| Skip tests during build | 
mvn clean install -DskipTests (compiles tests but doesn’t run them)
 | 
| Build without compiling/running tests | 
mvn clean package -Dmaven.test.skip=true | 
| Quick dev run of Spring Boot | 
mvn spring-boot:run | 
 
  
  
  4️⃣ Tips for Pro Users
- 
Parallel Builds: 
mvn -T 4 clean install uses 4 threads for faster builds. 
- 
Profiles: Activate build profiles: 
mvn package -Pprod. 
- 
Offline Mode: 
mvn -o package builds without hitting remote repos. 
- 
Debug Output: 
mvn -X package shows detailed logs for troubleshooting. 
  
  
  🏁 Key Takeaways
- Maven operates through lifecycles → phases → goals.
 
- Running a later phase (e.g., 
package) implicitly executes all earlier phases. 
- Learn a handful of commands (
clean, compile, test, package, install, deploy, plus Spring Boot plugin goals) to cover 99% of daily tasks. 
Mastering these commands keeps your builds predictable and production-ready—whether you’re packaging a simple library or a full-blown Spring Boot microservice.
             
              
Top comments (0)