{

How to exclude dependency in Gradle projects?


This tutorial talks about how to exclude dependency in Gradle. This post also includes about exclude jar file in groovy and kotlin script.

exclude dependency in gradle projects

Gradle uses two different version syntaxes for configuring build dependencies

  • groovy
  • kotlin

How to exclude transitive dependencies in Gradle?

This section talks about excluding at runtime and compile time for each dependency.

exclude runtime dependency: Sometimes, You have transitive dependencies which are causing conflict with the version of the direct dependency.

It excludes the dependency library from the given output jar or the war file.

Suppose org.springframework.security module has a dependency spring-security-web You want to exclude spring-security-web,

You have to configure groovy script as follows

implementation('org.springframework.security.spring-security-taglibs:4.3.0') {
        exclude group: 'org.springframework.security', module: 'spring-security-web'
    }

Kotlin

implementation('org.springframework.security.spring-security-taglibs:4.3.0') {
        exclude(group='org.springframework.security', module= 'spring-security-web')
    }

with exclude, It excludes the module by group and module and there is no support for version.

exclude compile dependency: In compile configuration, Add exclude configuration with module and module names to exclude during compile goal.

compile (‘org.springframework.security.spring-security-taglibs:4.3.0’){ exclude group: ‘org.springframework.security’, module: ‘spring-security-web’ }

compile('org.springframework.security.spring-security-taglibs:4.3.0') {
        exclude(group='org.springframework.security', module= 'spring-security-web')
    }

How to exclude dependencies globally in Gradle projects?

Sometimes, We want to exclude global dependencies that are applicable to compile and runtime dependencies.

using configurations.all attribute, You can add exclude option with dependency using group and module

configurations.all {
   exclude group:"org.slf4j", module: "slf4j-api"
}

Conclusion

In this tutorial, You learned how to exclude dependencies globally as well per dependency in compile and runtime dependency.

With Gradle, You can exclude with group and module names only, But not version configuration.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.