java - 如何使用 Maven - intellij IDEA 在纯 java 项目中尝试 Dagger 2

标签 java maven intellij-idea dagger

我从 friend 那里听说 Dagger 2 在 Android 中使用它。挺好的!

但我有一个疯狂的想法,我想在 Maven 中构建的纯 Java 项目中尝试 Dagger 2 示例并使用 intellij IDEA。但是编译器的错误无法像 Dagger 用户指南那样从 ConffeeShop 接口(interface)生成 DaggerCoffeeShop 类。

我的所有示例代码都与示例相同。

CoffeeShop coffeeShop = DaggerCoffeeShop.builder()
    .dripCoffeeModule(new DripCoffeeModule())
    .build();

我尝试在 setting > compiler 中打开 enable annotation processing 但它不起作用。我需要你的帮助来完成我疯狂的想法。 :(

最佳答案

使用 JDK 8。它应该支持 JDK 9,但我不知道该怎么做 ;)

确保在 POM 中包含:

<dependencies>

    <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger</artifactId>
        <version>2.11</version>
    </dependency>
    <dependency>
        <groupId>com.google.dagger</groupId>
        <artifactId>dagger-compiler</artifactId>
        <version>2.11</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <annotationProcessorPaths>
                    <path>
                        <groupId>com.google.dagger</groupId>
                        <artifactId>dagger-compiler</artifactId>
                        <version>2.11</version>
                    </path>
                </annotationProcessorPaths>
            </configuration>
        </plugin>
    </plugins>
</build>

关于java - 如何使用 Maven - intellij IDEA 在纯 java 项目中尝试 Dagger 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40958056/

相关文章:

java - 在 gradle 中,如何为单独的任务创建单独的测试报告?

java - 在单个java文件中搜索算法(图)

java - 错误 : Wrapper cannot find servlet class VendorRegistration or a class it depends on

java - Spring - 使用 applicationContext.xml 和 XXXXX-servlet.xml

java - LAN 上的 Maven 设置

java - org.apache.camel.NoSuchLanguageException : No language could be found for: simple

java - 忽略 Mercurial 子目录

java - IntelliJ gradle 添加模块依赖

eclipse - 针对 Groovy/Grails 开发从 Eclipse 切换到 IntelliJ

grails - IntelliJ Grails View 始终消失