【问题标题】:How to make GitHub's Immutables work in IntelliJ + Gradle如何让 GitHub 的 Immutables 在 IntelliJ + Gradle 中工作
【发布时间】:2018-05-01 07:24:05
【问题描述】:

我使用 GitHub 的 Immutables 库进行 Android 开发,现在我也想在后端尝试一下。

在 Android 中,为了使用该库,我只需要这样做:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // immutable entities generation
    provided "org.immutables:value:2.5.5" // for annotations
    provided "org.immutables:builder:2.5.5" // for annotations
    provided "org.immutables:gson:2.5.5" // for annotations

    ... other dependencies
}

当我尝试将上述依赖项复制到我的 Java 项目的 build.gradle 中时,我收到此错误:

Error:(24, 0) Gradle DSL method not found: 'provided()'

我尝试将provided替换为compileOnlycompile,但是没有生成带有@Value.Immutable注释的接口的实现。

如何让它发挥作用?

【问题讨论】:

    标签: java intellij-idea gradle build.gradle immutables-library


    【解决方案1】:

    我无法添加评论(代表太低),但对于未来的读者,我想扩展 Vasiliy answer

    就我而言(5.2.1 版中的 gradle 包装器),以下代码会自动神奇地发现生成的源:

    dependencies {
        def immutablesVersion = "2.8.2"
        annotationProcessor "org.immutables:value:$immutablesVersion" // <--- this is important
        compileOnly "org.immutables:value:$immutablesVersion"
    }
    

    我不需要更改 IDE 注释处理器选项中的任何内容,它开箱即用。

    【讨论】:

    • 由于这是对现有答案的扩展,它提供了额外信息来回答问题,因此将其作为答案发布是完全合法的。即使您拥有评论特权,答案仍然是合理的。作为一般提示,如果您直接引用另一个答案,您可以像我一样通过编辑您的帖子链接到它。我知道如果只有一个其他答案,这可能看起来并不重要,但将来会有更多答案,所以很高兴:)
    【解决方案2】:

    找到了答案。分享以防对任何人(或将来我自己)有帮助。

    首先,我必须在 IntelliJ 中启用注释处理,如 here 所述(尽管该选项现在位于 Settings &gt; Build, Execution, Deployment &gt; Compiler &gt; Annotation Processors)。

    之后,以下代码开始实际生成实现:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    
        // immutable entities generation
        compile "org.immutables:value:2.5.5" // for annotations
        compile "org.immutables:builder:2.5.5" // for annotations
        compile "org.immutables:gson:2.5.5" // for annotations
    
        ... other dependencies
    }
    

    但是,我仍然无法将实现自动导入到源文件中。

    为了允许发现生成的类,我必须右键单击main 包中的generated 文件夹,然后单击Mark Directory As &gt; Generated Sources Root

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多