Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 not generateing components on Eclipse

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.

like image 451
Ivan Stoyanov Avatar asked Jul 27 '15 13:07

Ivan Stoyanov


People also ask

What is dagger2 component?

Components are essentially the glue that holds everything together. They are a way of telling Dagger 2 what dependencies should be bundled together and made available to a given instance so they can be used. They provide a way for a class to request dependencies being injected through their @Inject annotation.

How does Dagger2 work?

Dagger 2 walks through the dependency graph and generates code that is both easy to understand and trace, while also saving you from writing the large amount of boilerplate code you would normally need to write by hand to obtain references and pass them to other objects as dependencies.

What is dagger2?

Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.

What is dagger2 module?

Modules are just pieces of code that describe HOW you want to build your dependencies. For example, if you need to create a new object every time, the provider will look like this: @Provides fun provideUnscoped() : IUnscoped = Unscoped()


1 Answers

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

  • Install m2e-apt
  • Window -> Preferences -> Maven -> Annotation Processing: Select "Automatically configure JDT APT"
  • Update the maven project
like image 181
Jotschi Avatar answered Sep 22 '22 07:09

Jotschi