Gradle Exclude Transitive Dependency Example
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' }
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
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' }
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' }
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' } }