The meaning and application of maven scope

Dependency scope controls which dependencies are available on which classpath and which dependencies are included in an application.

Detailed scope description:

compile (compile scope)

compile is the default scope; if no scope is provided, the dependency's scope is the compile scope. Compile-scoped dependencies are available on all classpaths, and they are also packaged.

provided (scope provided)

A provided dependency is only used when the JDK or a container has provided the dependency. For example, if you develop a web application, you may need the Servlet API available on the compile classpath to compile a servlet, but you would not want to include the Servlet API in the packaged WAR; the Servlet API JAR is provided by your Provided by the application server or servlet container. Provided scoped dependencies are available on the compile classpath (not runtime). They are not transitive and are not packaged.

runtime (runtime scope)

Runtime dependencies are needed when running and testing the system, but not at compile time. For example, you might only need the JDBC API JAR at compile time, and only need the JDBC driver implementation at runtime.

test (test scope)

Test-scoped dependencies are not needed at normal compile and run time, they are only available during the test compile and test run phases.

system (system-wide)

System-wide dependencies are similar to provided, but you must explicitly provide a path to the JAR file on the local system. This is done to allow compilation based on native objects that are part of the system class library. Such an artifact should always be available and Maven will not look for it in the repository. If you set a dependency scope to be system scope, you must also provide a systemPath element. Note that this scope is deprecated (you should always try to reference dependencies from public or custom Maven repositories).

 

Simple example:

 

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.4</version>
    <scope>provided</scope>
   </dependency>
   <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
   </dependency>

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326914519&siteId=291194637