Use because() on an exclude

Gradle 4.6 introduces because() to specify why a dependency is added to a configuration.

This is nice, but why a dependency is added is, most of the time, obvious. What’s often not obvious is why a transitive dependency is excluded. Is there a way to specify a reason on an exclude? Something like

    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude(module: 'junit') {
            because 'we want to use JUnit 5 and not JUnit 4'
        }
    }
2 Likes

Curious about your thoughts @jendrik and company.

That’s unfortunately not possible at the moment. We currently only show modules that have been selected in the dependency result/report/build scan. During dependency resolution, only these things have a node in the dependency graph to which the reasons are attached.

Adding a reason for why something is not in the result is a larger feature addition (how do we transport this information from the declaration to the result? How/where do we present it in the result?). Still I agree that it is useful and we might add something along these lines in the future.

Just today I came across a situation where I wished I could express exactly that. I think this feature would be a great addition.

1 Like

Are there any plans for this feature? I very often have to add excludes and having because() clause would be better than using comments

You could fake it using

testImplementation("org.springframework.boot:spring-boot-starter-test") {
    exclude(module: "junit").because("we want to use JUnit 5 and not JUnit 4")
    because(null)
}

exclude returns the ExternalModuleDependency so you can call because on it,
but it would be set on the main dependency effectively, not on the exclude.
So you set the reason to null again afterward to not have the exclude reason as selection reason in the reports.

To get proper support, you probably need to open a feature request on the GitHub Gradle repository if there is none yet.
At least from quickly searching I didn’t find one.
Just a thread in the community forum here will usually not work to get something in. :slight_smile:

I’ve opened a feature request here

1 Like