What do the Maven dependency scopes compile, provided, runtime and test mean?
Options
- They are the names of branches used in a Git repository
- Build and run, build only, run only, and tests only (Correct option)
- They define the author and license of each library
- They are the version numbers of a Maven release
Answer
compile: needed to build and run; provided: needed to build but supplied by the runtime; runtime: not needed to compile but needed to run; test: only for tests
Why
The scope controls when a dependency is on the classpath:
compile(default): always, and it is packaged.provided: available at compile time but not packaged, because the server provides it (e.g. the Servlet API in an external Tomcat).runtime: not needed to compile, needed at execution (e.g. a JDBC driver).test: only when compiling and running tests (JUnit, Mockito).
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>Remember
test = tests only; provided = server gives it; runtime = needed only to run.