Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude a package from a transitive dependency of external jar included in build.gradle?

I have included the following dependency in my build.gradle:

testCompile group: 'com.xebialabs.restito', name: 'restito', version:'0.5.1'

But with this jar other dependencies like jersey-core-1.18.3.jar also get included.

Now I want this jersey jar but this also has a package javax.ws.rs.core which contains classes that conflict with my latest version of javax.ws.rs.core included explicitly in build.gradle.

Is there any way that I can exclude only a specific package from a transitive dependency and not the whole dependency. I am a newbie to gradle so please correct me if any incorrect term was used.

When I run the following command:

gradlew dependencies --configuration testCompile

it gives me following dependency tree. Only the relevant part is shown here

testCompile - Compile classpath for source set 'test'.

+--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3
|    +--- org.apache.cxf:cxf-core:3.0.3 (*)
|    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.apache.cxf:cxf-rt-transports-http:3.0.3 (*)
+--- org.apache.cxf:cxf-rt-rs-service-description:3.0.0-milestone1
|    \--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.0-milestone1 -> 3.0.3 (*)
+--- org.codehaus.jackson:jackson-jaxrs:1.9.13
|    +--- org.codehaus.jackson:jackson-core-asl:1.9.13
|    \--- org.codehaus.jackson:jackson-mapper-asl:1.9.13
|         \--- org.codehaus.jackson:jackson-core-asl:1.9.13
+--- org.codehaus.jackson:jackson-mapper-asl:1.9.12 -> 1.9.13 (*)
+--- com.fasterxml.jackson.core:jackson-databind:2.4.1.2
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0
|    \--- com.fasterxml.jackson.core:jackson-core:2.4.1.1
\--- com.xebialabs.restito:restito:0.5.1
     +--- org.slf4j:slf4j-api:1.7.5 -> 1.7.7
     +--- org.mockito:mockito-core:1.10.17
     |    +--- org.hamcrest:hamcrest-core:1.1 -> 1.3
     |    \--- org.objenesis:objenesis:2.1
     +--- org.apache.mina:mina-core:2.0.4
     |    \--- org.slf4j:slf4j-api:1.6.1 -> 1.7.7
     +--- org.glassfish.grizzly:grizzly-http-server:2.3.17
     |    \--- org.glassfish.grizzly:grizzly-http:2.3.17
     |         \--- org.glassfish.grizzly:grizzly-framework:2.3.17
     +--- junit:junit:4.12
     |    \--- org.hamcrest:hamcrest-core:1.3
     \--- com.sun.jersey:jersey-grizzly2:1.18.3
          +--- org.glassfish.grizzly:grizzly-http:2.2.16 -> 2.3.17 (*)
          +--- org.glassfish.grizzly:grizzly-http-server:2.2.16 -> 2.3.17 (*)
          \--- com.sun.jersey:jersey-server:1.18.3
               \--- com.sun.jersey:jersey-core:1.18.3

(*) - dependencies omitted (listed previously)

Now the class Response in javax.ws.rs:javax.ws.rs-api:2.0.1 is under the package javax.ws.rs.core. Similarly there is another Response class present in com.sun.jersey:jersey-core:1.18.3 in package javax.ws.rs.core. But the later one contains earlier version which does not has readEntity() method introduced in rs-api 2.0. The dependency for Response in my project gets resolved to the earlier version always.

like image 884
Niharika G. Avatar asked May 14 '15 12:05

Niharika G.


1 Answers

Citing gradle user guide section about Excluding transitive dependencies:

You can exclude a transitive dependency either by configuration or by dependency:

Example 50.14

configurations {
    compile.exclude module: 'commons'
    all*.exclude group: 'org.gradle.test.excludes', module: 'reports'
}

dependencies {
    compile("org.gradle.test.excludes:api:1.0") {
    exclude module: 'shared'
    }
}

So back to your case, you can simply just take one of the methods described above and replace the modules in the example with with the module you'd like to exclude, e.g.

configurations {
    testCompile.exclude group: 'javax.ws.rs', module: 'jsr311-api'
}

Update (per the additional details provided)

What you have to do is to add to your script the following lines

configurations {
  testCompile.exclude group: 'com.sun.jersey', module: 'jersey-core'
}

Consider the following minimal build.gradle script:

apply plugin: 'java'

repositories {
    mavenCentral()
}

configurations {
  // Excluding com.sun.jersey:jersey-core:1.18.3, option #1: 
  // compile.exclude group: 'com.sun.jersey', module: 'jersey-core'
}

dependencies {
    compile "org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3"
    compile ("com.sun.jersey:jersey-grizzly2:1.18.3") {
      // Excluding com.sun.jersey:jersey-core:1.18.3, option #2: 
      // exclude group: 'com.sun.jersey', module: 'jersey-core'
    }
}

Now, with the file above as is running gradlew dependencies --configuration testCompile you'll get the next dependency tree:

+--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3
|    +--- org.apache.cxf:cxf-core:3.0.3
|    |    +--- org.codehaus.woodstox:woodstox-core-asl:4.4.1
|    |    |    \--- org.codehaus.woodstox:stax2-api:3.1.4
|    |    \--- org.apache.ws.xmlschema:xmlschema-core:2.1.0
|    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.apache.cxf:cxf-rt-transports-http:3.0.3
|         \--- org.apache.cxf:cxf-core:3.0.3 (*)
\--- com.sun.jersey:jersey-grizzly2:1.18.3
     +--- org.glassfish.grizzly:grizzly-http:2.2.16
     |    \--- org.glassfish.grizzly:grizzly-framework:2.2.16
     +--- org.glassfish.grizzly:grizzly-http-server:2.2.16
     |    +--- org.glassfish.grizzly:grizzly-http:2.2.16 (*)
     |    \--- org.glassfish.grizzly:grizzly-rcm:2.2.16
     |         \--- org.glassfish.grizzly:grizzly-framework:2.2.16
     \--- com.sun.jersey:jersey-server:1.18.3
          \--- com.sun.jersey:jersey-core:1.18.3

Note that com.sun.jersey:jersey-core:1.18.3 is listed as a transitive dependency. Now, uncommenting either of the exclusion options and rerunning the same command you'll get the following output which does contain this module:

+--- org.apache.cxf:cxf-rt-frontend-jaxrs:3.0.3
|    +--- org.apache.cxf:cxf-core:3.0.3
|    |    +--- org.codehaus.woodstox:woodstox-core-asl:4.4.1
|    |    |    \--- org.codehaus.woodstox:stax2-api:3.1.4
|    |    \--- org.apache.ws.xmlschema:xmlschema-core:2.1.0
|    +--- javax.ws.rs:javax.ws.rs-api:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.apache.cxf:cxf-rt-transports-http:3.0.3
|         \--- org.apache.cxf:cxf-core:3.0.3 (*)
\--- com.sun.jersey:jersey-grizzly2:1.18.3
     +--- org.glassfish.grizzly:grizzly-http:2.2.16
     |    \--- org.glassfish.grizzly:grizzly-framework:2.2.16
     +--- org.glassfish.grizzly:grizzly-http-server:2.2.16
     |    +--- org.glassfish.grizzly:grizzly-http:2.2.16 (*)
     |    \--- org.glassfish.grizzly:grizzly-rcm:2.2.16
     |         \--- org.glassfish.grizzly:grizzly-framework:2.2.16
     \--- com.sun.jersey:jersey-server:1.18.3
like image 188
Amnon Shochot Avatar answered Nov 07 '22 01:11

Amnon Shochot