Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

What are dependency scopes in Maven?

IntermediateConceptJava builds (Maven, Gradle, JAR, WAR)
Answer

Short answer

A scope tells Maven when a dependency is needed and whether it goes into the final artifact. The main ones are compile (default — needed everywhere and packaged), test (only for running tests), provided (needed to compile, but the server provides it at runtime) and runtime (not needed to compile, but needed to run).

Table

Scope Compile? Tests? Packaged in JAR/WAR? Example
compile ✅ ✅ ✅ spring-boot-starter-web
test ❌ ✅ ❌ junit-jupiter, mockito
provided ✅ ✅ ❌ servlet-api (Tomcat already has it)
runtime ❌ ✅ ✅ JDBC driver (postgresql)

Example

<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>jakarta.servlet</groupId>
  <artifactId>jakarta.servlet-api</artifactId>
  <scope>provided</scope>
</dependency>

Why DevOps cares

Wrong scopes cause real bugs: marking a JDBC driver as test means the app crashes in production with ClassNotFoundException. Marking servlet-api as compile in a WAR can clash with Tomcat's own copy.

What to say in the interview

"Scopes control the classpath at each stage and what gets packaged. Test libraries use test scope so they stay out of the artifact; container-supplied APIs like servlet use provided; drivers are often runtime."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR

More on Java builds (Maven, Gradle, JAR, WAR)