TExcelTAD
Home › Java Builds: Maven & Gradle › Dependencies & Scopes

Dependencies & Scopes

A <dependency> names an artifact by its coordinates. Maven downloads it (and everything it depends on, transitively) into the local repository and puts the JARs on the right classpaths. "The right classpaths" is decided by the dependency's scope.

The scopes

Remember the two classpaths from the by-hand lesson: one for compiling, one for running. Maven has a third, for tests. Each scope says which of them a dependency joins:

Scope Compile classpath Test classpath Runtime (packaged) Transitive? Typical use
compile (default) yes yes yes yes libraries your code calls
provided yes yes no no supplied by the container or JDK at run time (Servlet API), compile-only annotations
runtime no yes yes yes implementations you never call directly (logging backend, JDBC driver)
test no yes no no JUnit, AssertJ, mocks
system like provided, from a local file path deprecated, avoid
import only in <dependencyManagement> for BOMs the BOM lesson

Seeing scopes in the tree

This project uses one dependency of each everyday scope. dependency:tree prints the whole graph with the scope at the end of each line:

bash · try it yourself
mkdir -p scopes/src/main/java/demo && cd scopes
cat > src/main/java/demo/Names.java <<'SRC'
package demo;

import org.apache.commons.lang3.StringUtils;
import org.jspecify.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Names {
    private static final Logger log = LoggerFactory.getLogger(Names.class);

    public static String display(@Nullable String name) {
        log.info("formatting {}", name);
        return StringUtils.defaultIfBlank(name, "anonymous");
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>scopes</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.20</version>
    </dependency>
    <dependency>
      <groupId>org.jspecify</groupId><artifactId>jspecify</artifactId><version>1.0.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.20</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>5.14.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
POM
mvn -q -o dependency:3.11.0:tree -DoutputFile=/dev/stdout
com.example:scopes:jar:1.0
+- org.apache.commons:commons-lang3:jar:3.21.0:compile
+- org.slf4j:slf4j-api:jar:2.0.20:compile
+- org.jspecify:jspecify:jar:1.0.0:provided
+- org.slf4j:slf4j-simple:jar:2.0.20:runtime
\- org.junit.jupiter:junit-jupiter:jar:5.14.4:test
   +- org.junit.jupiter:junit-jupiter-api:jar:5.14.4:test
   |  +- org.opentest4j:opentest4j:jar:1.3.0:test
   |  +- org.junit.platform:junit-platform-commons:jar:1.14.4:test
   |  \- org.apiguardian:apiguardian-api:jar:1.1.2:test
   +- org.junit.jupiter:junit-jupiter-params:jar:5.14.4:test
   \- org.junit.jupiter:junit-jupiter-engine:jar:5.14.4:test
      \- org.junit.platform:junit-platform-engine:jar:1.14.4:test

Three details to read in that tree:

  • junit-jupiter is a small aggregator: it brings -api, -params and -engine, which bring the platform JARs. Every transitive dependency of a test dependency is test too.
  • slf4j-simple (runtime) depends on slf4j-api, which we also declare directly, so it doesn't appear twice: each artifact is in the graph once.
  • dependency:3.11.0:tree names the plugin version on the command line. Maven 3.9's built-in default for the dependency plugin is the older 3.7.0; in a real project you'd pin it in pluginManagement instead. -DoutputFile=/dev/stdout with -q prints just the tree.

Three classpaths

dependency:build-classpath prints the classpath Maven builds for a scope. Compare them:

bash · try it yourself
mkdir -p cp && cd cp
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>cp</artifactId>
  <version>1.0</version>
  <dependencies>
    <dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.21.0</version></dependency>
    <dependency><groupId>org.jspecify</groupId><artifactId>jspecify</artifactId><version>1.0.0</version><scope>provided</scope></dependency>
    <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-simple</artifactId><version>2.0.20</version><scope>runtime</scope></dependency>
    <dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>3.27.7</version><scope>test</scope></dependency>
  </dependencies>
</project>
POM
for scope in compile runtime test; do
  echo "== $scope"
  mvn -q -o dependency:3.11.0:build-classpath -DincludeScope=$scope -Dmdep.outputFile=/dev/stdout \
    | tr ':' '\n' | xargs -n1 basename | sort
done
== compile
commons-lang3-3.21.0.jar
jspecify-1.0.0.jar
== runtime
commons-lang3-3.21.0.jar
slf4j-api-2.0.20.jar
slf4j-simple-2.0.20.jar
== test
assertj-core-3.27.7.jar
byte-buddy-1.18.3.jar
commons-lang3-3.21.0.jar
jspecify-1.0.0.jar
slf4j-api-2.0.20.jar
slf4j-simple-2.0.20.jar

The compile classpath has jspecify (provided) but not the logging backend; the runtime classpath, which is also what a fat JAR or copy-dependencies would ship, has the backend (plus slf4j-api, which it depends on) and drops jspecify. The test classpath has everything, including AssertJ and its own dependency, Byte Buddy.

Declared vs used: dependency:analyze

A dependency you call but haven't declared works by accident: it arrived transitively, and it disappears the day the library that brought it stops needing it. dependency:analyze inspects the compiled bytecode and reports both kinds of mistake:

bash · try it yourself
mkdir -p an/src/main/java/demo && cd an
cat > src/main/java/demo/Json.java <<'SRC'
package demo;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Json {
    public static ObjectMapper mapper() {
        return new ObjectMapper(new JsonFactory());
    }
}
SRC
cat > pom.xml <<'POM'
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>an</artifactId>
  <version>1.0</version>
  <properties>
    <maven.compiler.release>21</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
    <dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.22.4</version></dependency>
    <dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>33.7.2-jre</version></dependency>
  </dependencies>
</project>
POM
mvn -q compile
mvn -o dependency:3.11.0:analyze | grep -E '^\[WARNING\]'
[WARNING] Used undeclared dependencies found:
[WARNING]    com.fasterxml.jackson.core:jackson-core:jar:2.22.4:compile
[WARNING] Unused declared dependencies found:
[WARNING]    com.google.guava:guava:jar:33.7.2-jre:compile

jackson-core is used but undeclared (our code imports JsonFactory, which comes from it), and Guava is declared but unused. Add the first, remove the second. In CI, dependency:analyze-only with failOnWarning turns this into a rule.

Note: The analyzer reads bytecode, so it can't see dependencies used only through reflection, ServiceLoader or string class names (JDBC drivers, logging backends). That's what runtime scope is for; check the report before deleting anything.

Test yourself

Which scope fits a JDBC driver your code never imports but needs when it runs?

  1. compile
  2. provided
  3. runtime
  4. test

Your web app compiles against the Servlet API, and the application server supplies it at run time. Which scope?

  1. compile
  2. provided
  3. runtime
  4. system

dependency:analyze reports a 'used undeclared' dependency. What is the risk?

  1. None, it works
  2. Your code relies on a library that only arrives transitively and can disappear when another dependency changes
  3. The JAR will be packaged twice
  4. The build will be slower