这些Maven依赖范围之间有什么区别:提供/编译/系统/导入 [英] What's the difference between these Maven dependency scopes: provided/compile/system/import

查看:91
本文介绍了这些Maven依赖范围之间有什么区别:提供/编译/系统/导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档并有所了解.

I have read the documentation and have some understanding.

请更正或告知我真相;根据我的理解:

Please correct or inform me of the truth; as per my understanding:

  • provided
    依赖关系必须在运行代码的计算机上,并且必须包含在路径中

  • provided
    The dependencies must will be on the machine you run the code on, and must be included in the path

compile
依赖项将不在运行代码的计算机上,因此请将其包含在构建版本中

compile
The dependencies will not be on the machine that runs the code, so include them in the build

system
与提供的完全相同,但是您需要严格将依赖项存在于jar文件中

system
Exactly the same as provided, but you need the dependencies to be present in a jar file strictly

import
似乎它应该从其他POM文件导入依赖项,但我不知道如何/为什么,因此请多加阐述

import
Seems like it should import the dependencies from some other POM file but I don't know how/why, so a little elaboration would be appreciated

推荐答案

您对provided错误/模棱两可.意思是,这个jar应该在本地编译,但是它将在运行时通过其他方式在类路径上提供,因此请不要在我的类路径中包含它."例如,所有Web容器(例如tomcat)都包含servlet的jar.您应该对servlet类使用provided,以便可以在本地编译代码,但是您不希望覆盖tomcat部署到它时为您提供的servlet类.

You are wrong/ambiguous about provided. It means, "This jar should be compiled against locally, but it will be provided on the classpath by something else during runtime, so don't include it in the classpath for me." For example, all web containers (eg: tomcat) include the jars for servlets. You should use provided for the servlet classes so you can compile your code locally, but you don't want to override the servlet classes that tomcat provides for you when you deploy to it.

system的意思是,这些依赖关系在 my 系统上,我想直接指向它们".如果可能的话,您要避免这种情况,因为另一台计算机上的另一个人不一定具有这些依赖关系.

system means, "These dependencies are on my system and I want to point to them directly". You want to avoid this if you can, because another person on another computer won't necessarily have these dependencies.

provided之间的区别更容易显示:

The difference between provided is easier to show:

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

看看它有什么<systemPath>吗?那是区别.您无需使用provided指定路径,只要知道如何从存储库中获取依赖项即可. system仅从您的文件系统获取它.

See how it has that <systemPath>? That's the difference. You don't specify the path with provided, provided knows how to get the dependency from a repository. system gets it from your file system only.

我什至从未听说过import. @JigarJoshi链接到 http://maven.apache.org/guides /introduction/introduction-to-dependency-mechanism.html 其中表示

I've never even heard of import. @JigarJoshi linked to http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html which says

导入(仅在Maven 2.0.9或更高版本中可用)

此作用域仅用于<dependencyManagement>部分中pom类型的依赖项.它指示应使用该POM部分中的依赖项替换指定的POM.由于已将它们替换,因此具有导入范围的依赖项实际上并不参与限制依赖项的可传递性.

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 section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

认为的意思是,获取该项目具有的所有依赖项,并将其内联到此<dependencyManagement>部分中."如果我错了,请有人纠正我.

I think this is saying, "take all the dependencies this project has and inline them in this <dependencyManagement> section." Someone correct me if I'm wrong.

这篇关于这些Maven依赖范围之间有什么区别:提供/编译/系统/导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆