注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

和申的个人主页

专注于java开发,1985wanggang

 
 
 

日志

 
 

Dependency Scopes(maven)  

2012-04-06 10:01:21|  分类: Maven |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Dependency scope 是用来限制Dependency的作用范围的, 影响maven项目在各个生命周期时导入的package的状态。

自从2.0.9后,新增了1种,现在有了6种scope:

  • compile
    默认的scope,表示 dependency 都可以在生命周期中使用。而且,这些dependencies 会传递到依赖的项目中。
  • provided
    跟compile相似,但是表明了dependency 由JDK或者容器提供,例如Servlet AP和一些Java EE APIs。这个scope 只能作用在编译和测试时,同时没有传递性。
  • runtime
    表示dependency不作用在编译时,但会作用在运行和测试时
  • test
    表示dependency作用在测试时,不作用在运行时。
  • system
    跟provided 相似,但是在系统中要以外部JAR包的形式提供,maven不会在repository查找它。 例如:
<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>

 

B项目导入A项目中的包配置 

====================================================================================================

====================================================================================================




Dependency Scopes

This page is started because I (the first author) don't understand the dependency scope mechanism. In the first version of this page, I am trying to make sense of different sources about the topic. This means the information here is not correct per definition! The intention of this page is to get it correct and clear, so please, if you do know something about dependency scopes, change the nonsense below (wink).

Introduction to the Dependency Mechanism

Dependencies are defined in the POM, and can be resolved transitively. However, you don't need all dependencies in all situations. That is why dependecies can have a scope defined. Furthermore, there is a tag <optional>true</optional> you can use for a dependency.

In the following table, we explain the behavior of each scope for different goals. We talk about the current project, the dependency and a user project. The current project is the project whose POM we are editing. The dependency is a project the user project directly depends on. A user project is a project for which the current project is a direct depency. Consequentially, a user project has an indirect depency on the dependency. We also presume a maven2-based mechanism (goal) to run final projects.

The following lifecycle phases are important:

  • compile: Compile the main source.
  • test: Compile the test source and run the tests. This requires the main source to be compiled (the test goal depends on the compile goal).
  • run: (non-existing) goal that runs a final artifact. Obviously, this requires the main sources to be compiled (the run goal depends on the compile goal).
  • assembly: Create an assembly of all kinds off stuff around the artifact. Amongst others, this might contain a lib-directory that contains external libraries the current project depends on.

Furthermore, it is important to see that some dependencies that are required for compilation (and testing), are optional for using the artifact on a user project or for running it if it is a final project itself. Examples are the dependency of commons-logging on log4j or of hibernate on ehcache and c3po and others. These projects or coded in such a way that they detect whether or not a library is available in the classpath, and use it if it is, but it is not necessary.

Also, indirect dependencies are not necessarily needed for compiling a user project: the current project might depend on the dependency internally, but have no mentioning of the dependency in its API that is used by the user project. Thus, dependency is not needed for compiling the user project, but it might be for running the final project.

The following dependency scopes are supported:

  • compile: This dependency is needed for compilation of the main source
  • test: This dependency is needed for compiling and running tests. It is not needed for compiling the main source or running the final artifact.
  • runtime: This dependency is needed for running the final artifact. It is not needed for compiling the main source or compiling or running the tests.
  • provided: This dependency is needed for compiling and/or running the artifact but is not necessary to include in the package, because it is provided by the runtime environment - for example, jsp-api.jar is provided by your web application container, so you don't include it in your WEB-INF/lib (for the example of a webapp); or a plugin or optional package that is a prerequisite for your application, but is not bundled with your application.
  • system: This dependency is required in some phase of your project's lifecycle, but is system-specific. Use of this scope is discouraged: This is considered an "advanced" kind of feature and should only be used when you truly understand all the ramifications of its use, which can be extremely hard if not actually impossible to quantify. This scope by definition renders your build non-portable. It may be necessarry in certain edge cases. The system scope includes the <systemPath> element which points to the physical location of this dependency on the local machine. It is thus used to refer to some artifact expected to be present on the given local machine an not in a repository; and whose path may vary machine-to-machine. The systemPath element can refer to environment variables in its path: ${JAVA_HOME} for instance.
  • tag <optional />

Legend:

  • U: Download and use dependency in the classpath. / Dowload and include dependency in the assembly.
  • U!O: Download and use dependency in the classpath, unless the dependency is <optional />. / Dowload and include dependency in the assembly, unless the dependency is <optional />.
  • !: dependency is not used

For the current project:

scope/phase -->

compile

test

run

assembly

compile

U

U

U

U

test

!

U

!

!

runtime

!

U

U

U

provided

U

!

!

!

For a user project that has the current project as a dependency with scope compile:

scope/phase -->

compile

test

run

assembly

compile

U!O

U!O

U!O

U!O

test

!

U

!

!

runtime

!

U!O

U!O

U!O

provided

U!O

!

!

!

For a user project that has the current project as a dependency with scope test:

??

For a user project that has the current project as a dependency with scope runtime:

??

For a user project that has the current project as a dependency with scope provided:

摘自:http://docs.codehaus.org/display/MAVENUSER/Dependency+Scopes


  评论这张
 
阅读(2095)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016