Sobes.tech
Middle+

Do you understand the lifecycle of Maven and the scope of dependencies? What is scope? Why is servlet-api set as provided?

sobes.tech AI

Answer from AI

In Maven, the lifecycle is a sequence of phases that are executed to build the project, for example: validate, compile, test, package, install, deploy.

Scope of dependencies determines at which stages of the build and execution they are available:

  • compile — by default, available at all stages (compilation, testing, execution).
  • provided — available during compilation and testing, but not included in the final artifact, assuming the runtime environment (e.g., application server) provides this dependency.
  • runtime — needed only during execution, not during compilation.
  • test — needed only for testing.
  • system — similar to provided, but requires manually specifying the path to the library.

Why is servlet-api set as provided?

Because the servlet container (e.g., Tomcat, Jetty) already contains this library. Including it in the artifact could cause version conflicts or duplication. Therefore, servlet-api is marked as provided to use it during compilation but not include it in the final WAR/JAR.

Example in pom.xml:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>