I have created a Java application with Eclipse and I'm using Maven for the package management. Several days ago I was able to configure my application to work with Dagger 1 (adding the dependencies to the pom file, enabling the annotation processing and adding the dagger, dagger-compile, javax and javawriter jars to the Factory Path). After several discussion with my colleagues we decided to use Dagger 2. I tried to migrate the Dagger 1 implementation to Dagger 2 by following the Dagger 2 documentation, but it did not work.
For some unexplained reason the @Component
class with the Dagger
prefix is not generated.
Because of this I decided to try the Dagger 2 Coffee sample.
I created a new Eclipse Java project, converted it to Maven, added the sample code and the Dagger 2 dependencies to the pom file:
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.0.1</version>
<optional>true</optional>
</dependency>
My build failed with the following error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
DaggerCoffeeApp_Coffee cannot be resolved
I extracted the Coffee
component interface to a separate file (named Coffee.java
) and tried again but I got the same error.
I removed the Factory Path jars from Dagger 1, but the result was still the same. If I try to add the Dagger 2 jars, I get the Multiple problems have occurred window with the following error text
Errors occurred during the build.
Errors running builder 'Java Builder' on project 'dagger'.
com/google/common/collect/SetMultimap
I found out that the problem occurs when I add the dagger-compiler
jar.
If I remove every jar from the Factory Path the build still fails.
I tried to find the DaggerCoffee
file, that should be automatically generated but I couldn't find it.
How can I use Dagger 2 with Eclipse? I red the documentation several times and spend a day researching this, but no luck so far.
So after a lot of trials and errors I finally managed to configure Dagger 2 in Eclipse. I went to Project => Properties => Java Compiler => Annotation Processing => Factory Path and added the jars for dagger
, dagger-compiler
, dagger-producers
, javax.inject-1
and guava-18.0
. The guava-18.0
jar is the tricky part. I had another dependency that used an older version of guava-14.0.1
and it was added first to the pom.xml
file. This resulted in the older version of guava
downloaded to my maven dependencies, but it did not work with Dagger 2.
So the moral of the story is that you should always check the dependency versions.