作者:杜伟丿2552 | 来源:互联网 | 2023-02-10 18:41
tl; dr:我无法配置IntelliJ来生成与gradle相同的目录中的java文件
我有一个使用immutables注释处理器的小项目.它在gradle命令行构建中按预期工作,但我无法让IntelliJ将生成的文件输出到同一目录.
整个项目可在GitLab上获得
Gradle配置:
我使用以下gradle插件:
gradle-idea插件,用于处理构思配置
gradle-apt-plugin,提供apt配置并处理与注释处理相关的编译类路径和构思配置(如果还应用了构思插件)
build-script的相关部分(链接到完整列表):
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
一切都如预期:
DataEncoding.java
处理源文件,生成的java文件DataEncodingEnabled.java
最终进入
/build/generated/source/apt/main
根据预期的方案 com.tmtron.immutables.data
并且生成的文件也被编译为.class文件
在IntelliJ中,我按照gradle-apt-plugin文档的建议激活注释处理:
然后我执行./gradlew clean
以确保先前的文件已经消失然后我单击Build
- Build Project
在IntelliJ中.
执行注释处理器,但问题是生成的java文件最终位于错误的位置:
它位于:/ build/generated/source/apt/main/build/generated/source/apt/main /com.tmtron.immutables.data
,粗体部分是多余的.
我做错了什么以及如何正确设置,以便IntelliJ和gradle在同一目录中生成文件?
笔记:
我当然已经尝试将IntelliJ注释配置中的"生成源目录"留空,但是它不起作用:然后它会自动使用"生成"而我最终也会得到错误的路径.
IntelliJ版本2016.3.4
TmTron..
13
自Gradle 5.2以来更新2.2019有一个简单的方法 - 请参阅gavenkoa的回答
更新5.2018
我知道最简单的方法是使用apt-idea插件
只需激活build.gradle
文件中的插件:
plugins {
id 'java'
id 'net.ltgt.apt-idea' version "0.15"
}
然后将注释处理器添加到annotationProcessor
配置中:
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)
ORIG ANSWER
使用parent-dir有一个简单的解决方法,在IntelliJ 2016.3.4中可以正常工作
生产来源目录: ../main
测试源目录: ../test
现在gradle和IntelliJ将生成相同目录的代码.
在GitLab项目V0.0.2中修复
另见:apt-gradle-plugin问题#35
1> TmTron..:
自Gradle 5.2以来更新2.2019有一个简单的方法 - 请参阅gavenkoa的回答
更新5.2018
我知道最简单的方法是使用apt-idea插件
只需激活build.gradle
文件中的插件:
plugins {
id 'java'
id 'net.ltgt.apt-idea' version "0.15"
}
然后将注释处理器添加到annotationProcessor
配置中:
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)
ORIG ANSWER
使用parent-dir有一个简单的解决方法,在IntelliJ 2016.3.4中可以正常工作
生产来源目录: ../main
测试源目录: ../test
现在gradle和IntelliJ将生成相同目录的代码.
在GitLab项目V0.0.2中修复
另见:apt-gradle-plugin问题#35
2> gavenkoa..:
现在https://github.com/tbroyer/gradle-apt-plugin指出:
该插件的目标是最终不再需要,已被内置功能取代。随着Gradle 5.2和IntelliJ IDEA 2019.1成为现实。
所以:
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
如果使用注释,compile
则必须使用Gradle 4.6中annotationProcessor
引入的类。
要启用处理特定的编译任务:
compileJava {
options.annotatiOnProcessorPath= configurations.annotationProcessor
}
禁用:
compileTestJava {
options.compilerArgs += '-proc:none'
}
3> de.la.ru..:
嘿,每个人我都有同样的问题,并找到了解决此问题的干净方法。我正在使用两个需要批注处理的库(Lombok和MapStruct)。
我的IntelliJ也是2019.1(如果年纪较大,请更新)和Gradle 5.2.1。
首先让我们配置IntelliJ:
在设置中禁用注释处理,因为我们将所有内容委托给Gradle:
将IDE操作委托给Gradle:
最后一步是在Gradle中正确配置依赖项。
Gradle中的Dependencies部分:
现在,您可以从命令行和IDE中执行“生成和运行”。
干杯!