Maven Dependency Scopes
Fast recall
Quick revision
Core answer
compile is the default and is on compile, test, and runtime classpaths; runtime is on test/runtime but not main compile; test is only for test compilation/execution; provided is on compile/test but expected from the runtime and is not transitive. import applies only to a POM dependency inside dependencyManagement; system is a discouraged local-file scope.Understanding of Maven classpath management and how scopes prevent unnecessary dependencies at runtime
Using compile scope for everything including JUnit, which bloats the final JAR and breaks separation of concerns
Explain a real scenario: servlet-api as provided because Tomcat supplies it, or MySQL driver as runtime because your code only references JDBC interfaces
Interview-ready guide
Interview answer
Scope decides which classpaths contain a dependency and whether downstream projects inherit it. Compile is the default and covers main compilation, tests, and runtime. Runtime is not needed to compile main code but is needed when tests or the application run. Test stays on test classpaths. Provided is available for compile and tests but is supplied by the runtime environment and is not exposed transitively.
I also separate scope from archive contents. A normal Maven JAR contains this project's classes and resources, not every compile dependency. An assembly, shade, or Spring Boot repackage plugin controls bundled or nested dependencies.
Maven Dependency Scopes
Direct Answer
A dependency scope controls which Maven classpaths include a dependency and whether it is exposed transitively. compile is the default and is on compile, test, and runtime classpaths; runtime is on test/runtime but not main compile; test is only for test compilation/execution; provided is on compile/test but expected from the runtime and is not transitive. import applies only to a POM dependency inside dependencyManagement; system is a discouraged local-file scope.
Code Example
<dependencies>
<!-- compile: default, available on main compile, test, and runtime classpaths -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- test: only during test compilation and execution -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<!-- runtime: not needed at compile time, only when running -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope> <!-- code uses java.sql interfaces only -->
</dependency>
<!-- provided: container supplies it, don't package it -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope> <!-- Tomcat already has this -->
</dependency>
</dependencies>
Common Mistakes & Pitfalls
- Using compile scope for test libraries: Freshers often leave JUnit or Mockito with the default compile scope, which packages these multi-megabyte testing frameworks into the production JAR. This bloats deployments and can cause ClassLoader conflicts if the runtime environment has different versions. Always mark test frameworks with test scope.
- Marking database drivers as compile: Because you import java.sql.DriverManager in your code, freshers think the MySQL driver needs compile scope. But DriverManager loads drivers via reflection at runtime, so your code never directly references com.mysql classes. Using runtime scope keeps compile-time dependencies minimal.
- Forgetting provided scope for servlet-api: When building a WAR for Tomcat, freshers include servlet-api with compile scope. Tomcat already provides this, so you end up with two versions on the classpath, causing cryptic ClassCastExceptions at runtime. Use provided scope so Maven includes it during compilation but excludes it from the WAR.
Check your understanding
Answer these without looking back at the article.
- What happens if you put JUnit in compile scope instead of test?
- What is the 'system' scope?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.