【问题标题】:How do I set up APT for Immutables such that Intellij Idea will recognize the generated code?如何为不可变对象设置 APT,以便 Intellij Idea 能够识别生成的代码?
【发布时间】:2017-11-02 22:59:39
【问题描述】:

我正在考虑从 maven 迁移到 gradle,在这种情况下,gradle 本身似乎工作正常,但 Idea 无法识别 Immutables 生成的源代码。

我读过这个blog post on APT,这就是我得到这个的原因。

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a commented-out sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.3/userguide/tutorial_java_projects.html
 */


// Apply the java plugin to add support for Java
apply plugin: 'java-library'
apply plugin: 'idea'

buildscript {
    repositories {
        maven {
            url 'https://d3vfm0n2cffdwd.cloudfront.net'
        }
        jcenter()
    }
    dependencies {
        classpath 'io.spring.gradle:dependency-management-plugin:1.0.0.RELEASE'
    }
}

apply plugin: 'io.spring.dependency-management'

dependencyManagement {
    imports {
        mavenBom 'com.xenoterracide:platform:0.1.39-SNAPSHOT'
    }
}

repositories {
    maven {
        url 'https://d3vfm0n2cffdwd.cloudfront.net'
    }
    jcenter()
}

configurations {
    apt
    aptCompile
}

// In this section you declare the dependencies for your production and test code
dependencies {
    implementation 'com.google.guava:guava'

    aptCompile 'org.immutables:value'
    compileOnly 'org.immutables:value'
    apt 'org.immutables:builder'

    // The production code uses the SLF4J logging API at compile time
    implementation 'org.slf4j:slf4j-api'

    // Declare the dependency for your favourite test framework you want to use in your tests.
    // TestNG is also supported by the Gradle Test task. Just change the
    // testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
    // 'test.useTestNG()' to your build script.
    testImplementation 'junit:junit'
    testImplementation 'org.assertj:assertj-core'
    testImplementation 'org.mockito:mockito-core'
    testImplementation 'org.hamcrest:hamcrest-library'
}

compileJava {
    options.annotationProcessorPath = configurations.aptCompile
}

更多代码见bitbucket

让我感动的一件事是,根据我的尝试,生成的 java 要么最终出现在 out 目录中,要么出现在类路径上的 build 中,当然到目前为止,这些都不能解决问题。

我该如何解决这个问题,以便 Idea 可以看到生成类型的来源(这样它就不会全部以红色突出显示)?

【问题讨论】:

  • 看看stackoverflow.com/a/46035904/104891是否有帮助。
  • @CrazyCoder 嗯...我真的不需要他们共享输出目录,只需要在编译时查看生成的源代码。
  • 查看youtrack.jetbrains.com/issue/IDEA-152581 了解前段时间已修复的相关问题。如果您的问题有所不同,请通过youtrack.jetbrains.com/issues/IDEA 报告。
  • @CrazyCoder 可能相关,突出显示的问题(除了需要取消选中为每个源集创建单独的模块)似乎是 out/production/classes/generated 没有标记为生成的源目录,所以即使编译器识别事物,语法高亮不识别。我不确定这是否是新问题 2017.2.5
  • 或者target/generated-sources/annotatons 对如何设置目录非常困惑,但更大的问题是这个想法不是自动将它们添加为生成的源目录

标签: java intellij-idea gradle annotation-processing immutables-library


【解决方案1】:

@CrazyCoder 的链接帮助我更接近解决方案,但这似乎解决了它。

idea {
    module {
        sourceDirs += file("out/production/classes/generated")
    }
}

【讨论】:

    猜你喜欢
    • 2018-03-20
    • 1970-01-01
    • 2014-03-29
    • 2017-09-24
    • 2012-05-15
    • 2015-10-08
    • 1970-01-01
    • 2012-03-15
    相关资源
    最近更新 更多