maven scope compile 和提供给 JAR 打包的区别 [英] Difference between maven scope compile and provided for JAR packaging

查看:72
本文介绍了maven scope compile 和提供给 JAR 打包的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将工件构建为 JAR 时,maven 范围 compileprovided 之间有什么区别?如果是战争,我会理解 - 该工件将包含或不包含在 WEB-INF/lib 中.但在 JAR 的情况下,这无关紧要 - 不包括依赖项.当它们的作用域是 compileprovided 时,它们必须在类路径上.我知道 provided 依赖项不是可传递的 - 但它只有一个区别吗?

What is the difference between the maven scope compile and provided when artifact is built as a JAR? If it was WAR, I'd understand - the artifact would be included or not in WEB-INF/lib. But in case of a JAR it doesn't matter - dependencies aren't included. They have to be on classpath when their scope is compile or provided. I know that provided dependencies aren't transitive - but is it only one difference?

推荐答案

来自 Maven 文档:

  • 编译

这是默认范围,如果没有指定则使用.编译依赖项在项目的所有类路径中都可用.此外,这些依赖项会传播到依赖项.

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.

提供

这很像编译,但表明您期望 JDK 或容器在运行时提供依赖.例如,当为 Java Enterprise Edition 构建 Web 应用程序,您将将 Servlet API 和相关 Java EE API 的依赖设置为提供范围是因为 Web 容器提供了这些类.这个范围仅在编译和测试类路径上可用,并且是不可传递.

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.

回顾:

  • 依赖项不是可传递的(如您所述)
  • 提供的范围仅在编译和测试类路径中可用,而编译范围在所有类路径中可用.
  • 提供的依赖项没有打包

这篇关于maven scope compile 和提供给 JAR 打包的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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