Scala从Java8升级到Java11踩过的坑

首先要考虑到JDK11和scala的兼容性,根据实际情况选择需要的scala版本

Scala从Java8升级到Java11踩过的坑_第1张图片
JDK&Scala Compatibility

问题 01

Detect muti scala-library version

原因

同一个依赖有多个版本,maven不知道选择哪个

解决方案

  1. 同一依赖maven会根据路径最短原则选择依赖,依赖路径相同会根据声明优先选择
    可以根据这个特性,使想要用的版本被选择
  2. Dependency_Management是一个更好的选择,如果在dependency_Management中指定只能用某个版本,maven只会选择这个版本
    DependencyManagement官方介绍



org.scala-lang
scala-library
${scala-library.version}



问题02

error: object parsing is not a member of package util

原因

因为 scala-parser-combinators 依赖在2.11中被移除了,必须自己添加

解决方案


org.scala-lang.modules
scala-parser-combinators_2.13
1.1.2

问题03

[ERROR] (class java.lang.RuntimeException/error reading Scala signature of JSON.class: Scala signature JSON has wrong version

原因

因为 scala-parser-combinators 的版本太高了

解决方案


org.scala-lang.modules
scala-parser-combinators_2.12
1.1.0

问题04

FATAL ERROR in native method: processing of -javaagent failed
Exception in thread “main” java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Caused by: java.lang.NoSuchFieldException: $jacocoAccess
at java.base/java.lang.Class.getField(Class.java:2000)

原因

因为jacoco-maven-plugin插件只支持到JAVA9

解决方案


org.jacoco
jacoco-maven-plugin
0.8.4

问题05

Exception in thread “ScalaTest-main” java.lang.NoClassDefFoundError: org/scalacheck/Test T e s t C a l l b a c k a t o r g . s c a l a t e s t . t o o l s . R u n n e r TestCallback at org.scalatest.tools.Runner TestCallbackatorg.scalatest.tools.Runner.runOptionallyWithPassFailReporter(Runner.scala:919)
at org.scalatest.tools.Runner . m a i n ( R u n n e r . s c a l a : 832 ) a t o r g . s c a l a t e s t . t o o l s . R u n n e r . m a i n ( R u n n e r . s c a l a ) C a u s e d b y : j a v a . l a n g . C l a s s N o t F o u n d E x c e p t i o n : o r g . s c a l a c h e c k . T e s t .main(Runner.scala:832) at org.scalatest.tools.Runner.main(Runner.scala) Caused by: java.lang.ClassNotFoundException: org.scalacheck.Test .main(Runner.scala:832)atorg.scalatest.tools.Runner.main(Runner.scala)Causedby:java.lang.ClassNotFoundException:org.scalacheck.TestTestCallback
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
… 3 more

原因

缺少scalacheck_2.13依赖

解决方案


org.scalacheck
scalacheck_2.13
1.14.0
test

问题06

error: object parsing is not a member of package util

原因

因为 scala-parser-combinators 依赖在2.11中被移除了,如果需要必须自己添加

解决方案


org.scala-lang.modules
scala-parser-combinators_2.13
1.1.2

问题07

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.20:integration-test (integration-test) on project commons-ui-expweb: Execution integration-test of goal o
rg.apache.maven.plugins:maven-failsafe-plugin:2.20:integration-test failed: A required class was missing while executing org.apache.maven.plugins:maven-failsafe-plugin:2.20:integration-
test: javax/xml/bind/JAXBException

原因

maven-failsafe-plugin版本太低,与scala不能很好兼容

解决方案



org.apache.maven.plugins
maven-failsafe-plugin
3.0.0-M3


integration-test

integration-test
verify




Tips:查看依赖
mvn dependency:tree

你可能感兴趣的:(scala,scala,scalatest,openJDK11)