什么是<范围>在<依赖>下在 pom.xml 中? [英] What is <scope> under <dependency> in pom.xml for?

查看:38
本文介绍了什么是<范围>在<依赖>下在 pom.xml 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看文档 http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html,我们可以看到下的标签

那是什么,我们如何使用它来运行测试?

解决方案

元素可以取 6 个值:compile, provided运行时测试系统导入.

此范围用于限制依赖项的传递性,并影响用于各种构建任务的类路径.

<块引用>

编译

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

提供

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

运行时

这个作用域表示该依赖不是编译所必需的,而是执行所必需的.它在运行时和测试类路径中,但不在编译类路径中.

测试

此范围表示该依赖项不是应用程序正常使用所必需的,仅适用于测试编译和执行阶段.

系统

此范围类似于提供的范围,但您必须提供明确包含它的 JAR.该工件始终可用,不会在存储库中查找.

import(仅适用于 Maven 2.0.9 或更高版本)

此范围仅用于该部分中 pom 类型的依赖项.它表示指定的 POM 应该替换为该 POM 部分中的依赖项.由于它们被替换,具有导入范围的依赖项实际上并不参与限制依赖项的传递性.

回答问题的第二部分:

<块引用>

我们如何使用它来运行测试?

请注意,test 范围只允许在测试阶段使用依赖项.

阅读文档了解完整详情.

Looking at documentation http://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html, we can see <scope> tag under <dependency>

What is that and how can we use it for running test?

解决方案

The <scope> element can take 6 values: compile, provided, runtime, test, system and import.

This scope is used to limit the transitivity of a dependency, and also to affect the classpath used for various build tasks.

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.

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 (only available in Maven 2.0.9 or later)

This scope is only used on a dependency of type pom in the 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.

To answer the second part of your question:

How can we use it for running test?

Note that the test scope allows to use dependencies only for the test phase.

Read the documentation for full details.

这篇关于什么是&lt;范围&gt;在&lt;依赖&gt;下在 pom.xml 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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