All 15 Build Tools: Maven & Gradle questions
More in Build Tools: Maven & Gradle
Advertisement
Back to results for “Security”
Java › Build Tools: Maven & Gradle

How do you resolve a dependency version conflict manually in Maven using exclusions, and when would this be necessary despite the automatic nearest-wins resolution?

Intermediate

Answer

<exclusions> inside a <dependency> declaration explicitly prevents a specific transitive dependency from being pulled in through that particular parent dependency, useful when the automatic nearest-wins resolution selects an incompatible or vulnerable version, or when you want to substitute your own explicitly-declared version of that transitive dependency instead -- necessary in scenarios like a security vulnerability being patched in a newer version of a transitive library that the automatic resolution wouldn't otherwise select.
<dependency>
    <groupId>com.example</groupId>
    <artifactId>some-library</artifactId>
    <version>1.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!-- Now explicitly declare the desired, patched version yourself -->
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.3</version>  <!-- patched for a known CVE -->
</dependency>
Real-world example A security audit flags a known CVE in a transitive jackson-databind version pulled in indirectly through a third-party library, resolved by explicitly excluding that transitive dependency and declaring the patched version directly, without needing to wait for the third-party library maintainer to update their own dependency.

Common follow-ups: How would you use `mvn dependency:tree` to identify exactly which parent dependency is pulling in the problematic transitive version?;What's the equivalent mechanism in Gradle for excluding a specific transitive dependency?