Maven Scopes: Compile, Provided, Runtime, Test, Import, System

Maven dependency scope is used to specify the visibility of a dependency. It provides six scopes i.e. compile, provided, runtime, test, system, and import.

maven

The Maven dependency scope‘ attribute is used to specify the visibility of a dependency, relative to the different lifecycle phases (build, test, runtime, etc). Each scope indicates how a dependency is handled, when it is included in the classpath, and which dependencies are inherited by child projects.

Maven provides six scopes:

ScopeDescriptionClasspath InclusionWhen to Use
compileDefault scope. Available in all classpaths (compilation, testing, runtime).Included in all classpathsFor dependencies required for both compilation and runtime.
providedSimilar to compile, but expected to be provided by the JDK or container at runtime (e.g., servlet API).Compile, testFor dependencies provided by the runtime environment (e.g., web containers).
runtimeRequired during execution but not for compilation (e.g., JDBC drivers).Test, runtimeFor dependencies needed at runtime but not at compile time.
testOnly available in the test classpath, not required for runtime or compile.TestFor dependencies needed solely for testing (e.g., JUnit, Mockito).
systemSimilar to provided, but you must explicitly provide the JAR file with a local path.Compile, testFor dependencies not available in any repository and must be manually provided.
importOnly available in dependencyManagement section to import dependency from another BOM (Bill of Materials).N/AFor managing versions of dependencies across multiple projects.

1. Maven ‘Compile‘ Scope

This is Maven’s default scope. Dependencies with compile scope are needed to build, test, and run the project. Dependencies with compile scope are included during compile, test, and runtime phases.

Scope compile is to be required in most cases to resolve the import statements into your Java class’s source code.

<dependencies>

  <dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>${log4j-version}</version>
    <!-- You can ommit this because it is default -->
    <scope>compile</scope>
  </dependency>

</dependencies>

2. Maven ‘Provided‘ Scope

The Maven dependency scope ‘provided‘ is used during the build and test of the project. These dependencies are also required to run, but should not exported, because the dependency will be provided by the runtime, for instance, by a servlet container or application server.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

3. Maven ‘Runtime‘ Scope

Dependencies with maven dependency scope ‘runtime‘ are not needed to build, but are part of the classpath to test and run the project.

<dependency>
    <groupId>com.thoughtworks.xstream</groupId>
    <artifactId>xstream</artifactId>
    <version>1.4.4</version>
    <scope>runtime</scope>
 </dependency>

4. Maven ‘Test‘ Scope

Dependencies with maven dependency scope test are not needed to build and run the project. They are needed to compile and run the unit tests.

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

5. Maven ‘System‘ Scope

Dependencies with system scope are similar to ones with scope provided. The only difference is system dependencies are not retrieved from the remote repository. They are present under the project’s subdirectory and are referred from there. See external dependency for more details.

<dependency>
  <groupId>extDependency</groupId>
  <artifactId>extDependency</artifactId>
  <scope>system</scope>
  <version>1.0</version>
  <systemPath>${basedir}\war\WEB-INF\lib\extDependency.jar</systemPath>
</dependency>

6. Maven ‘Import‘ Scope

The import scope is only supported on a dependency of type pom in the ‘dependencyManagement‘ section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM’s dependencyManagement section.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>other.pom.group.id</groupId>
            <artifactId>other-pom-artifact-id</artifactId>
            <version>SNAPSHOT</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>   
    </dependencies>
</dependencyManagement>

7. Transitivity Resolution

When you include a maven dependency and it has its other dependencies (i.e. transitive dependencies) then you may want to be clear about the scope of these transitive dependencies as well.

Let’s understand maven transitive dependencies with a simple table. In this table, if a dependency is set to the scope in the left column, transitive dependencies at the top row will result in a dependency with the scope listed at their intersection.

Dependencycompileprovidedruntimetest
compilecompileruntime
providedprovidedprovided
runtimeruntimeruntime
testtesttest

8. Summary

Understanding and using the correct Maven dependency scope ensures that the correct project dependencies are included in the correct phases of the build lifecycle. This helps in managing the classpath efficiently and avoiding issues related to missing or redundant dependencies.

Drop me your questions in the comments section.

Happy Learning!!

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.