Maven Dependency Scope

 Official API Description

 

Dependency scope is used to limit the scope of Dependency, which affects the status of packages imported by maven projects in each life cycle.

Since 2.0.9, 1 new scope has been added, and now there are 6 scopes :


  • The default scope of compile means that all dependencies can be used in the life cycle. Also, these dependencies are passed to the dependent projects.
  • provided
    is similar to compile, but indicates that the dependency is provided by the JDK or container, such as Servlet AP and some Java EE APIs. This scope  can only be used at compile and test time, and is not transitive.
  • When using this, the package will not be imported into this project, but only depended on.   
  • When using the default or other, the dependent projects will be packaged into jar packages and placed in the Lib of this project
  • when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • Xml code   Favorite code
    1. <!-- Servlet -->  
    2.         <dependency>  
    3.             <groupId>javax.servlet</groupId>  
    4.             <artifactId>servlet-api</artifactId>  
    5.             <version>2.5</version>  
    6.             <scope>provided</scope>  
    7.         </dependency>  
    8.         <dependency>  
    9.             <groupId>javax.servlet.jsp</groupId>  
    10.             <artifactId>jsp-api</artifactId>  
    11.             <version>2.1</version>  
    12.             <scope>provided</scope>  
    13.         </dependency>  
     
  • runtime
    indicates that dependency does not work at compile time, but at runtime and test time
  • test
    indicates that the dependency works at test time, not at runtime.
  • system
    is similar to provided, but it should be provided in the form of an external JAR package in the system , maven will not look for it in the repository. E.g:

<project>
...
<dependencies>
  <dependency>
   <groupId>javax.sql</groupId>
   <artifactId>jdbc-stdext</artifactId>
   <version>2.0</version>
   <scope>system</scope>
   <systemPath>${java.home}/lib/rt.jar</systemPath>
  </dependency>
</dependencies>
...
</project>

 

 

  • import (Maven 2.0.9 之后新增)
    它只使用在<dependencyManagement>中,表示从其它的pom中导入dependency的配置,例如:    This scope is only used on a dependency of type pom in the <dependencyManagement> section. It indicates that the specified POM should be replaced with the dependencies in that POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

<project>

<modelVersion>4.0.0</modelVersion>

<groupId>maven</groupId>

<artifactId>B</artifactId>

<packaging>pom</packaging>

<name>B</name>

<version>1.0</version>

 

<dependencyManagement>

    <dependencies>

      <dependency>

        <groupId>maven</groupId>

        <artifactId>A</artifactId>

        <version>1.0</version>

        <type>pom</type>

        <scope>import</scope>

      </dependency>

      <dependency>

        <groupId>test</groupId>

        <artifactId>d</artifactId>

        <version>1.0</version>

      </dependency>

    </dependencies>

</dependencyManagement>

</project>

Project B imports the package configuration in project A


Original: http://uule.iteye.com/blog/2087485

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444971&siteId=291194637