Annotation Processor in IntelliJ and Gradle

前端 未结 4 1502
终归单人心
终归单人心 2020-12-17 09:18

tl;dr: I cannot configure IntelliJ to generate the java files in the same directory as gradle

I have a small project which uses the immutables annot

相关标签:
4条回答
  • 2020-12-17 09:32

    Hey there everyone I had the same issue and found a clean way of solving this issue. I am using two libraries that require annotation processing (Lombok and MapStruct).

    Also my IntelliJ is 2019.1 (update yours in case it's older) and Gradle 5.2.1.

    First let's configure IntelliJ:

    1. Disable Annotaion Processing in Settings, since we're going to delegate everything to Gradle:

    1. Delegeate IDE actions to Gradle:

    Last step is to configure your dependencies correctly in Gradle.

    1. Dependencies section in Gradle:

    Now you can execute the Build and Run from both command line and IDE.

    Cheers!

    0 讨论(0)
  • 2020-12-17 09:33

    Now https://github.com/tbroyer/gradle-apt-plugin states:

    The goal of this plugin was to eventually no longer be needed, being superseded by built-in features. This is becoming a reality with Gradle 5.2 and IntelliJ IDEA 2019.1.

    So:

    dependencies {
      compile("com.google.dagger:dagger:2.18")
      annotationProcessor("com.google.dagger:dagger-compiler:2.18")
    
      compileOnly("com.google.auto.factory:auto-factory:1.0-beta6")
      annotationProcessor("com.google.auto.factory:auto-factory:1.0-beta6")
    
      compileOnly("org.immutables:value-annotations:2.7.1")
      annotationProcessor("org.immutables:value:2.7.1")
    }
    

    compileOnly is necessary if you use annotations, compile if you use classes, annotationProcessor introduced in Gradle 4.6.

    To enable processing specific compile task:

    compileJava {
        options.annotationProcessorPath = configurations.annotationProcessor
    }
    

    To disable:

      compileTestJava {
          options.compilerArgs += '-proc:none'
      }
    
    0 讨论(0)
  • 2020-12-17 09:39

    2019.2.x

    • Disable annotation processor of intellij

    • add, build directory in your gradle build.gradle file

    • then run your gradle task to generate build file classes, example gradle compileJava

    • File -> project structure -> Modules -> Main Folder || remove exclude and add as source

    And project should find all annotation and generated source file. Hope it helps.

    0 讨论(0)
  • 2020-12-17 09:52

    UPDATE 2.2019
    since Gradle 5.2 there is an easy way to do it - see gavenkoas answer

    UPDATE 5.2018

    The easiest way, I know of is to use the apt-idea plugin

    Just activate the plugin in the build.gradle file:

    plugins {
        id 'java'
        id 'net.ltgt.apt-idea' version "0.15"
    }
    

    and then add the annotation processors to the annotationProcessor configuration:

    final DAGGER_VER = '2.16'
    dependencies {
        implementation "com.google.dagger:dagger:${DAGGER_VER}"
        annotationProcessor"com.google.dagger:dagger-compiler:${DAGGER_VER}"
    }
    

    Test-project on GitHub: ex.dagger
    (using IntelliJ 2018.1.4, Gradle 4.7)

    ORIG ANSWER

    There's a simple workaround using the parent-dir which works fine in IntelliJ 2016.3.4

    • Production sources directory: ../main
    • Test sources directory: ../test

    Now gradle and IntelliJ will generate the code to the same directories.

    Fixed in GitLab project V0.0.2

    see also: apt-gradle-plugin issue#35

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