What Maven solves, the POM, dependency scopes (compile vs runtime and more), lifecycles vs phases vs goals, repositories, excluding transitive dependencies, speeding up large builds, running builds, mvn clean vs install, and managing dependencies.
Published September 25, 2026
Maven questions are short, but precise vocabulary matters: lifecycle vs phase vs goal, and scope. Many prepared answers get "Maven has three lifecycle phases" wrong. Maven has three lifecycles, each made of many phases.
Short answer: Maven is a build automation and dependency management tool for Java. It solves three problems:
src/main/java, src/test/java), so any developer knows where everything is.Learn it in depth → CI/CD Pipeline Design
Short answer: pom.xml is the Project Object Model, the XML file that describes the project for Maven:
groupId:artifactId:version) and packaging (jar, war, pom);dependencyManagement;<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.shop</groupId>
<artifactId>order-service</artifactId>
<version>1.4.0-SNAPSHOT</version>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Key points to cover:
spring-boot-starter-parent). mvn help:effective-pom shows the fully merged result.Short answer: A compile-scope dependency (the default) is needed to compile and run the code, and it's on every classpath. A runtime dependency isn't needed to compile, only to run and test. The classic example is a JDBC driver: your code uses javax.sql/JPA interfaces, and the driver is only needed at runtime.
| Scope | Compile | Test | Runtime / packaged | Example |
|---|---|---|---|---|
compile (default) | ✅ | ✅ | ✅ | Spring, Jackson |
provided | ✅ | ✅ | ❌ (the container supplies it) | Servlet API in a WAR, Lombok |
runtime | ❌ | ✅ | ✅ | JDBC drivers |
test | ❌ | ✅ | ❌ | JUnit, Mockito |
import | Only in dependencyManagement, to import a BOM | spring-boot-dependencies |
Key points to cover:
runtime scope stops you accidentally coding against the driver's internal classes.Short answer: Maven has three built-in lifecycles:
clean: removes the previous build output.default (build): from validation through to deployment.site: generates project documentation.Each lifecycle is an ordered list of phases. Running a phase runs every earlier phase in that lifecycle. The key phases of the default lifecycle, in order:
validate → compile → test → package → verify → install → deploy
Plugin goals are bound to phases, and they do the actual work. For example, compiler:compile is bound to compile, surefire:test to test, and jar:jar to package.
Common trap: saying "Maven has three lifecycle phases: clean, default and site". Those are three lifecycles, and the default lifecycle alone has more than 20 phases.
Short answer: A repository stores artifacts (JARs, POMs, plugins), identified by their coordinates. There are three kinds:
~/.m2/repository. A cache on your machine, where mvn install also puts your own builds.Key points to cover:
SNAPSHOT versions are mutable and re-checked. Release versions are immutable.Short answer: Use <exclusions> inside the dependency that pulls in the unwanted transitive dependency.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
Key points to cover:
mvn dependency:tree -Dincludes=groupId:artifactId.<dependencyManagement>.Short answer:
mvn -T 1C install (one thread per core).-pl module-a -am builds a module plus the modules it depends on.-DskipTests (tests are still compiled), -Dmaven.test.skip=true (skips even compiling them). Never skip tests in CI.-o) when dependencies are already cached.~/.m2 in CI.mvnd).Short answer: From the directory containing pom.xml, run a phase, for example mvn package (compile, test and create the JAR or WAR in target/), or mvn verify in CI. Use the Maven Wrapper (./mvnw package), so every machine uses the project's pinned Maven version.
./mvnw clean verify # full, clean build with tests
./mvnw spring-boot:run # run a Spring Boot app
./mvnw -pl order-service -am package -DskipTests
mvn clean and mvn install?Short answer: mvn clean runs the clean lifecycle, and deletes the target/ directory, meaning all previous build output. mvn install runs the default lifecycle up to install: it compiles, tests, packages, verifies, and then copies the artifact into your local repository (~/.m2), so other local projects can depend on it.
Key points to cover:
mvn clean install runs both lifecycles in one command.install is only needed when another local project consumes the artifact. In CI, verify is usually enough, with deploy publishing to the remote repository.Short answer:
<dependencies>, and let Maven resolve the transitive ones.<dependencyManagement>, or import a BOM (spring-boot-dependencies), so all modules agree on versions.<properties>.dependency:tree and dependency:analyze (declared but unused, used but undeclared).mvn install?Short answer: Maven executes, in order, every phase of the default lifecycle up to install: validate, initialize, the source-generation phases, compile, process-classes, test-compile, test (Surefire runs the unit tests), package (builds the JAR), pre-integration-test/integration-test/post-integration-test (Failsafe, if configured), verify, and finally install. Each phase runs whichever plugin goals are bound to it for your packaging type.
Key points to cover:
mvn dependency:tree or mvn surefire:test. That runs only the goal, not the lifecycle before it.Q: What is a SNAPSHOT version?
A: A version still in development (1.4.0-SNAPSHOT). Each deploy can overwrite it, and consumers periodically re-download the latest. A release version (1.4.0) is immutable.
Q: How does Maven resolve version conflicts?
A: By "nearest definition wins" in the dependency tree. At equal depth, the first declaration wins. <dependencyManagement> overrides both, which is why BOMs are so useful.
Q: What is a multi-module project?
A: A parent POM with <packaging>pom</packaging> listing <modules>. It shares configuration and dependency versions, and builds the modules in dependency order (the reactor).
Q: Maven or Gradle? A: Maven is declarative XML, extremely standard, and predictable. Gradle uses a Groovy or Kotlin DSL, with incremental builds and a build cache, so it's faster and more flexible for large or complex builds. Spring Boot supports both equally.