How to exclude dependency in gradle projects?


This tutorials 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 exclude at runtime and compile time for each dependency

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

This excludes the dependency library from given ouput jar or 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?

Some times, We want to exclude global dependencies which 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.