Compile-scope and runtime-scope dependencies differ in when Maven makes them available during the build. Compile dependencies are needed to compile and package the project and are on the classpath at every stage, while runtime dependencies are only needed when the application actually executes.
Key Points: • The default scope in Maven, if none is specified, is compile — available for compiling, testing, and running the application. • Runtime scope dependencies are added to the runtime and test classpaths but are excluded when compiling your own source code. • A typical runtime-scope example is a JDBC driver: your code compiles against the JDBC API, but the concrete driver implementation is only needed when the application actually connects to a database. • Using the narrowest correct scope keeps the compile-time classpath smaller and avoids accidentally coding against an implementation class.
Example: A Spring application might declare spring-context as compile scope, since it's needed to compile against its classes, while declaring the MySQL driver as runtime scope, since the code only references java.sql interfaces directly.
Code Example:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
<scope>runtime</scope>
</dependency>Interview Tip: A concise interview answer is:
"Compile-scope dependencies are on the classpath when compiling, testing, and running the app, and that's the default scope. Runtime-scope dependencies are excluded from compilation but included at test and execution time — a JDBC driver is the classic example, since you code against the interface but only need the implementation at runtime."