Maven Dependency Scopes: compile, test, provided, runtime
Fast recall
Quick revision
Core answer
compile (default) is everywhere; test is test classpath only; provided is compile classpath but excluded from the package (container provides it); runtime is runtime and package but not compile.Whether you understand how to correctly declare dependencies so the packaged artifact does not include unnecessary JARs.
Using `compile` scope for a Servlet API or Lombok dependency that should be `provided`, which causes conflicts when the artifact is deployed to an application server.
Mention the `import` scope for importing BOMs (Bill of Materials) in `<dependencyManagement>`, and the `system` scope (deprecated, avoid), and how transitive scope rules work (e.g. a `runtime` dependency of a `compile` dependency becomes `runtime` in your project).
Interview-ready guide
Interview answer
Maven's five dependency scopes control which classpaths a dependency appears on and whether it ships in the packaged artifact. Getting scopes wrong is a common source of both bloated artifacts and production ClassNotFoundException errors.
Compile and test — the most common scopes
compile(the default) goes on every classpath: compile, test, and runtime; it ships in the final JAR — use it for core libraries your code directly imports and consumers of your library also needtestrestricts the dependency to test compilation and execution only — JUnit, Mockito, AssertJ belong here; they never appear in the final package and never reach production; forgettingtestscope on Mockito ships it to production unnecessarilyruntimeis not needed to compile but must be on the runtime classpath — JDBC drivers fit perfectly because you code againstjava.sqlinterfaces, not the driver classes; compile scope on a driver adds unnecessary compile-time coupling
Provided — the shared-runtime trap
providedmeans the container or runtime environment supplies the dependency — the Servlet API on Tomcat, Lombok during annotation processing; the dependency is on the compile classpath but excluded from the final package- Packaging a Spring Boot fat JAR with Tomcat as
compilescope includes an embedded container; packaging it asprovidedcorrectly excludes it when deploying to an external Tomcat - Lombok must be
provided(oroptional) — including it ascompileships it to consumers of your library who may be using a different version
Scope verification before deployment
mvn dependency:tree -Dscope=runtimeshows exactly what ships in the final artifact — run this before every release to catch accidentally included test or compile-only dependencies- The most painful scope bugs surface as
ClassNotFoundExceptionin production: a library wasprovidedin a context where no container supplied it, ortestscoped in a module that shipped it to a WAR
Maven Dependency Scopes: compile, test, provided, runtime
Direct Answer
Maven dependency scopes control which classpath a dependency appears on and whether it is included in the packaged artifact. compile (default) is everywhere; test is test classpath only; provided is compile classpath but excluded from the package (container provides it); runtime is runtime and package but not compile.
Deep Dive & Architecture
Java build tools (Maven, Gradle) automate the compile-test-package pipeline and declaratively resolve a project's dependencies from a repository (Maven Central), producing a reproducible, versioned artifact (jar/war) with a single command.
They exist because doing this by hand — javac with a classpath, manually downloading jars, scripting test runs — is unreproducible and unscalable: a 50-dependency Spring Boot app would be impossible to assemble consistently across machines.
The build tool captures the dependency graph and the task order in a declarative file so 'mvn package' or 'gradle build' produces the same artifact everywhere.
Reciting 'Maven is XML, Gradle is Groovy/Kotlin' without explaining the fixed-lifecycle vs. flexible-task-graph distinction or dependency resolution.
Common Mistakes & Pitfalls
Using compile scope for a Servlet API or Lombok dependency that should be provided, which causes conflicts when the artifact is deployed to an application server.
Check your understanding
Answer these without looking back at the article.
- What are Java build tools and why do they exist? They automate the compile-test-package pipeline and resolve dependencies reproducibly — turning source into a versioned artifact with one command.
- What are Maven lifecycle phases (validate, compile, test, package, install, deploy)?
- How does Maven POM inheritance and aggregation work?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.