如何配置IntelliJ / gradle以使用dagger 2.0

时间:2015-04-10 13:11:35

标签: java gradle dagger-2

我有一个gradle项目,我想在其中使用dagger 2.0。我不知道如何配置IntelliJ和gradle来生成文件并让IntelliJ找到它们?

我的build.gradle文件如下:

apply plugin: 'java'
apply plugin: 'idea'

version = '1.0'

repositories {
    mavenCentral()
    maven {
        url "https://oss.sonatype.org/content/repositories/snapshots"
    }
}

dependencies {
    compile 'org.slf4j:slf4j-api:1.7.12'
    compile 'org.slf4j:slf4j-simple:1.7.12'
    compile 'commons-configuration:commons-configuration:1.10'
    compile 'commons-collections:commons-collections:3.2.1'
    compile 'com.google.dagger:dagger:2.0'
    compile 'com.google.dagger:dagger-compiler:2.0:jar-with-dependencies'
    compile 'com.pi4j:pi4j-distribution:1.1-SNAPSHOT'
}

在我的应用程序的构建目录中,文件DaggerXmlConfigurationComponent存在,这是Dagger创建的组件。但我无法在IntelliJ中使用它,因为它找不到类。

这不是Android应用程序,而是Raspberry Pi的应用程序。

8 个答案:

答案 0 :(得分:16)

您必须手动为IntelliJ启用注释处理:在设置...→构建,执行,部署→编译器→注释处理器中,选中启用注释处理并从项目类路径获取处理器。

答案 1 :(得分:8)

我找到了解决方案。

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

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "net.ltgt.gradle:gradle-apt-plugin:0.3"
  }
}

apply plugin: "net.ltgt.apt"

dependecies {
  apt 'com.google.dagger:dagger-compiler:2.0.1'
  compile 'com.google.dagger:dagger:2.0.1'
}

此外,如果您使用的是Intellij,建议采用以下配置:

然而,在IntelliJ IDEA中使用Gradle集成时,您需要手动启用注释处理,而不是构思任务:在设置...→构建,执行,部署→编译器→注释处理器中,选中启用注释处理和获取项目类路径中的处理器。要模仿Gradle行为和生成的文件行为,您可以将生产和测试源目录分别配置为build / generated / source / apt / main和build / generated / source / apt / test,并选择相对于以下方式存储生成的源:Module内容根。 我还必须从整个构建目录中删除Exclude并将生成的/ source / apt / main目录标记为源。

答案 2 :(得分:3)

我知道最简单的方法是使用apt-idea plugin

只需激活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)

答案 3 :(得分:2)

我也无法使任何插件工作,所以基于Stefan的响应,我做了以下工作,但令人讨厌的是IntelliJ似乎创建了组模块,以前没有。如果有人知道造成这种情况的原因我会非常想解决这个问题。

apply plugin: 'java'
apply plugin: 'idea'

configurations {
    compileDagger
}

def genPath = new File(buildDir,"generated/source/apt/main" )

task createGenPath << {
    if(!genPath.exists()){
        genPath.mkdirs()
    }
}

compileJava.dependsOn(createGenPath)

compileJava {
    source += genPath
    classpath += configurations.compileDagger
    options.compilerArgs += ['-s', genPath]
}

idea.module {
    sourceDirs += genPath
}

dependencies {
    compileDagger "com.google.dagger:dagger-compiler:${dagger2Version}"
    compile "com.google.dagger:dagger:${dagger2Version}"
}

答案 4 :(得分:2)

我完成了以下解决方案(它似乎是所有已发送答案中最简单的一个):

apply plugin: 'java'
apply plugin: 'idea'

def generatedMain = new File(buildDir, "generated/main")

compileJava {
    doFirst {
        generatedMain.mkdirs()
    }
    options.compilerArgs += ['-s', generatedMain]
}
idea.module.sourceDirs += generatedMain

dependencies {
    compileOnly 'com.google.dagger:dagger-compiler:2.8'
    compile 'com.google.dagger:dagger:2.8'
}

答案 5 :(得分:1)

我遇到了现有插件的问题,所以我在build.gradle添加了以下内容:

def daggerVersion = "2.4"

// APT >>
def genPath = new File(buildDir,"generated/java/APT" )

task createGenPath << {
    if(!genPath.exists()){
        genPath.mkdirs()
    }
}
compileJava.dependsOn(createGenPath)

compileJava {
     options.compilerArgs << '-s' << genPath
}
// APT <<


dependencies {
    compile "com.google.dagger:dagger:$daggerVersion"
    compile "com.google.dagger:dagger-compiler:$daggerVersion"
}

// APT IDEA >>
idea.module {
    sourceDirs += genPath
    // maybe add to generatedSourceDirs
    iml {
        withXml {
            File ideaCompilerXml = project.file('.idea/compiler.xml')
            if (ideaCompilerXml.isFile()) {
                Node parsedProjectXml = (new XmlParser()).parse(ideaCompilerXml)
                updateIdeaCompilerConfiguration(parsedProjectXml)
                ideaCompilerXml.withWriter { writer ->
                    XmlNodePrinter nodePrinter = new XmlNodePrinter(new PrintWriter(writer))
                    nodePrinter.setPreserveWhitespace(true)
                    nodePrinter.print(parsedProjectXml)
                }
            }
        }
    }
}

static void updateIdeaCompilerConfiguration( Node projectConfiguration) { //actually resets APT
    Object compilerConfiguration = projectConfiguration.component.find { it.@name == 'CompilerConfiguration' }
    compilerConfiguration.annotationProcessing.replaceNode{
        annotationProcessing() {
            profile(default: 'true', name: 'Default', enabled: 'true') {
                sourceOutputDir(name: '')
                sourceTestOutputDir(name: '')
                outputRelativeToContentRoot(value: 'true')
                processorPath(useClasspath: 'true')
            }
        }
    }
}
// APT IDEA <<

答案 6 :(得分:1)

就我而言,问题是IDEA为匕首生成的文件创建了一个单独的模块。我必须转到projectname_dagger并删除projectname_main模块(通过单击红色减号),然后单击Add Content Root并选择生成的源文件夹到我的 <input type="text" id="username" /> <!-- won't work --> <input type="text" name="username" /> <!-- will work --> <input type="text" name="username" id="username" /> <!-- will work too --> 模块并选择它

出于某种原因,我不得不删除Dagger的文件并让IDEA重新生成它们,因为我收到了项目中重复文件的错误。

现在它可以正常工作,关闭Annotation Processors的事件(我怀疑它们必须主要对Android项目很重要)。

答案 7 :(得分:0)

从net.ltgt.apt版本0.11(2018年2月)开始,您可以将插件net.ltgt.apt-idea应用于build.gradle

plugins {
    id "net.ltgt.apt-idea" version "0.18"
}

apply plugin: 'idea'
apply plugin: 'java'

dependencies {
    compile             "com.google.dagger:dagger:2.17"
    annotationProcessor "com.google.dagger:dagger-compiler:2.17"
}