我們有一個簡單的 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
uj5u.com熱心網友回復:
由于您沒有為專案定義Scala版本,因此當您%%包含ScalaTest時,它使用默認的sbt 版本,2.12因此會導致二進制不兼容。
我相信最好的解決方案是使用并洗掉其顯式依賴項來定義專案的正確Scala版本。scalaVersion := "2.13.8
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/412146.html
標籤:
