Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 204
Wing Avatar asked Dec 04 '16 11:12

Wing


People also ask

How do I run a Maven clean test in IntelliJ?

Run testsOpen the Maven tool window. Under the Lifecycle node select test. Note that goals specified in the Maven surefire plugin will be activated at this phase and all tests in a project or in a module will be run.

How do I point to Maven in IntelliJ?

In the Project tool window, right-click your project and select Add Framework Support. In the dialog that opens, select Maven from the options on the left and click OK. IntelliJ IDEA adds a default POM to the project and generates the standard Maven layout in Project tool window.


1 Answers

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>
like image 104
Francesco Gabbrielli Avatar answered Sep 21 '22 23:09

Francesco Gabbrielli