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 annotation processor. It works as expected in the gradle command line build, but I cannot get IntelliJ to output the generated files to the same directory.
The full project is available on GitLab
Gradle config:
I use the folowing gradle plugins:
relevant parts of the build-script (link to the full listing):
apply plugin: 'java' apply plugin: "net.ltgt.apt" apply plugin: 'idea' dependencies { def immutablesVersion = '2.3.9' compileOnly "org.immutables:value:$immutablesVersion:annotations" compileOnly "org.immutables:encode:$immutablesVersion" apt "org.immutables:value:$immutablesVersion" }
when I start ./gradlew build
everything is as expected:
DataEncoding.java
is processed an the generated java-file DataEncodingEnabled.java
ends up in/build/generated/source/apt/main
under the expected package com.tmtron.immutables.data
In IntelliJ I activate the annotation processing as suggested by the gradle-apt-plugin docs:
Then I execute ./gradlew clean
to make sure, that the previous files are gone and then I click Build
- Build Project
in IntelliJ.
The annotation processor is executed, but the problem is that the generated java file ends up in the wrong location:
It is in: /build/generated/source/apt/main/build/generated/source/apt/main/com.tmtron.immutables.data
the bold part is redundant.
What am I doing wrong and how can I set it up correctly, so that IntelliJ and gradle generate the files in the same directory?
Notes:
The annotation processor can validate, generate, and modify your code based on the annotations, which help you significantly reduce the amount of code you need to write. The annotation processor could be stored inside your project. In this case IntelliJ IDEA obtains it from the classpath.
Annotation processing is a tool built into javac for scanning and processing annotations at compile time. It can create new source files; however, it can't modify existing ones. It's done in rounds. The first round starts when the compilation reaches the pre-compile phase.
An annotation processor is a class which extends AbstractProcessor (in the package javax. annotation. processing ) providing the functionality needed to generate whatever it needs to generate, such as classes in the case of mapstruct.
Enable annotationsRight-click the gutter in the editor or in the Differences Viewer and select Annotate with Git Blame from the context menu. You can assign a custom shortcut to the Annotate command: go to the Keymap page of the IDE settings Ctrl+Alt+S and look for Version Control Systems | Git | Annotate.
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' }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With