William
William

Reputation: 942

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. :(

Upvotes: 8

Views: 4847

Answers (2)

expert21
expert21

Reputation: 31

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.

Upvotes: 3

Francesco Gabbrielli
Francesco Gabbrielli

Reputation: 369

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>

Upvotes: 8

Related Questions