对于scala来说,我是新手,我正在第一步中使用https://github.com/embeddedkafka/embedded-kafka 根据自述文件顶部的第二个示例:
import net.manub.embeddedkafka.EmbeddedKafka
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike
class MinimalTest extends AnyWordSpecLike with Matchers {
"runs with embedded kafka" should {
"work" in {
EmbeddedKafka.start()
1 + 1 shouldBe 2
// ... code goes here
EmbeddedKafka.stop()
}
}
}
运行此测试时,失败的级别低于我所熟悉的级别:
MinimalTest:
runs with embedded kafka
***RUN ABORTED***
java.lang.NoClassDefFoundError: scala/collection/GenTraversableOnce
at com.myorganization.api.MinimalTest.$anonfun$new$2(MinimalTest.scala:13)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.wordspec.AnyWordSpecLike$$anon$3.apply(AnyWordSpecLike.scala:1076)
at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
...
Cause: java.lang.ClassNotFoundException: scala.collection.GenTraversableOnce
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at com.myorganization.api.MinimalTest.$anonfun$new$2(MinimalTest.scala:13)
at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
...
我怀疑依赖版本不匹配,但无法发现它。以下是我的相关build.gradle内容:
plugins {
id 'java'
id 'scala'
}
task spec(dependsOn: ['testClasses'], type: JavaExec) {
main = 'org.scalatest.tools.Runner'
args = ['-R', 'build/classes/scala/test', '-o']
classpath = sourceSets.test.runtimeClasspath
}
dependencies {
compile 'io.confluent:kafka-streams-avro-serde:5.4.0'
compile 'io.github.embeddedkafka:embedded-kafka-streams_2.12:2.4.0'
compile 'io.github.embeddedkafka:embedded-kafka_2.12:2.4.0'
compile 'org.apache.avro:avro:1.9.1'
compile 'org.apache.kafka:kafka-clients:2.4.0'
compile 'org.apache.kafka:kafka-streams:2.4.0'
compile 'org.apache.kafka:kafka_2.13:2.4.0'
compile 'org.scala-lang:scala-reflect:2.12.6'
testCompile 'io.github.embeddedkafka:embedded-kafka-schema-registry_2.12:5.4.0' // match schema registry version
testCompile 'io.github.embeddedkafka:embedded-kafka-streams_2.13:2.4.0' // match kafka streams version
testCompile 'io.github.embeddedkafka:embedded-kafka_2.13:2.4.0' // match kafka version
testCompile 'org.scala-lang:scala-library:2.13.2'
testCompile 'org.scalatest:scalatest_2.13:3.1.2'
testImplementation 'junit:junit:4.11'
testRuntime 'org.pegdown:pegdown:1.4.2'
}
1条答案
按热度按时间z4bn682m1#
gradle对于scala的可传递依赖确实有点搞笑,因为它不会自动计算一组连贯的版本。
“missing”类scala/collection/gentraversableonce是scala库的一部分,在2.12.x中提供
因此,您应该能够通过以下方式解决此问题:
答。在运行时类路径上显式声明scala库版本:
implementation group: 'org.scala-lang', name: 'scala-library', version: '2.12.6'
b。检查库的2.13版本的所有其他依赖项(正如@shankarshastri所建议的)。似乎嵌入式Kafka在这里有一个2.12兼容版本:https://mvnrepository.com/artifact/io.github.embeddedkafka/embedded-kafka_2.12/2.5.0完成此操作后,假设您的ide设置为与
build.gradle
文件中,您应该能够查看gradle计算的依赖项(声明的和可传递的)。如果仍然有问题,请手动检查这些问题,看看org.scala-lang:scala-library
库丢失或声明了两次。如果有多个声明,可以在mvncentral.com中查看每个库的依赖项。n、 b.步骤(a)的原因是您的“编译”依赖项中有scala工件,因此我假设您的所有代码(不仅仅是测试代码)都在使用scala。