当前位置: 首页 > news >正文

Maven Dependency Scope用法

原帖地址:http://uule.iteye.com/blog/2087485

 

 官方API描述

 

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 只能作用在编译和测试时,同时没有传递性。
  • 使用这个时,不会将包打入本项目中,只是依赖过来。   
  • 使用默认或其他时,会将依赖的项目打成jar包,放入本项目的Lib里
  • 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代码   收藏代码
    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
    表示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项目中的包配置

转载于:https://www.cnblogs.com/cl1234/p/5360394.html

相关文章:

  • 结对编写四则运算
  • Appium 一个测试套件多次启动android应用
  • zookeeper 配置
  • JAVA基础知识总结
  • 敌兵布阵_区间求和问题_线段树 or 树状数组
  • CI 笔记(1)
  • hdu 1874 畅通工程续
  • 补--第四周
  • UI-定时器与动画使用总结
  • J2EE 课件2
  • Oracle SQL monitor
  • HTML 利用MAP创建图片中的链接的映射
  • CSS选择器(二)
  • 2016.04.14,英语,《Vocabulary Builder》Unit 14
  • 实验三 白盒测试
  • 【编码】-360实习笔试编程题(二)-2016.03.29
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Fabric架构演变之路
  • fetch 从初识到应用
  • java取消线程实例
  • Meteor的表单提交:Form
  • Objective-C 中关联引用的概念
  • React-redux的原理以及使用
  • 道格拉斯-普克 抽稀算法 附javascript实现
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 跨域
  • 人脸识别最新开发经验demo
  • ​queue --- 一个同步的队列类​
  • (1)(1.9) MSP (version 4.2)
  • (8)STL算法之替换
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • .bat文件调用java类的main方法
  • .Net core 6.0 升8.0
  • .net 按比例显示图片的缩略图
  • .net 调用php,php 调用.net com组件 --
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .NET/C# 利用 Walterlv.WeakEvents 高性能地中转一个自定义的弱事件(可让任意 CLR 事件成为弱事件)
  • .set 数据导入matlab,设置变量导入选项 - MATLAB setvaropts - MathWorks 中国
  • @GlobalLock注解作用与原理解析
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!
  • [ vulhub漏洞复现篇 ] JBOSS AS 4.x以下反序列化远程代码执行漏洞CVE-2017-7504
  • [8-23]知识梳理:文件系统、Bash基础特性、目录管理、文件管理、文本查看编辑处理...
  • [android] 看博客学习hashCode()和equals()
  • [BZOJ4566][HAOI2016]找相同字符(SAM)
  • [CareerCup] 17.8 Contiguous Sequence with Largest Sum 连续子序列之和最大
  • [CC2642r1] ble5 stacks 蓝牙协议栈 介绍和理解
  • [dts]Device Tree机制
  • [EFI]DELL XPS13 9360电脑 Hackintosh 黑苹果efi引导文件
  • [EFI]MSI GF63 Thin 9SCXR电脑 Hackintosh 黑苹果efi引导文件
  • [IE9] 解决了傲游、搜狗浏览器在IE9下网页截图的问题
  • [IM] [Webhook] Webhook实现IM平台机器人
  • [ios-必看] IOS调试技巧:当程序崩溃的时候怎么办 iphone IOS
  • [LeetCode] 2.两数相加
  • [leetcode] 3Sum