Search code examples
scalagradlezinc

Can't set zinc version to 2.12


I have a Gradle Scala project where I'm trying to set up zinc 2.12. However, when I attempt to run the project using the command ./gradlew run, I encounter a NoClassDefFoundError related to scala/jdk/javaapi/CollectionConverters or The version of 'scala-library' was changed while using the default Zinc version. Version 2.12.15 is not compatible with org.scala-sbt:zinc_2.13:1.6.1 error if I comment zinc out from dependencies.

Here's how my Gradle file looks like:

plugins {
    id 'scala'
    id 'application'
}

repositories {
    mavenCentral()
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
      if (details.requested.group == 'org.scala-lang') {
        details.useVersion '2.12.15'
      }
    }
}

dependencies {
    // zinc "org.scala-sbt:zinc_2.12:1.6.1"
    implementation 'org.scala-lang:scala-library:2.12.15'
}

application {
    mainClass = 'test.gradle.plugin.App'
}

I created a project to replicate this error, which can be found here.

I'm unsure how to resolve this issue. Any help would be greatly appreciated.


Solution

  • Please refer to Zinc compatibility table Since Gradle 7.5 is used, it requires Scala 2.13.x to run Zinc. To be able to compile Scala 2.12.x sources, you need to configure Zinc configuration resolution strategy so it may use Scala 2.13.x like this

    plugins {
        id 'scala'
        id 'application'
    }
    
    repositories {
        mavenCentral()
    }
    
    configurations.zinc {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->
          if (details.requested.group == 'org.scala-lang') {
            details.useVersion '2.13.11'
          }
        }
    }
    
    dependencies {
        implementation 'org.scala-lang:scala-library:2.12.15'
    }
    
    application {
        mainClass = 'test.gradle.plugin.App'
    }