Home  >  Gradle

Gradle Exclude Transitive Dependency Example

By Arvind Rai, September 20, 2014
Gradle script downloads the JAR and the dependency JAR when gradle script run. This dependency is called transitive dependency. Gradle script downloads the JAR from maven central or any other location which we specify. Sometimes we come in the situation to exclude transitive dependency. The scenario can be like
1. JAR version of transitive dependency is not available
2. Dependency is not available in specified location
3. Dependency is not required in runtime or both in compile and runtime.

To exclude transitive dependency, we have two approach as below.
1. Exclude transitive dependency by configuration
2. Exclude transitive dependency by dependency

To understand exclusion of transitive dependency, find a simple gradle script.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
   compile 'org.hibernate:hibernate-core:4.3.6.Final'
} 
When we run the command gradle eclipse, we get the below Jar in eclipse classpath.
hibernate-core-4.3.6.Final.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
javassist-3.18.1-GA.jar
antlr-2.7.7.jar
jandex-1.1.0.Final.jar
xml-apis-1.0.b2.jar
Now we will see how to exclude the transitive dependency for any JAR.

Exclude Transitive Dependency by Configuration

Excluding Transitive Dependency by Configuration is preferable way. Using configuration we can exclude transitive dependency for module and group. Find the separate example of module and group. First find the example which will use module dependency as below.
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
   compile 'org.hibernate:hibernate-core:4.3.6.Final'
}
configurations {
    compile.exclude module: 'dom4j'
} 
Run the command gradle eclipse, you will see that dom4j and its dependency JAR will not be available in classpath. Now find the example using group and module both.
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
   compile 'org.hibernate:hibernate-core:4.3.6.Final'
}
configurations {
    compile.exclude module: 'dom4j'
    compile.exclude group: 'org.jboss.logging'
} 
We can also use the syntax as below to exclude the transitive dependency.
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
   compile 'org.hibernate:hibernate-core:4.3.6.Final'
}
configurations {
    all*.exclude group: 'org.jboss.logging'
} 

Exclude Transitive Dependency by Dependency

To exclude the transitive dependency using Dependency will be done as below which filter JAR.
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
    mavenCentral()
}
dependencies {
   compile ('org.hibernate:hibernate-core:4.3.6.Final') {
      exclude module: 'dom4j'
      exclude group: 'org.jboss.logging'
   }
} 
In the example, I am using module and group both together, we can use separately too. The gradle script will filter module 'dom4j' and group 'org.jboss.logging' and its transitive dependency.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us