IntelliJ和Gradle中的注释处理器 [英] Annotation Processor in IntelliJ and Gradle
问题描述
tl; dr :我无法配置IntelliJ在与gradle相同的目录中生成Java文件
tl;dr: I cannot configure IntelliJ to generate the java files in the same directory as gradle
我有一个小项目,它使用 immutables 注释处理器. 它可以在gradle命令行构建中按预期方式工作,但是我无法让IntelliJ将生成的文件输出到同一目录.
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.
完整项目可在 GitLab
分级配置:
我使用下面的gradle插件:
Gradle config:
I use the folowing gradle plugins:
- 渐变想法插件,用于处理想法配置
- gradle-apt-plugin ,它提供apt配置并处理编译类与注解处理有关的路径和想法配置(如果还应用了想法插件)
- gradle-idea plugin which handles the idea configuration
- gradle-apt-plugin which provides the apt configuration and handles the compile-class path and idea config related to annotation processing (if also the idea plugin is applied)
构建脚本的相关部分(链接至全文列表):
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"
}
当我启动./gradlew build
时,一切都按预期进行:
when I start ./gradlew build
everything is as expected:
- 处理源文件
DataEncoding.java
,生成的java文件DataEncodingEnabled.java
最终存储在 -
/build/generated/source/apt/main
在预期的软件包com.tmtron.immutables.data
下
- 并且生成的文件也被编译为.class文件
- The source file
DataEncoding.java
is processed an the generated java-fileDataEncodingEnabled.java
ends up in /build/generated/source/apt/main
under the expected packagecom.tmtron.immutables.data
- and the generated file is also compiled to a .class file
在IntelliJ中,我按照 gradle-apt-plugin的建议激活注释处理.文档:
In IntelliJ I activate the annotation processing as suggested by the gradle-apt-plugin docs:
然后我执行./gradlew clean
以确保以前的文件都消失了,然后在IntelliJ中单击Build
-Build Project
.
注解处理器已执行,但问题是生成的java文件最终在错误的位置:
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:
它位于:/build/generation/source/apt/main/build/generation/source/apt/main/com.tmtron.immutables.data
粗体部分是多余的.
It is in: /build/generated/source/apt/main/build/generated/source/apt/main/com.tmtron.immutables.data
the bold part is redundant.
我在做什么错了,如何正确设置它,以便IntelliJ和gradle在同一目录中生成文件?
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?
注意:
- 我当然已经尝试将IntelliJ批注配置中的生产源目录"保留为空,但他不起作用:然后它会自动使用生成",并且我也遇到了错误的路径.
- IntelliJ版本2016.3.4
推荐答案
更新2.2019
从Gradle 5.2开始,有一种简便的方法-请参见 gavenkoa 的
UPDATE 2.2019
since Gradle 5.2 there is an easy way to do it - see gavenkoas answer
更新5.2018
我知道,最简单的方法是使用 apt-idea插件
The easiest way, I know of is to use the apt-idea plugin
只需激活build.gradle
文件中的插件:
Just activate the plugin in the build.gradle
file:
plugins {
id 'java'
id 'net.ltgt.apt-idea' version "0.15"
}
,然后将注释处理器添加到annotationProcessor
配置中:
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}"
}
在GitHub上进行测试项目: ex.dagger
(使用IntelliJ 2018.1.4,Gradle 4.7)
Test-project on GitHub: ex.dagger
(using IntelliJ 2018.1.4, Gradle 4.7)
原始答案
使用父目录有一个简单的解决方法,在IntelliJ 2016.3.4中可以正常工作
There's a simple workaround using the parent-dir which works fine in IntelliJ 2016.3.4
- 生产来源目录:
../main
- 测试源目录:
../test
- Production sources directory:
../main
- Test sources directory:
../test
现在gradle和IntelliJ会将代码生成到相同的目录.
Now gradle and IntelliJ will generate the code to the same directories.
已修复 GitLab项目V0.0.2
另请参阅: apt-gradle-plugin问题#35
这篇关于IntelliJ和Gradle中的注释处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!