Quick revision
The short answer
A transitive dependency is a library we never declared ourselves: one of our direct dependencies needs it, so Maven or Gradle pulls it in automatically. We exclude one when it conflicts with something, duplicates functionality we already have or is simply unwanted, for example excluding spring-boot-starter-tomcat to run on Jetty, or excluding an extra logging binding so that only one logging backend remains. An exclusion applies only on the path where we declare it, and in Maven an optional dependency is never passed on to consumers at all.
What to remember
In this answer, we saw that transitive dependencies are the libraries our direct dependencies need, pulled in automatically from their metadata, and that Spring Boot starters rely on them. We exclude one when it conflicts, duplicates something or is unwanted, such as the Tomcat starter when moving to Jetty or an extra logging binding. An exclusion works only on its own path, so the artifact can still arrive through another one, and optional dependencies are the library author's way of not passing a dependency on in the first place.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A transitive dependency is a library we never declared ourselves: one of our direct dependencies needs it, so Maven or Gradle pulls it in automatically. We exclude one when it conflicts with something, duplicates functionality we already have or is simply unwanted, for example excluding spring-boot-starter-tomcat to run on Jetty, or excluding an extra logging binding so that only one logging backend remains. An exclusion applies only on the path where we declare it, and in Maven an optional dependency is never passed on to consumers at all.
Why this question is asked
Open the dependency tree of a small Spring Boot service and we usually find that a handful of declared dependencies turn into a hundred jars. Most of them arrive transitively. That is convenient, but it also means most of the code in our application was chosen by someone else. Interviewers ask this question to see whether we understand where those jars come from and how to take control when the default choice is wrong. In this answer, we will see how transitive dependencies are resolved, look at two common Spring Boot exclusions, understand why an exclusion does not always remove a jar, and see how optional dependencies differ.
1A Quick Recap: Direct and Transitive Dependencies
A direct dependency is one we declare in our pom.xml or build.gradle. Every published library comes with its own metadata, a pom file in Maven repositories, which lists the libraries it needs. When we add a direct dependency, the build tool reads that metadata, adds those libraries too, reads their metadata, and so on down the tree. Everything it adds this way is a transitive dependency.
Spring Boot starters are built on this idea. A starter such as spring-boot-starter-web contains almost no code; it is a dependency with a carefully chosen list of transitive dependencies: Spring MVC, Jackson, a logging setup and an embedded Tomcat. We declare one line and get a working web stack.
Notice that we never wrote tomcat-embed-core anywhere. It is two levels down. Spring Boot 4 reorganised some starters (for example a spring-boot-starter-webmvc starter), so check your version for the exact names, but the idea is the same.
2Example 1: Excluding Tomcat to Use Jetty
Suppose we want Jetty instead of Tomcat as the embedded server. If we only add spring-boot-starter-jetty, both servers are on the classpath and Spring Boot's auto-configuration has to pick one; it is also wasted space. So we exclude the Tomcat starter from the web starter and add Jetty.
Notice that we exclude by group and artifact only, without a version. An exclusion removes that artifact, and everything beneath it on that path, from the tree.
3Example 2: Keeping One Logging Backend
Logging is the other classic case. Spring Boot logs through SLF4J, a logging facade (an API that libraries call, which then forwards to a real logging library), and by default the real library is Logback, brought in by spring-boot-starter-logging. If a team wants Log4j 2 instead, it excludes spring-boot-starter-logging and adds spring-boot-starter-log4j2.
A subtler version happens with older libraries. A third-party client may bring its own SLF4J binding, such as slf4j-reload4j, or the old commons-logging jar. Now two logging backends compete: SLF4J warns that it found multiple providers and picks one, and log configuration seems to be ignored because the other backend is the one in use. The fix is to exclude the unwanted binding from that client so only one backend remains. Spring already provides its own commons-logging bridge (spring-jcl), so an extra commons-logging jar is pure duplication.
4Why an Exclusion Does Not Always Remove the Jar
Here is the catch many people miss. An exclusion is attached to one dependency declaration, so it only cuts the artifact out of the tree below that declaration. If another dependency also needs the same artifact, it still arrives through that other path.
| Path | Exclusion declared? | commons-logging on classpath? |
|---|---|---|
| orders -> library-a -> commons-logging | Yes, on library-a | Not through this path |
| orders -> library-b -> commons-logging | No | Yes, through this path |
| Result for the whole service | Yes: one path is enough |
So after adding an exclusion, we always check the tree again. To remove an artifact everywhere, Gradle offers a global exclusion with configurations.all { exclude(group = "commons-logging", module = "commons-logging") }. Maven has no global exclusion, so we add the exclusion on each path or use the Enforcer plugin's bannedDependencies rule to fail the build if the artifact appears through any path.
5Optional Dependencies Are Not Passed On
Maven has a related feature on the library author's side. A library can mark a dependency as optional with optional set to true. The library compiles and tests against it, but consumers of the library do not receive it transitively. It is meant for features that only some users need: a library may support both Jackson and Gson, mark both optional, and let each application add the one it uses.
The difference is who decides. An exclusion is the consumer saying, I do not want this. An optional dependency is the library author saying, you only get this if you ask for it. Spring Boot's own auto-configuration module uses optional dependencies heavily, which is why adding a library to our build is what switches on the matching auto-configuration.
6Follow-up questions
What is the Gradle equivalent of a Maven optional dependency?
Gradle's java-library plugin separates api, implementation and compileOnly. A compileOnly dependency is visible when compiling the library but is not passed to consumers, which is close to Maven's optional. Gradle also has feature variants, which let a library publish optional capabilities that consumers request explicitly. When Gradle publishes a pom, it maps some of these to optional or excludes them; check your plugin version for the exact mapping.
How do you see why a transitive dependency is in the build?
In Maven, mvn dependency:tree with -Dincludes filters the tree to one artifact and shows every path that brings it in, and -Dverbose also shows versions that lost conflict resolution. In Gradle, ./gradlew dependencyInsight --dependency name --configuration runtimeClasspath shows which paths requested it and why a particular version was chosen.
Is excluding a dependency risky?
It can be. If the excluded library is actually used at runtime by the dependency that brought it, we get ClassNotFoundException or NoClassDefFoundError, sometimes only on a rarely used code path. So we exclude only when we replace the functionality (Jetty for Tomcat, Log4j 2 for Logback) or know it is unused, and we run tests that exercise the affected library.
7Conclusion
In this answer, we saw that transitive dependencies are the libraries our direct dependencies need, pulled in automatically from their metadata, and that Spring Boot starters rely on them. We exclude one when it conflicts, duplicates something or is unwanted, such as the Tomcat starter when moving to Jetty or an extra logging binding. An exclusion works only on its own path, so the artifact can still arrive through another one, and optional dependencies are the library author's way of not passing a dependency on in the first place.
Open with
A transitive dependency is one I did not declare myself: a library I depend on needs it, so Maven or Gradle reads that library's metadata and pulls it in, and so on down the tree.
Then, if they want more
Spring Boot starters are built on this; spring-boot-starter-web brings Spring MVC, Jackson, logging and an embedded Tomcat. I would exclude a transitive dependency when it conflicts, duplicates something I already have, or I just do not want it. The common example is excluding spring-boot-starter-tomcat from the web starter and adding spring-boot-starter-jetty to run on Jetty.
Another is logging: if a third-party client brings its own SLF4J binding or commons-logging, I exclude it so there is only one backend, or I exclude spring-boot-starter-logging to switch to Log4j 2.
One thing to remember is that an exclusion only applies to the path where I declare it, so if another dependency also needs that jar, it still arrives; I always re-check with dependency:tree, and in Gradle I can exclude globally.
Also, a dependency marked optional in Maven is never passed on to consumers at all, which is the library author's way of leaving that choice to us.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Maven guides, Gradle user manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a transitive dependency, and when would you exclude one? Give an example from a...": https://scrivio-interview-library.netlify.app/java/dependency-and-supply-chain/1acec1ee44/