Maven Dependency配置说明

Dependency Scope

依赖的范围分为以下几种环境:编译环境(compile classpath)、测试环境(test classpath)、运行环境(runtime classpath)。根据不同的修饰控制依赖的影响范围

  • compile: 默认Scope, 依赖再项目的所有环境引用
  • provided: 运行时不提供依赖,运行时依赖由JDK或web容器提供,依赖不传递,如J2EE
  • runtime: 编译环境不提供依赖,运行时和测试环境需要依赖,如数据库驱动依赖
  • test: 测试环境依赖,依赖不传递
  • system: 与provided类似,但是使用的是本地jar包
  • import: 只有在type为pom的时候使用,只能在dependencyManagement中使用,表示引用Pom文件中的那些依赖

官方文档说明:

Dependency Scope
Dependency scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.
There are 6 scopes available:
compile
This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
provided
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, 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.
runtime
This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
test
This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.
system
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
import
This scope is only supported on a dependency of type pom in the dependencyManagement section. It indicates the dependency to be replaced with the effective list of dependencies in the specified 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.

Dependency Type
没有在官网上找到说明,在其他博客中看到这样写的

Adding the type of apk to the dependency allows the Maven Android plugin to find the Android package of the application.
不难看出,type=apk告诉maven使用maven android plugin来进行处理type=为apk的依赖。从而推想type=ejb就是告诉maven使用maven ejb plugin来处理。

type应该就是“maven处理依赖的方式”?不确定,望指正
type默认值是jar

记录一个关于type=pom的坑:
使用下面代码配置,版本信息会unknow,需要删除type与scope节点才能找到版本。

 <dependencyManagement>
        <dependencies>
			<dependency>
	               <groupId>com.baomidou</groupId>
	               <artifactId>mybatis-plus</artifactId>
	               <version>${mybatis-plus.version}</version>
	               <type>pom</type>
	               <scope>import</scope>
	           </dependency>
	        <dependencies>
<dependencyManagement>
<dependency>
     <groupId>com.baomidou</groupId>
     <artifactId>mybatis-plus</artifactId>
</dependency>

但是在配置spring-boot-dependencies时type需要设置为pom

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
       <dependencies>
<dependencyManagement>

Dependency optional

如果B依赖C,A依赖B,默认情况下A中会引入依赖C,如果想再A中不引入依赖C,可以在A的pom文件依赖B处加上"exclusions"将C去除掉。或者在B的pom文件依赖C处加上optional=true。
比如B可能又关于数据库mysql、oracle的依赖,可以将关于数据库的依赖全部设置为optional=true,当A使用B的时候用哪个数据库依赖再自己手动引入哪个。如果B不引入数据库的依赖代码会报错,所以这种情况就可以使用optional。
再比如 spring-boot-actuator pom.xml 文件中,有超过 20 个依赖是 optional。因为 Spring Boot 不可能将没必要的依赖也打包到你最终的 jar package 中,所以用到 spring boot actuator 的项目最终生成的 jar package 中不会包含这 20 多个依赖 jar,如果你要用到哪一个,显式的加入到你的项目就好了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值