Loading published curriculum…

06 · Maven from first build to release / LESSON 21 OF 102

Dependency scopes and transitive dependencies

A dependency scope decides which classpaths receive a library.

Before this lesson: Lifecycle phases versus plugin goals

Understand the topic

compile is the default and participates in main compilation and runtime. test is limited to tests. runtime is available when running but not for main-source compilation. provided expects the runtime environment to supply the dependency; it is not a general way to shrink an executable JAR.

Transitive dependencies arrive through direct dependencies. Inspect their origin before adding exclusions. An exclusion removes a transitive path, not every possible path to that library. Keep test frameworks out of production runtime unless there is a concrete need.

EXAMPLE · XML

pom.xml dependency fragment

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>runtime</scope>
</dependency>

What to expect

Inside a Boot-managed POM the driver version is managed; JDBC APIs compile from the JDK and the driver is present at runtime.

YOUR TURN

Put it into practice

  1. 01Inspect production and test dependency trees.
  2. 02Place a test-only library in test scope and verify it is unavailable in main sources.
  3. 03Verify: Runtime packaging contains the PostgreSQL driver and excludes test-only libraries.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

A dependency scope decides which classpaths receive a library.

  1. 01compile is the default and participates in main compilation and runtime. test is limited to tests. runtime is available when running but not for main-source compilation. provided expects the runtime environment to supply the dependency; it is not a general way to shrink an executable JAR.
  2. 02Transitive dependencies arrive through direct dependencies. Inspect their origin before adding exclusions. An exclusion removes a transitive path, not every possible path to that library. Keep test frameworks out of production runtime unless there is a concrete need.
  3. 03Trace the example: Inside a Boot-managed POM the driver version is managed; JDBC APIs compile from the JDK and the driver is present at runtime.

A mistake worth avoiding

The app compiles but cannot load the JDBC driver when launched.

DEBUGGING ROOM

Something broke. Where do you look?

The app compiles but cannot load the JDBC driver when launched.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Where should a library used only by tests normally be scoped?

Choose one answer

BUILD IT YOURSELF

Inspect production and test dependency trees.

  1. 1.Inspect production and test dependency trees.
  2. 2.Place a test-only library in test scope and verify it is unavailable in main sources.
  3. 3.Reproduce this failure, then repair it: The app compiles but cannot load the JDBC driver when launched.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Where should a library used only by tests normally be scoped?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

Runtime packaging contains the PostgreSQL driver and excludes test-only libraries.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references