Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve dependency conflicts with Gradle?

Tags:

java

gradle

I am developing a project with Dropwizard and Titan DB. Both depend on Google Guava. One depends on version 15 and the other on 18. This error occurs at runtime:

! java.lang.IllegalAccessError: tried to access method com.google.common.base.Stopwatch.<init>()V from class com.thinkaurelius.titan.graphdb.database.idassigner.StandardIDPool$ID
BlockRunnable

I researched the error and found that it was being caused by titan's Guava 15.0 dependency being evicted by Guava 18.0.

I am new to Java and Gradle. I am using Gradle's java and application plugins to build and run the main class with gradle run. How can I resolve this problem?


Here is my build.gradle:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = "com.example.rest.App"

repositories {
    mavenCentral()
}

dependencies {
    compile (
        [group: 'io.dropwizard', name: 'dropwizard-core', version: '0.8.0-rc1'],
        [group: 'com.thinkaurelius.titan', name: 'titan-core', version: '0.5.1'],
        [group: 'com.thinkaurelius.titan', name: 'titan-berkeleyje', version: '0.5.1'],
        [group: 'com.tinkerpop', name: 'frames', version: '2.6.0']
    )
    testCompile group: 'junit', name: 'junit', version: '3.8.1'
}

run {  
    if ( project.hasProperty("appArgs") ) {  
        args Eval.me(appArgs)  
    }  
}
like image 279
Dmitry Minkovsky Avatar asked Nov 18 '14 21:11

Dmitry Minkovsky


People also ask

How do I force Gradle to use specific dependency?

If the project requires a specific version of a dependency on a configuration-level then it can be achieved by calling the method ResolutionStrategy. force(java. lang. Object[]).

How do I manage Gradle dependencies?

At runtime, Gradle will locate the declared dependencies if needed for operating a specific task. The dependencies might need to be downloaded from a remote repository, retrieved from a local directory or requires another project to be built in a multi-project setting. This process is called dependency resolution.


1 Answers

By default, Gradle will select the highest version for a dependency when there's a conflict. You can force a particular version to be used with a custom resolutionStrategy (adapted from http://www.gradle.org/docs/current/dsl/org.gradle.api.artifacts.ResolutionStrategy.html):

configurations.all {
  resolutionStrategy {
    force 'com.google.guava:guava:15.0'
  }
}

This doesn't add a dependency on guava 15.0, but says if there is a dependency (even transitively) to force the use of 15.0.

You can get more information on where your dependencies come from with gradle dependencies and gradle dependencyInsight ....

FYI, it looks like you have a few different versions of Guava requested (11.0.2, 14.0.1, 15.0 and 18.0).

HTH

like image 149
bigguy Avatar answered Sep 28 '22 02:09

bigguy