Quick revision
The short answer
compile is the default scope: the dependency is on every classpath and is transitive. provided is on the compile and test classpaths but expected from the runtime, like the servlet API in a war, and is not transitive; runtime is only needed to run, like a JDBC driver, and is transitive as runtime. test is only for compiling and running tests and is not transitive. system points to a file on disk and is discouraged, and import is only valid for a pom-type dependency inside dependencyManagement, where it pulls in that POM's managed versions.
What to remember
In this answer, we saw that scopes decide which classpaths a dependency joins and whether it is passed on. compile is everywhere and transitive; provided is for compiling and testing against something the runtime supplies and is not transitive; runtime is for running only and transitive as runtime; test is for tests only and not transitive. system points to a local file and should be avoided, and import is only for pom-type entries in dependencyManagement, where it merges a BOM's managed versions into our project.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
compile is the default scope: the dependency is on every classpath and is transitive. provided is on the compile and test classpaths but expected from the runtime, like the servlet API in a war, and is not transitive; runtime is only needed to run, like a JDBC driver, and is transitive as runtime. test is only for compiling and running tests and is not transitive. system points to a file on disk and is discouraged, and import is only valid for a pom-type dependency inside dependencyManagement, where it pulls in that POM's managed versions.
Background to this question
A dependency scope tells Maven where a library is needed: when compiling, when testing, when running, and whether projects that depend on ours should get it too. Getting scopes wrong causes some of the most confusing build problems: a ClassNotFoundException in production for a library that compiled fine, a war with two copies of the servlet API, or test libraries leaking into a production jar. In this answer, we will recap the three classpaths Maven builds, go through each scope with an example, explain transitivity with a table, and then look at the two special scopes, system and import.
1A Quick Recap: Three Classpaths
A classpath is the list of jars and folders the Java compiler or the JVM searches for classes. Maven builds different classpaths for different tasks. The compile classpath is used to compile src/main/java. The test classpath is used to compile and run src/test/java, and contains everything from the compile classpath plus test libraries. The runtime classpath is what the application needs to run, and is what packaging plugins put into an executable jar or a war's WEB-INF/lib.
A scope says which of these classpaths a dependency joins. Transitivity is the second question: when another project depends on our artifact, which of our dependencies does it also get, and with which scope? Maven reads our published POM to decide.
2Compile, provided, runtime and test
Here are the four everyday scopes in one POM, with the reason for each.
| Scope | Compile | Test | Runtime / packaged | Transitive |
|---|---|---|---|---|
| compile | yes | yes | yes | yes, as compile |
| provided | yes | yes | no | no |
| runtime | no | yes | yes | yes, as runtime |
| test | no | yes (test only) | no | no |
| system | yes | yes | no | no; use is discouraged |
| import | - | - | - | only versions, in dependencyManagement |
compile is the default when we write no scope. The library is on all three classpaths and is passed on to anyone who depends on us, because our public classes may expose its types.
provided means we compile against the library, but at runtime something else provides it. The classic case is the servlet API in a war deployed to Tomcat or another application server: the server already contains it, and a second copy in WEB-INF/lib can cause class loading conflicts. So it is on the compile and test classpaths but not packaged, and it is not transitive, because whoever uses our artifact runs in their own environment.
runtime is the opposite: we do not compile against it, but it must be there to run. A JDBC driver is the standard example, since our code talks to java.sql interfaces and the driver is found at runtime. Keeping it out of the compile classpath stops anyone from accidentally importing driver-specific classes. It is transitive as runtime.
test is for testing libraries such as JUnit, AssertJ and Mockito. They are on the test classpath only, never packaged, and not transitive, so our test tools never leak into other projects.
3How Transitive Scopes Combine
When our project depends on library A, and A depends on B, the scope B gets in our project depends on both scopes: our scope for A and A's scope for B. The rule is that the result is never wider than our own scope for A, and that A's provided and test dependencies are never passed on.
For example, if we depend on A with compile scope and A depends on a JDBC driver with runtime scope, we get the driver as runtime. If we depend on A with test scope, everything A brings in compile or runtime scope becomes test scope for us. If A declares the servlet API as provided, we get nothing from it, and must declare it ourselves if we need it.
| Our scope for A | A has B as compile | A has B as runtime |
|---|---|---|
| compile | compile | runtime |
| provided | provided | provided |
| runtime | runtime | runtime |
| test | test | test |
4The Special Scopes: system and import
system scope points at a jar by file path with systemPath. It behaves like provided, but Maven never downloads it and the path is tied to one machine's file layout, so builds become non-reproducible. It is deprecated in practice; the right way to use a jar that is not in any repository is to install or deploy it to an internal repository and depend on it normally.
import is not a classpath scope at all. It is only allowed inside dependencyManagement, on a dependency whose type is pom. It means: take the dependencyManagement section of that POM and merge it into mine. This is how we use a BOM, a bill of materials, such as spring-boot-dependencies, to get a set of tested versions without inheriting a parent POM. Nothing is added to any classpath; only versions become managed.
5Follow-up questions
Why does a Spring Boot executable jar not need provided scope for Tomcat?
Because in an executable jar, the application is not deployed into a server; it starts the server itself from its main method. Embedded Tomcat is a normal library the application needs at runtime, so it comes in compile scope through spring-boot-starter-web and is packaged into the jar. provided only makes sense when we build a war for an external server that already has Tomcat; then we mark spring-boot-starter-tomcat as provided. Spring Boot's repackaging even keeps provided dependencies in a separate folder of an executable war, so the same war can run on its own or be deployed.
How do we see the scope each dependency ended up with?
mvn dependency:tree prints every dependency with its resolved scope, such as org.postgresql:postgresql:jar:42.7.4:runtime, and shows which direct dependency brought in each transitive one.
Is optional the same as provided?
No. optional is a flag, not a scope. An optional dependency is used normally by our module, but is not passed on to projects that depend on us, which is useful for libraries with optional integrations. provided is about who supplies the library at runtime.
6Conclusion
In this answer, we saw that scopes decide which classpaths a dependency joins and whether it is passed on. compile is everywhere and transitive; provided is for compiling and testing against something the runtime supplies and is not transitive; runtime is for running only and transitive as runtime; test is for tests only and not transitive. system points to a local file and should be avoided, and import is only for pom-type entries in dependencyManagement, where it merges a BOM's managed versions into our project.
Open with
Scopes decide which classpath a dependency is on and whether it flows to projects that depend on mine. compile is the default: it is on the compile, test and runtime classpaths, packaged, and transitive. provided is on the compile and test classpaths, but I expect the runtime environment to supply it, like the servlet API in a war deployed to Tomcat, so it is not packaged and not transitive. runtime is the opposite: I do not compile against it, but the application needs it to run, like a JDBC driver, and it is transitive as runtime. test is only for compiling and running tests, like JUnit or Mockito, and is never transitive, so test tools never leak to consumers.
Then, if they want more
When scopes combine transitively, the result is never wider than my scope for the direct dependency, so anything coming through a test dependency becomes test for me. system points to a jar by file path; it ties the build to one machine and I would install the jar into an internal repository instead. import is special: it is only valid for a pom-type dependency inside dependencyManagement, and it merges that BOM's managed versions into my project, which is how I use spring-boot-dependencies without the Boot parent. So the transitive ones are compile and runtime.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Maven guides
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain Maven dependency scopes compile, provided, runtime, test, system and import....": https://scrivio-interview-library.netlify.app/java/maven/0e97d6fa11/