google / guava

Google core libraries for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`guava:32.1.2-jre` make `checkstyle` fail

LiYing2010 opened this issue · comments

Description

We are using checkstyle plugin in a gradle (v8.3) project
checkstyle is the latest version 10.12.4, and our project also use com.google.guava:guava:31.1-jre
and everything works fine

but when I upgrade guava to 32.1.2-jre, it make checkstyle failed with following error:

Execution failed for task ':myproject:checkstyleMain'.
> Could not resolve all files for configuration ':myproject:checkstyle'.
   > Could not resolve com.google.guava:guava:32.0.1-jre.
     Required by:
         project :myproject > com.puppycrawl.tools:checkstyle:10.12.4
      > Module 'com.google.guava:guava' has been rejected:
           Cannot select module with conflict on capability 'com.google.collections:google-collections:32.1.2-jre' also provided by [com.google.collections:google-collections:1.0(runtime)]
   > Could not resolve com.google.collections:google-collections:1.0.
     Required by:
         project :myproject > com.puppycrawl.tools:checkstyle:10.12.4 > org.apache.maven.doxia:doxia-core:1.12.0 > org.codehaus.plexus:plexus-container-default:2.1.0
      > Module 'com.google.collections:google-collections' has been rejected:
           Cannot select module with conflict on capability 'com.google.collections:google-collections:1.0' also provided by [com.google.guava:guava:32.1.2-jre(jreRuntimeElements)]

some investigation:

I checked the dependency list of checkstyle : https://checkstyle.sourceforge.io/dependencies.html
looks like it depends on com.google.guava:guava:32.0.1-jre
and there is a transitive dependency com.google.collections:google-collections:1.0 too

which means, guava:32.0.1-jre + google-collections:1.0 + checkstyle 10.12.4 should work

so I changed my dependency to guava:32.0.1-jre, and it worked!

I tested some versions of guava:
32.1.0-jre ---> failed with same error
32.1.1-jre ---> failed with same error
32.1.2-jre ---> failed with same error
32.1.3-jre ---> failed with same error

32.0.0-jre ---> works fine
32.0.1-jre ---> works fine
31.1-jre ---> works fine

looks like this error only occurs in 32.1.x version

and I check the maven repo: https://repo1.maven.org/maven2/com/google/guava/guava/
the only strange thing I found is:
all the 32.1.x version has a file guava-32.1.x-jre.module, with following content:

      "capabilities": [
        {
          "group": "com.google.collections",
          "name": "google-collections",
          "version": "32.1.x-jre"
        },
      ]
    },

I am not sure, but I guess this is why gradle cannot resolve com.google.collections:google-collections:1.0 ?

Example

......

    apply plugin: 'checkstyle'

    ......

    checkstyle {
        toolVersion = '10.12.4'
        configFile = file("${rootProject.rootDir}/config/checkstyle/checkstyle.xml")
        ignoreFailures true
        sourceSets = [project.sourceSets.main] // ignore test code checkstyle
    }

    dependencyManagement {
        dependencies {
            ......

            dependency 'com.google.guava:guava:31.1-jre'

            ......
        }
    }


### Expected Behavior

`checkstyle` should run without error

### Actual Behavior

`checkstyle` fails

### Packages

_No response_

### Platforms

_No response_

### Checklist

- [X] I agree to follow the [code of conduct](https://github.com/google/.github/blob/master/CODE_OF_CONDUCT.md).

I reproduced this issue with a very simple build script:

build.gradle.kts:

plugins {
    java
}

repositories {
    mavenCentral()
    maven { setUrl("https://repo.maven.apache.org/maven2") }
}

dependencies {
    implementation("com.google.guava:guava:31.1-jre")
    implementation("com.google.collections:google-collections:1.0")
}

and tested for some versions:
32.0.0-jre ---> works fine
32.0.1-jre ---> works fine
31.1-jre ---> works fine

32.1.0-jre ---> failed with same error
32.1.1-jre ---> failed with same error
32.1.2-jre ---> failed with same error
32.1.3-jre ---> failed with same error

error message is:

Could not resolve: com.google.guava:guava:32.1.0-jre
Could not resolve: com.google.collections:google-collections:1.0
Could not resolve: com.google.guava:guava:32.1.0-jre
Could not resolve: com.google.collections:google-collections:1.0

oh

I found this in your release note:

Reporting dependencies that overlap with Guava
If your dependency graph contains the very old google-collections or the hacky listenablefuture, Gradle will now report that those libraries contain duplicates of Guava classes. When this happens, you'll need to tell Gradle to select Guava:

configurations.all {
resolutionStrategy.capabilitiesResolution.withCapability("com.google.collections:google-collections") {
select("com.google.guava:guava:0")
}
// and/or
resolutionStrategy.capabilitiesResolution.withCapability("com.google.guava:listenablefuture") {
select("com.google.guava:guava:0")
}
}

I added the following to my test build script:

configurations.all {
    resolutionStrategy.capabilitiesResolution.withCapability("com.google.collections:google-collections") {
        select("com.google.guava:guava:0")
    }
}

and it fixed the dependencies conflict in that test build script

but I am confused, is this a workaround? or the official solution?

also, this does NOT work for our real project,
because google-collections is not a dependency of our project, but a transitive dependency of checkstyle plugin

I don't know how to tell gradle to select guava over google-collections in plugin dependency

oh~~~
good news

this workaround works for our real project: 😅

    dependencies {
        modules {
            // replace old dependency `google-collections` with `guava`
            module("com.google.collections:google-collections") {
                replacedBy("com.google.guava:guava", "google-collections is part of guava")
            }
        }
    }

looks like it also applied to the transitive dependency of checkstyle plugin

now our project works just fine ~~~