Dagger 2 does not generate components on Eclipse

I created a Java application with Eclipse and I use Maven to manage packages. A few days ago, I was able to configure my application to work with Dagger 1 (add dependencies to the pom file, enable annotation processing and add daggers, daggers, javax and javawriter jars to the Factory path). After several discussions with my colleagues, we decided to use Dagger 2. I tried to migrate the implementation of Dagger 1 to Dagger 2 by following the Dagger 2 documentation , but that did not work.

For some inexplicable reason, the @Component class with the Dagger prefix is ​​not generated.

Because of this, I decided to try a sample of the dagger 2 coffee .

I created a new Java Eclipse project, converted it to Maven, added sample code and 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:

 Exception in thread "main" java.lang.Error: Unresolved compilation problem: DaggerCoffeeApp_Coffee cannot be resolved 

I extracted the Coffee component interface into a separate file (named Coffee.java ) and tried again, but got the same error.

I removed the Factory Path of Dagger 1 containers, but the result is still the same. If I try to add Dagger 2 banks, I get a window Several problems with the following error text Errors occurred during the build. Errors running builder 'Java Builder' on project 'dagger'. com/google/common/collect/SetMultimap 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 a jar of dagger-compiler .

If I remove each jar from the Factory Path, the assembly still fails.

I tried to find the DaggerCoffee file, which should be automatically generated, but I could not find it.

How to use Dagger 2 with Eclipse? I dye the documentation several times and spent the day researching this, but so far no luck.

+6
source share
4 answers

So, after many trials and errors, I was able to configure Dagger 2 in Eclipse. I went to Project => Properties => Java Compiler => Processing annotation => Factory Path and added banks for dagger , dagger-compiler , dagger-producers , javax.inject-1 and guava-18.0 . Bright guava-18.0 is the hard 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 led to an earlier version of guava loading into my maven dependencies, but it did not work with Dagger 2.

So, the moral of this story is that you should always check dependency versions.

+7
source

It also works using m2e-apt. No need to add custom libraries:

  • Install m2e-apt
  • Window → Settings → Maven → Annotation Processing: Select "Automatically configure JTT APT"
  • Update maven project
+12
source

In addition to what Jocky said, I also needed to add the following to my room. Please note that the JDK version number must be set since Maven uses v1.5 by default and annotation code generation is added in v1.6:

 <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.4</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> <dependencies> <dependency> <groupId>com.google.dagger</groupId> <artifactId>dagger-compiler</artifactId> <version>2.0.2</version> <optional>true</optional> </dependency> </dependencies> </plugin> </plugins> 
+2
source

I also found getting dagger v2.8 to work in Eclipse 4.6.1 (Neon) with JDK 1.7 is a bit of a challenge. After installing m2e-apt and turning on annotation processing in “Window → Preferences → Maven → Editing Annotations” and “Project / Properties → Maven → Editing Annotations” I still had to add the text of the plugins to my pom. xml file. Here is a snippet of the POM file that worked for me:

 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.4</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> <dependencies> <dependency> <groupId>com.google.dagger</groupId> <artifactId>dagger-compiler</artifactId> <version>2.8</version> <optional>true</optional> </dependency> </dependencies> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>com.google.dagger</groupId> <artifactId>dagger</artifactId> <version>2.8</version> </dependency> </dependencies> 
0
source

All Articles