java - 如何为 Immutables 设置 APT,以便 Intellij Idea 能够识别生成的代码?

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

我正在考虑从 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 可以看到生成类型的源代码(这样它就不会全部以红色突出显示)?

最佳答案

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

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

关于java - 如何为 Immutables 设置 APT,以便 Intellij Idea 能够识别生成的代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47085917/

相关文章:

java - AsyncTask 在 Kotlin 中的使用

java - 复制任务未将复制的文件放入其TaskOutputs中-为什么?

intellij-idea - 如何在intellij中格式化花括号以进行自动格式化

java - 如何通过Java驱动正确连接到Atlas M0 (Free Tier) 集群?

eclipse - 在 Eclipse EJB Gradle 项目中将 persistence.xml 放在哪里?

java - Google App Engine 上的 GSON 抛出安全异常

javascript - 回调、Promise 和事件的区别 JavaScript/React-Native <-> Java/Android

ubuntu - Intellij 启动器不能统一工作?

gradle - 自定义选择依赖版本

android - 在 Android Studio 3.1 中将问题从 Gradle 3 迁移到 4