Maven
Maven Command Line Mastery for Spring Boot Developers
Section titled “Maven Command Line Mastery for Spring Boot Developers”1. Maven Fundamentals - The Command Line Perspective
Section titled “1. Maven Fundamentals - The Command Line Perspective”1.1 Understanding the Maven CLI Structure
Section titled “1.1 Understanding the Maven CLI Structure”mvn [options] [goal(s)] [phase(s)]Basic Command Structure:
mvn clean compile # Run phasesmvn spring-boot:run # Execute plugin goalmvn clean install -DskipTests # With options1.2 Essential Maven Commands for Daily Work
Section titled “1.2 Essential Maven Commands for Daily Work”1.2.1 Project Lifecycle Commands
Section titled “1.2.1 Project Lifecycle Commands”# 1. Create a Spring Boot projectmvn archetype:generate \ -DarchetypeGroupId=org.springframework.boot \ -DarchetypeArtifactId=spring-boot-starter-parent \ -DarchetypeVersion=3.2.0 \ -DgroupId=com.example \ -DartifactId=myapp \ -Dversion=1.0.0
# 2. Compile the projectmvn compile
# 3. Run testsmvn test
# 4. Package the applicationmvn package
# 5. Install to local repositorymvn install
# 6. Clean and rebuildmvn clean compile
# 7. Full build lifecyclemvn clean verify
# 8. Deploy to remote repositorymvn deploy1.2.2 Spring Boot Specific Commands
Section titled “1.2.2 Spring Boot Specific Commands”# Run Spring Boot applicationmvn spring-boot:run
# Run with specific profilemvn spring-boot:run -Dspring-boot.run.profiles=dev
# Run with debug modemvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"
# Run with specific portmvn spring-boot:run -Dspring-boot.run.arguments="--server.port=9090"
# Build executable JARmvn spring-boot:repackage
# Build Docker imagemvn spring-boot:build-image
# Generate build informationmvn spring-boot:build-info1.3 Maven Options - What They Really Do
Section titled “1.3 Maven Options - What They Really Do”# Skip testsmvn clean install -DskipTestsmvn clean install -Dmaven.test.skip=true
# Skip integration testsmvn clean install -DskipITs
# Run only specific testsmvn test -Dtest=UserServiceTestmvn test -Dtest="User*Test" # Pattern matchingmvn test -Dtest="UserServiceTest,ProductServiceTest" # Multiple tests
# Run specific test methodsmvn test -Dtest=UserServiceTest#testCreateUser
# Skip compilationmvn install -Dmaven.compiler.skip=true
# Skip Javadoc generationmvn install -Dmaven.javadoc.skip=true
# Skip source generationmvn install -Dmaven.source.skip=true
# Offline mode (use local cache only)mvn clean install -o
# Quiet mode (less output)mvn clean install -q
# Debug mode (verbose output)mvn clean install -X
# Show errors onlymvn clean install -e
# Force snapshot updatesmvn clean install -U
# Non-recursive (single module)mvn clean install -N
# Resume from specific modulemvn clean install -rf :module-name
# Build without downloading dependenciesmvn clean install -o -nsu
# Custom Maven homemvn clean install -Dmaven.home=/path/to/maven2. Plugin Usage in Practice - Real World Examples
Section titled “2. Plugin Usage in Practice - Real World Examples”2.1 The Spring Boot Maven Plugin - Hands On
Section titled “2.1 The Spring Boot Maven Plugin - Hands On”Let me show you exactly how plugins work through real examples. First, here’s a complete Spring Boot pom.xml with plugin configurations:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId> <artifactId>demo-app</artifactId> <version>1.0.0</version> <packaging>jar</packaging>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> </parent>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
<build> <plugins> <!-- Spring Boot Plugin - The main one! --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>Now let’s use it from command line:
# 1. Run the applicationmvn spring-boot:run
# 2. Run with custom JVM argumentsmvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xmx512m -Xms256m"
# 3. Run with environment variablesmvn spring-boot:run -Dspring-boot.run.environmentVariables="SPRING_PROFILES_ACTIVE=dev,DB_HOST=localhost"
# 4. Run with system propertiesmvn spring-boot:run -Dspring-boot.run.systemPropertyVariables="logging.level.root=DEBUG"
# 5. Package as executable JARmvn clean package spring-boot:repackage
# 6. Build Docker image (requires Docker daemon)mvn spring-boot:build-image -Dspring-boot.build-image.imageName=myapp:latest
# 7. Start application in background (for integration tests)mvn spring-boot:start# ... run integration tests ...mvn spring-boot:stop
# 8. Generate build infomvn spring-boot:build-infoWhat’s happening behind the scenes?
When you run mvn spring-boot:run, Maven:
- Looks for the
spring-boot-maven-pluginin your pom.xml - Finds the
rungoal of that plugin - Executes that goal with any parameters you provided
- The plugin starts your Spring Boot application
2.2 Test Plugins - Maven Surefire & Failsafe
Section titled “2.2 Test Plugins - Maven Surefire & Failsafe”Let’s add test plugins and see how to use them:
<build> <plugins> <!-- Surefire for unit tests --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.1.2</version> </plugin>
<!-- Failsafe for integration tests --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>3.1.2</version> </plugin> </plugins></build>Command Line Usage:
# 1. Run unit tests (Surefire)mvn test
# 2. Run specific test classmvn test -Dtest=UserServiceTest
# 3. Run tests matching patternmvn test -Dtest="*ServiceTest"
# 4. Run specific test methodmvn test -Dtest=UserServiceTest#testCreateUser
# 5. Skip testsmvn install -DskipTests
# 6. Run integration tests (Failsafe)mvn verify
# 7. Run only integration testsmvn failsafe:integration-test
# 8. Run with test groupsmvn test -Dgroups="fast,unit"
# 9. Exclude slow testsmvn test -DexcludedGroups="slow"
# 10. Generate test reportsmvn surefire-report:report2.3 Dependency Plugin - Managing Dependencies
Section titled “2.3 Dependency Plugin - Managing Dependencies”# 1. Show dependency treemvn dependency:tree
# 2. Show tree for specific dependencymvn dependency:tree -Dincludes=org.springframework:spring-core
# 3. Find dependency conflictsmvn dependency:tree -Dverbose
# 4. Analyze dependencies for issuesmvn dependency:analyze
# 5. Copy dependencies to directorymvn dependency:copy-dependencies
# 6. Copy with specific scopemvn dependency:copy-dependencies -DincludeScope=runtime
# 7. List all dependenciesmvn dependency:list
# 8. Resolve specific artifactmvn dependency:get -Dartifact=org.springframework.boot:spring-boot-starter-web:3.2.0
# 9. Purge local repository cachemvn dependency:purge-local-repository
# 10. Build classpath filemvn dependency:build-classpath -Dmdep.outputFile=classpath.txt2.4 Versions Plugin - Managing Updates
Section titled “2.4 Versions Plugin - Managing Updates”# 1. Check for dependency updatesmvn versions:display-dependency-updates
# 2. Check for plugin updatesmvn versions:display-plugin-updates
# 3. Check for property updatesmvn versions:display-property-updates
# 4. Update to latest versionsmvn versions:use-latest-versions
# 5. Update to latest releases (not snapshots)mvn versions:use-releases
# 6. Update to next snapshotmvn versions:use-next-snapshots
# 7. Set specific versionmvn versions:set -DnewVersion=2.0.0
# 8. Revert version changemvn versions:revert
# 9. Commit version changemvn versions:commit
# 10. Update parent versionmvn versions:update-parent2.5 Jacoco Plugin - Code Coverage
Section titled “2.5 Jacoco Plugin - Code Coverage”Add Jacoco to pom.xml:
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.10</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>verify</phase> <goals> <goal>report</goal> </goals> </execution> </executions></plugin>Command Line Usage:
# 1. Run tests with coveragemvn clean test
# 2. Generate coverage reportmvn jacoco:report
# 3. Check coverage thresholdsmvn jacoco:check
# 4. Generate HTML reportmvn jacoco:report -Djacoco.outputDirectory=target/coverage
# 5. Dump execution datamvn jacoco:dump
# 6. Merge multiple execution filesmvn jacoco:merge
# 7. Set custom coverage limitsmvn jacoco:check -Djacoco.minLineCoverage=0.8 -Djacoco.minBranchCoverage=0.73. Real-World Plugin Scenarios for Spring Boot
Section titled “3. Real-World Plugin Scenarios for Spring Boot”3.1 Development Workflow Example
Section titled “3.1 Development Workflow Example”Here’s a complete development session with Maven commands:
# Start a new day - clean and updatemvn clean compile -U
# Run unit testsmvn test
# Start application with dev profilemvn spring-boot:run -Dspring-boot.run.profiles=dev
# In another terminal - run specific testmvn test -Dtest=UserControllerTest
# Make changes, then rebuildmvn clean compile
# Run integration testsmvn verify
# Check for dependency updatesmvn versions:display-dependency-updates
# Package for deploymentmvn clean package
# Check code coveragemvn jacoco:check
# Build Docker imagemvn spring-boot:build-image3.2 CI/CD Pipeline Commands
Section titled “3.2 CI/CD Pipeline Commands”# Clean build with all checksmvn clean verify
# With security scanningmvn clean verify org.owasp:dependency-check-maven:check
# With code quality checksmvn clean verify \ spotbugs:check \ pmd:check \ checkstyle:check
# Build and push Docker imagemvn clean package spring-boot:build-image \ -Dspring-boot.build-image.imageName=myregistry/myapp:$BUILD_NUMBER \ -Dspring-boot.build-image.publish=true \ -Dspring-boot.build-image.registryUsername=$DOCKER_USER \ -Dspring-boot.build-image.registryPassword=$DOCKER_PASS
# Generate site documentationmvn site
# Deploy to repositorymvn deploy -DaltDeploymentRepository=snapshots::default::https://repo.example.com/snapshots3.3 Debugging and Troubleshooting Commands
Section titled “3.3 Debugging and Troubleshooting Commands”# 1. See what Maven is doing (debug mode)mvn clean install -X
# 2. See effective POM (what Maven actually sees)mvn help:effective-pom
# 3. See effective settingsmvn help:effective-settings
# 4. Check dependency resolutionmvn dependency:resolve
# 5. Display plugin helpmvn help:describe -Dplugin=org.springframework.boot:spring-boot-maven-plugin
# 6. Show plugin goalsmvn spring-boot:help
# 7. Show full dependency treemvn dependency:tree > tree.txt
# 8. Profile activationmvn help:active-profiles
# 9. Check environmentmvn help:system
# 10. Dry run (simulate)mvn clean install -DdryRun=true4. Advanced Plugin Combinations
Section titled “4. Advanced Plugin Combinations”4.1 Multi-Module Project Commands
Section titled “4.1 Multi-Module Project Commands”# 1. Build all modulesmvn clean install
# 2. Build specific modulemvn clean install -pl module-name
# 3. Build module and its dependenciesmvn clean install -pl module-name -am
# 4. Build module and dependentsmvn clean install -pl module-name -amd
# 5. Resume from failed modulemvn clean install -rf failed-module
# 6. Skip modulemvn clean install -DskipModule=module-to-skip
# 7. Parallel buildmvn clean install -T 4
# 8. Thread count per coremvn clean install -T 1C
# 9. Build reactormvn clean install -r
# 10. List modulesmvn help:evaluate -Dexpression=project.modules -q -DforceStdout4.2 Profile-Based Execution
Section titled “4.2 Profile-Based Execution”<!-- In pom.xml --><profiles> <profile> <id>dev</id> <properties> <spring.profiles.active>dev</spring.profiles.active> </properties> </profile> <profile> <id>prod</id> <properties> <spring.profiles.active>prod</spring.profiles.active> </properties> </profile></profiles># 1. Activate dev profilemvn clean install -Pdev
# 2. Multiple profilesmvn clean install -Pdev,integration
# 3. Activate by propertymvn clean install -Denvironment=dev
# 4. List all profilesmvn help:all-profiles
# 5. Show active profilesmvn help:active-profiles
# 6. Profile with Spring Bootmvn spring-boot:run -Pdev -Dspring-boot.run.profiles=dev4.3 Custom Plugin Execution
Section titled “4.3 Custom Plugin Execution”Let’s create a custom execution in pom.xml and use it:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <executions> <execution> <id>dev-run</id> <goals> <goal>run</goal> </goals> <configuration> <jvmArguments> -Xmx512m -Dspring.profiles.active=dev </jvmArguments> </configuration> </execution> <execution> <id>prod-build</id> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>prod</classifier> </configuration> </execution> </executions></plugin># Run specific executionmvn spring-boot:run@dev-run
# Skip specific executionmvn package -DskipProdBuild
# List executionsmvn help:describe -Dplugin=spring-boot -Ddetail5. Practical Examples & Recipes
Section titled “5. Practical Examples & Recipes”5.1 Complete Development Session Example
Section titled “5.1 Complete Development Session Example”Scenario: You’re developing a Spring Boot REST API
# 1. Clone and setupgit clone https://github.com/example/spring-boot-api.gitcd spring-boot-api
# 2. First buildmvn clean compile
# 3. Run testsmvn test
# 4. Start application with auto-reload (devtools)mvn spring-boot:run -Dspring-boot.run.profiles=dev
# 5. In another terminal - run specific testmvn test -Dtest=UserControllerIntegrationTest
# 6. Check coveragemvn jacoco:reportopen target/site/jacoco/index.html
# 7. Check for security vulnerabilitiesmvn org.owasp:dependency-check-maven:check
# 8. Update dependenciesmvn versions:display-dependency-updates
# 9. Build for productionmvn clean package -Pprod
# 10. Build Docker imagemvn spring-boot:build-image -Dspring-boot.build-image.imageName=myapi:v1.0.05.2 Performance Optimization Commands
Section titled “5.2 Performance Optimization Commands”# 1. Parallel buildmvn clean install -T 4
# 2. Use daemon (mvnDaemon)export MAVEN_OPTS="-Dmaven.ext.class.path=$HOME/.m2/extensions/maven-daemon.jar"mvn clean install
# 3. Skip unnecessary pluginsmvn clean install -Dmaven.test.skip=true -Dmaven.javadoc.skip=true
# 4. Use local repository onlymvn clean install -o
# 5. Incremental compilationmvn compiler:compile
# 6. Build only changed modulesmvn clean install -pl changed-module -am
# 7. Use build cachemvn clean install -Dmaven.build.cache.enabled=true
# 8. Profile buildmvn clean install -Dmaven.perf.profile=true5.3 Troubleshooting Common Issues
Section titled “5.3 Troubleshooting Common Issues”# Issue: Dependency conflictmvn dependency:tree -Dverbose | grep conflictmvn dependency:analyze-duplicate
# Issue: Build failuremvn clean install -e -X 2>&1 | tee build.log
# Issue: Plugin not foundmvn help:effective-pom | grep pluginmvn dependency:resolve-plugins
# Issue: Memory problemsexport MAVEN_OPTS="-Xmx2048m -XX:MaxPermSize=512m"mvn clean install
# Issue: Slow downloads# Create settings.xml with mirrorcat > ~/.m2/settings.xml << EOF<settings> <mirrors> <mirror> <id>aliyun</id> <name>Aliyun Maven Mirror</name> <url>https://maven.aliyun.com/repository/public</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors></settings>EOF
# Issue: Tests failing intermittentlymvn test -Dtest=FlakyTest -DfailIfNoTests=falsemvn surefire:test -DreuseForks=false
# Issue: Can't find main classmvn spring-boot:run -Dspring-boot.run.main-class=com.example.Application6. Essential Cheat Sheet
Section titled “6. Essential Cheat Sheet”6.1 Daily Commands
Section titled “6.1 Daily Commands”# Buildmvn clean compilemvn clean testmvn clean packagemvn clean install
# Spring Bootmvn spring-boot:runmvn spring-boot:build-image
# Testingmvn testmvn test -Dtest=SpecificTestmvn verify
# Dependencymvn dependency:treemvn dependency:analyze
# Helpmvn help:effective-pommvn -v6.2 Plugin Goal Reference
Section titled “6.2 Plugin Goal Reference”spring-boot:run # Run applicationspring-boot:build-image # Build Docker imagespring-boot:repackage # Create executable jar
surefire:test # Run unit testsfailsafe:integration-test # Run integration tests
dependency:tree # Show dependency treedependency:analyze # Analyze dependencies
versions:display-dependency-updates # Check updates
jacoco:report # Generate coverage report
clean:clean # Clean target directorycompiler:compile # Compile source code
install:install # Install to local repodeploy:deploy # Deploy to remote repo6.3 Common Options
Section titled “6.3 Common Options”-DskipTests # Skip tests-DskipITs # Skip integration tests-Pprofile-name # Activate profile-pl module # Build specific module-am # Also build dependencies-T 4 # Parallel build with 4 threads-X # Debug output-e # Error output-o # Offline mode-U # Update snapshots7. Pro Tips & Best Practices
Section titled “7. Pro Tips & Best Practices”7.1 Shell Aliases for Productivity
Section titled “7.1 Shell Aliases for Productivity”Add these to your ~/.bashrc or ~/.zshrc:
# Maven aliasesalias mci='mvn clean install'alias mcit='mvn clean install -DskipTests'alias mcp='mvn clean package'alias mct='mvn clean test'alias mbr='mvn spring-boot:run'alias mbi='mvn spring-boot:build-image'alias mdt='mvn dependency:tree'alias mdup='mvn versions:display-dependency-updates'alias mdebug='mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"'
# Run with dev profilealias mbrd='mvn spring-boot:run -Dspring-boot.run.profiles=dev'
# Git + Mavenalias gmci='git pull && mvn clean install'7.2 Maven Wrapper Usage
Section titled “7.2 Maven Wrapper Usage”# Initialize wrappermvn wrapper:wrapper
# Use wrapper (no need for global Maven install)./mvnw clean install./mvnw spring-boot:run
# Update wrapper./mvnw wrapper:wrapper -Dmaven=3.9.57.3 Environment-Specific Commands
Section titled “7.3 Environment-Specific Commands”# Developmentmvn spring-boot:run -Dspring-boot.run.profiles=dev
# Testingmvn verify -Pintegration-test
# Stagingmvn clean package -Pstaging -DskipTests
# Productionmvn clean package -Pprodmvn spring-boot:build-image -Dspring-boot.build-image.imageName=prod/myapp:latest7.4 One-Liners for Common Tasks
Section titled “7.4 One-Liners for Common Tasks”# Quick restartmvn clean compile spring-boot:run
# Run single test with coveragemvn clean test -Dtest=UserServiceTest jacoco:report
# Check for updates and securitymvn versions:display-dependency-updates org.owasp:dependency-check-maven:check
# Build and push Dockermvn clean package spring-boot:build-image -Dspring-boot.build-image.publish=true
# Full CI pipelinemvn clean verify spotbugs:check pmd:check checkstyle:check jacoco:checkThe Maven Philosophy for Spring Boot Developers
Section titled “The Maven Philosophy for Spring Boot Developers”Remember these key principles:
- Maven is declarative - You declare what you want in pom.xml, Maven figures out how
- Plugins do the work - Every task is executed by a plugin goal
- Lifecycle drives execution - Phases trigger goals in sequence
- Convention over configuration - Defaults work well for Spring Boot
When you run mvn spring-boot:run:
- Maven finds the
spring-boot-maven-plugin - Executes its
rungoal - The plugin starts your Spring Boot app with embedded server
- You get live reload with spring-boot-devtools
When you run mvn clean install:
clean:clean- Deletes target directoryresources:resources- Copies resourcescompiler:compile- Compiles Java coderesources:testResources- Copies test resourcescompiler:testCompile- Compiles test codesurefire:test- Runs unit testsjar:jar- Creates JAR filespring-boot:repackage- Makes it executable (if configured)install:install- Installs to local repository