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

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


当前回答

.pom依赖作用域可以包含:

compile -在编译时和运行时可用 提供-在编译时可用。(此依赖项应该由外部容器提供,如OS…) runtime -在运行时可用 测试-测试编译和运行时 system -类似于提供的,但将<systemPath>path/some.jar</systemPath>暴露到.jar上 import -在Maven v2.0.9中可用于<type>pom</type>,它应该由这个文件<dependencyManagement/>中的有效依赖项替换

其他回答

<scope>元素可以有6个值:compile、provided、runtime、test、system和import。

此作用域用于限制依赖项的传递性,也用于影响用于各种构建任务的类路径。

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.

回答你问题的第二部分:

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

注意,测试范围只允许在测试阶段使用依赖关系。

详细信息请阅读文档。

六个依赖范围:

compile: default scope, classpath is available for both src/main and src/test test: classpath is available for src/test provided: like complie but provided by JDK or a container at runtime runtime: not required for compilation only require at runtime system: provided locally provide classpath import: can only import other POMs into the <dependencyManagement/>, only available in Maven 2.0.9 or later. It is not always practical to change parent, many projects already specify a parent project to manage their organization standards. dependencyManagement allows us to add parent project without making parent, it's like multiple inheritance.

Scope标记总是用于在类路径级别限制jar的传递依赖关系和可用性。如果我们不提供任何作用域,那么默认作用域将工作,即Compile。

Compile means that you need the JAR for compiling and running the app. For a web application, as an example, the JAR will be placed in the WEB-INF/lib directory. Provided means that you need the JAR for compiling, but at run time there is already a JAR provided by the environment so you don't need it packaged with your app. For a web app, this means that the JAR file will not be placed into the WEB-INF/lib directory. For a web app, if the app server already provides the JAR (or its functionality), then use "provided" otherwise use "compile".

.pom依赖作用域可以包含:

compile -在编译时和运行时可用 提供-在编译时可用。(此依赖项应该由外部容器提供,如OS…) runtime -在运行时可用 测试-测试编译和运行时 system -类似于提供的,但将<systemPath>path/some.jar</systemPath>暴露到.jar上 import -在Maven v2.0.9中可用于<type>pom</type>,它应该由这个文件<dependencyManagement/>中的有效依赖项替换