这些Maven依赖范围有什么区别:provided/compile/system/import [英] What's the difference between these Maven dependency scopes: provided/compile/system/import

查看:151
本文介绍了这些Maven依赖范围有什么区别:provided/compile/system/import的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.您应该将 provided 用于 servlet 类,以便您可以在本地编译您的代码,但您不想在部署时覆盖 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 的意思是,这些依赖关系在我的 系统上,我想直接指向它们".如果可以,您希望避免这种情况,因为另一台计算机上的其他人不一定有这些依赖项.

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>

看看它是如何拥有 的?这就是区别.您不用 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 或更高版本)

此范围仅用于 部分中 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.

认为这是在说,获取该项目具有的所有依赖项并将它们内联到此 部分."如果我错了,有人纠正我.

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依赖范围有什么区别:provided/compile/system/import的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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