Why do I need jsr305 to use guava in scala?

前端 未结 3 1253
情书的邮戳
情书的邮戳 2021-01-03 22:59

I have the following scala file:

object SGuavaTryout {
  com.google.common.cache.CacheBuilder.newBuilder()
}

I compile with guava-11.0.2.ja

相关标签:
3条回答
  • 2021-01-03 23:10

    That's because of the way the Scala compiler is designed, it requires all the types exposed by a class to be available at compile time, whereas the Java compiler effectively doesn't care.

    0 讨论(0)
  • 2021-01-03 23:12

    You can add this dependency:

    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>2.0.3</version>
        <scope>provided</scope>
    </dependency>
    

    thus the compilation will work and this won't come to the final release.

    0 讨论(0)
  • 2021-01-03 23:28

    Sean Parsons answered your first question, by explaining why Scala requires the JSR 305 dependency.

    As to the "official" JSR 305 implementation to use with Guava, I'd use the one they declare in their pom.xml:

    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>1.3.9</version>
    </dependency>
    

    If you were using Maven, I think it would add the dependency to the classpath automatically.

    Note: you can download the jar directly from the Maven Central repository.

    0 讨论(0)
提交回复
热议问题