【问题标题】:pure java project with unit tests written using scalatest having compiler compatibility issue带有使用 scalatest 编写的单元测试的纯 java 项目,具有编译器兼容性问题
【发布时间】:2022-01-15 18:05:26
【问题描述】:

我们有一个简单的 java 项目,其中包含用 scalatest 编写的单元测试:

  • 编译范围内没有 scala 库 (autoScalaLibrary := false)
  • scala-compiler 添加到测试范围以使用 scalatest 编译在 scala 中编写的单元测试
  • scala-library, scalatest 添加到单元测试的测试范围

从 intellij 和命令行 sbt 控制台执行单元测试时出现以下错误:-

scalac: error while loading package, class file '/Users/loc/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.8/scala-library-2.13.8.jar(scala/reflect/package.class)' is broken
(class java.lang.RuntimeException/error reading Scala signature of package.class: Scala signature package has wrong version
 expected: 5.0
 found: 5.2 in package.class)

下面是简化的 build.sbt 文件:-

name := "pure-java-project-with-scala-tests"

//this is added for packaging
enablePlugins(PackPlugin)

//this will not add scala library to compile classpath, as our project is a pure java project
//this will also not add scala library when "sbt pack" is triggered
autoScalaLibrary := false
//this will disable cross-building, as our project is a pure java project
crossPaths := false

//this will add scalatest to test classpath, as our project unit tests will be written using scalatest
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.10" % Test
//this will add scala library classes to test classpath, as our project unit tests will be written using scalatest
libraryDependencies += "org.scala-lang" % "scala-library" % "2.13.8" % Test

//our assumption is scala-compiler, it is required to compile scala unit tests
//but it is giving below error, when I try to execute unit test from intellij as well as sbt
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.13.8" % Test

可以在https://github.com/moglideveloper/pure-java-project-with-scala-tests找到简化的项目

sbt gitter 链接:- https://gitter.im/sbt/sbt?at=61e30f06f5a3947800088f3d

【问题讨论】:

    标签: java scala scope scalatest scala-compiler


    【解决方案1】:

    由于您没有为项目定义 Scala 版本,当您执行 %% 以包含 ScalaTest 时,它使用默认的 sbt 即为2.12,从而导致二进制不兼容。

    我相信最好的解决方案是使用 scalaVersion := "2.13.8 定义项目的正确 Scala 版本并删除其显式依赖。

    【讨论】:

    • 真...删除我的评论:)
    猜你喜欢
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-21
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多