[英]IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources
[英]Is there any way of making IntelliJ IDEA recognizing Dagger 2 generated classes in a Java project?
語境
我已經在 java 中開始了一個個人項目,其中Gradle
作為構建系統,我想使用 Dagger 2 作為 DI。 這樣做的主要原因是習慣該庫並能夠在更大的項目中輕松使用它。
我試過什么
我已經設法讓Google 示例在 IntelliJ IDEA 上運行
問題
IntelliJ IDEA 一直告訴我它無法解析生成的 class (在本例中DaggerCoffeeApp_Coffee
)。 不知道編寫的代碼是否正確有點煩人(特別是當您學習使用 Dagger 2 時)。
所有 java 類都與Google 示例相同。 這是我的build.gradle
文件:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
compile 'com.google.dagger:dagger-compiler:2.0.1'
}
問題
有什么方法可以讓 IntelliJ IDEA 將DaggerCoffeeApp_Coffee
識別為生成的 class (因此可以通過 `ctrl + 左鍵單擊實現 go)?
終於我做到了!
我必須添加apt
和idea
插件,所以現在我的build.gradle
文件如下所示:
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "net.ltgt.gradle:gradle-apt-plugin:0.4"
}
}
apply plugin: "net.ltgt.apt"
apply plugin: 'java'
apply plugin: 'idea'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.0.1'
apt 'com.google.dagger:dagger-compiler:2.0.1'
}
我發現的最簡單方法:
添加idea
插件並添加Dagger2依賴項,如下所示:
plugins { id "net.ltgt.apt" version "0.10" } apply plugin: 'java' apply plugin: 'idea' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile 'com.google.dagger:dagger:2.11' apt 'com.google.dagger:dagger-compiler:2.11' }
打開IntelliJ的Annotation Processing
:轉到Settings
並搜索“ Annotation Processors
,選中啟用注釋處理,如下圖所示:
您必須在IntelliJ中手動啟用注釋處理 。
來自:設置->構建,執行,部署->編譯器->注釋處理器-> 啟用注釋處理並從項目類路徑獲取處理器
然后重建項目,您將在項目中找到生成的類。
請注意,我已經在(java) android項目中使用了此解決方案。
我使用的版本, 2017.3.3
的IntelliJ IDEA的,版本0.14
中的net.ltgt.apt
插件和版本2.14.1
匕首和以及應用idea
在插件build.gradle
文件(如Pelocho的答案 )我發現我還必須告訴IntelliJ在哪里可以找到Dagger生成的源,如下所示:
apply plugin: 'idea'
idea {
module {
sourceDirs += file("$buildDir/generated/source/apt/main")
testSourceDirs += file("$buildDir/generated/source/apt/test")
}
}
這是對我有用的解決方案:
文件->項目結構->(在模塊列表下選擇您的項目)->打開“依賴項”選項卡
然后,單擊綠色的“ +”號,選擇“ JAR或目錄”,然后選擇“ build / classes / main”文件夾。
另一個解決方案是使用build.gradle中的“ dependencies”塊將文件夾與構建類文件鏈接:
這是我要使Idea與Dagger2和gradle一起工作所要做的。
將以下內容添加到build.gradle
文件中,以便Idea可以將生成的類視為源。
sourceDirs += file("$projectDir/out/production/classes/generated/")
這是我的build.gradle
的完整列表
plugins {
id 'java'
id 'idea'
id "net.ltgt.apt" version "0.10"
}
idea {
module {
sourceDirs += file("$projectDir/out/production/classes/generated/")
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.dagger:dagger:2.16'
apt 'com.google.dagger:dagger-compiler:2.16'
}
sourceCompatibility = 1.8
另外,我必須添加以下gradle任務(到我的build.gradle文件中)以清除我的out
目錄。 當我移動一些文件並且Dagger2重新生成源文件時, out
目錄沒有被清除:(。我也在運行配置中包括了此任務,以便在重建項目之前觸發它。
task clearOutFolder(type: Delete) {
delete 'out'
}
使用IntelliJ IDEA 2019.1和Gradle 5.4.1,這似乎足夠了:
plugins {
id 'java'
}
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'com.google.dagger:dagger:2.23.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.23.1'
}
不過,我不知道此解決方案適用的最低版本。
我有一個類似的問題,我很長一段時間都找不到原因。
剛推出,結果令我驚訝。 Intellij Idea 2018.3.6- build.gradle
:
plugins {
id "java"
}
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile 'com.google.dagger:dagger:2.11'
apt 'com.google.dagger:dagger-compiler:2.11'
}
以下在 IntelliJ 2021.3.3 (UE) 上為我工作
plugins {
id 'java'
id 'idea'
id("com.github.johnrengelman.shadow") version "7.1.2"
}
idea {
module {
sourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/main")
testSourceDirs += file("$projectDir/build/generated/sources/annotationProcessor/java/test")
}
}
group 'com.codigomorsa'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
annotationProcessor 'com.google.dagger:dagger-compiler:2.44'
implementation 'com.google.code.gson:gson:2.9.1'
implementation 'com.google.dagger:dagger:2.44'
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.44'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
test {
useJUnitPlatform()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.