繁体   English   中英

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

[英]How do I configure IntelliJ/gradle to use dagger 2.0

我有一个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的应用程序。

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

我找到了解决方案。

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目录标记为源。

我也无法使任何插件工作,所以基于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}"
}

我知道最简单的方法是使用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)

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

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'
}

我遇到了现有插件的问题,所以我在build.gradle添加了以下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 <<

在我的例子中,问题是IDEA为匕首生成的文件创建了一个单独的模块。 我必须转到File -> Project Structure -> Modules并删除projectname_dagger模块(通过单击红色减号),然后通过单击Add Content Root并选择它,将生成的源文件夹添加到我的projectname_main模块。

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

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

从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"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM