What is the <scope> in the <dependency> section in pom.xml for?

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

What is it and how can we use it to run a test?

+166
maven
Nov 17 '14 at 15:17
source share
4 answers

The <scope> element can take 6 values: compilation, provision, runtime, testing, system and import.

This area is used to limit the transitivity of dependencies, as well as to change the class path used for various build tasks.

compilation

This is the default area that is used if none are specified. Compilation dependencies are available in all project classes. In addition, these dependencies apply to dependent projects.

provided,

This is similar to compilation, but indicates that you expect the JDK or container to provide a dependency at runtime. For example, when creating a web application for Java Enterprise Edition, you must make a dependency on the Servlet API and its associated Java EE APIs for scope because the web container provides these classes. This scope is accessible only on the way to compilation and testing and is not transitive.

at runtime

This area indicates that the dependency is not required to compile, but is intended to be executed. It is in the runtime and tests class paths, but does not compile class paths.

test

In this area, it is indicated that the dependency is not required for normal use of the application and is available only for the compilation and test phases.

system

This area is similar to the one provided, except that you must provide a JAR that contains this explicitly. The artifact is always available and cannot be viewed in the repository.

import (only available in Maven 2.0.9 or later)

This area is used only for pom type dependencies in this section. It indicates that the specified POM should be replaced by the dependencies in this POM section. Since they are replaced, dependencies with the import region do not actually participate in limiting the transitivity of the dependency.

To answer the second part of your question:

How can we use it to run a test?

Note that the test area only allows dependencies for the test phase.

Read the documentation for more details.

+243
Mar 24 '15 at 11:02
source share

Six scopes in one line:

  • compile : default
  • provided : by JDK or container at runtime
  • runtime : not required for compilation
  • test : used only during tests
  • system : provided locally
  • import : only available in Maven 2.0.9 or later
+10
Nov 05 '17 at 12:54 on
source share

The scope tag is always used to limit transitive dependencies and the availability of a jar at the class path level. If we do not provide any area, then the default area will be used, i.e. Compile .

+3
Apr 6 '16 at 5:26
source share

If we do not provide any area, then the default area will be compiled. If you want to confirm, just go to the "Effective pom tab" tab in the eclipse editor, it will show you how to compile.

0
Feb 01 '17 at 13:58 on
source share



All Articles