243

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?

Alpha
  • 13,320
  • 27
  • 96
  • 163
  • 21
    it is all very well explained in the docs: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope – guido Nov 17 '14 at 15:24
  • possible duplicate of [Understanding Compile- vs Run-time Dependencies](http://stackoverflow.com/questions/7070570/understanding-compile-vs-run-time-dependencies) – arghtype Nov 17 '14 at 15:41

4 Answers4

342

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.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
  • 1
    So if you use "test" does it end up in the .jar created? – markthegrea Jul 10 '19 at 21:03
  • 2
    what if a dependency is needed at compile & runtime ? – Albin Jul 13 '19 at 19:05
  • 1
    @markthegrea probably i am late to answer this, but No. it does not end up in .jar created. – sherelock Feb 16 '21 at 17:11
  • @sherelock was is the actual benefit of this though? Why would you want to limit to only Test scope? what if you didn't care and it didn't matter if the dependency was there at compile/runtime and packaged with .jar? is there an actual negative to that?? – ennth Jun 08 '21 at 05:02
  • 2
    @ennth you will bloat your jar with unnecessary dependencies. You could think of cases where you want to have dependencies only for testing scenarios (mockito for example). No need to include that in your compiled jar. – Goopher Jun 18 '21 at 08:46
25

Six Dependency scopes:

  • 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.
Premraj
  • 72,055
  • 26
  • 237
  • 180
5

Scope tag is always use to limit the transitive dependencies and availability of the jar at class path level.If we don't provide any scope then the default scope will work i.e. 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".

Prakash K
  • 11,669
  • 6
  • 51
  • 109
Mukesh
  • 540
  • 1
  • 9
  • 13
  • 11
    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". – Mukesh Apr 06 '16 at 05:29
  • 1
    please add yhis comment inside your answer – Supun Wijerathne Jan 19 '18 at 04:53
2

.pom dependency scope can contain:

  • compile - available at Compile-time and Run-time
  • provided - available at Compile-time. (this dependency should be provided by outer container like OS...)
  • runtime - available at Run-time
  • test - test compilation and run time
  • system - is similar to provided but exposes <systemPath>path/some.jar</systemPath> to point on .jar
  • import - is available from Maven v2.0.9 for <type>pom</type> and it should be replaced by effective dependency from this file <dependencyManagement/>
yoAlex5
  • 29,217
  • 8
  • 193
  • 205