简体   繁体   中英

How to try Dagger 2 with pure java Project using Maven - intellij IDEA

i heard Dagger 2 from a friend use it in Android. it pretty good!

But i have a crazy idea, i want to try Dagger 2 example in a pure java project build in Maven and use intellij IDEA. But something wrong by Compiler couldn't generate DaggerCoffeeShop class from ConffeeShop Interface like Dagger user guide.

All my example code same as example.

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

I tried with turn on enable annotation processing in setting > compiler but it not work. I need your help to complete my crazy idea. :(

Use JDK 8. It should support JDK 9, but I did not figure it out how to do it ;)

Be sure to include in 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>

In Intelij Dagger2 creates auto generated classes under dir target\\generated-sources. You will have to add this folder to your source and you will able to use these auto generated java classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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