Resolve Library Conflicts With Maven

Viewed 80

Starting with kotlin 1.4.30, the kotlin-compiler-embeddable dependency has included the files for the 'fastutil' library inside their main jar package. The problem it this conflicts with my local specified version of fastutil in my pom.xml file.

Is there any way I can include the 1.4.30 kotlin-compiler-embeddable dependency and ignore the /it/unimi/dsi/fastutil folder that way it will only use my version I specified?

Screenshot of kotlin-compiler-embeddable layout: enter image description here

Project is here: https://gofile.io/d/9m5LiV

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap // Default imports from the kotlin-compiler-embeddable lib

fun main(args: Array<String>) {
    // http://fastutil.di.unimi.it/docs/it/unimi/dsi/fastutil/ints/Int2ObjectOpenHashMap.html#%3Cinit%3E(int)
    // ^ this is possible when you dont have a kotlin-compiler-embeddable conflict
    // Because its importing this class from the kotlin-compiler-embeddable library
    // instead of the fastutil library itself
    val map = Int2ObjectOpenHashMap<String>(10)
    println("Hello World!")
}
1 Answers

Maven 2.0.9 introduced deterministic ordering of dependencies on the classpath back in 2008.

If you have 2 dependencies containing the same class, first one mentioned in the pom wins.

You can use mvn dependency:build-classpath to inspect the class path.

In your case, place fastutil before kotlin compiler.

<dependency>
    <groupId>it.unimi.dsi</groupId>
    <artifactId>fastutil</artifactId>
    <version>8.2.1</version>
</dependency>
<dependency>
    <groupId>org.jetbrains.kotlin</groupId>
    <artifactId>kotlin-compiler-embeddable</artifactId>
    <version>1.4.30</version>
</dependency>

Notes:

  1. The wording of your question is puzzling. kotlin-compiler-embeddable is a regular dependency in your project, not a plugin.
  2. I checked that kotlin-compiler also has fastlib pulled in, so using it instead of kotlin-compiler-embeddable won't help
  3. The missing constructor problem comes from the fact that kotlin is not pulling in fastutil itself, but a repackaged version intellij-deps-fastutil-8.3.1-1. See commit: Add fastutil dependency for 202 and higher platforms
  4. If you intend to distribute your app as an uber-jar, make sure to check your packager oprions. Maven shade plugin has powerful filtering tools.
  5. Although relying on classpath order is fast to implement and works, it can get surprising. Consider repackaging your kotlin compiler dependency without fastlib.
Related