GWT & Dagger 2

1,252 views
Skip to first unread message

Vasco Andrade Silva

unread,
Feb 24, 2015, 8:29:17 AM2/24/15
to google-we...@googlegroups.com
Hi all,

I've been trying Dagger 2 with GWT and have been fighting to get things working. Could someone help?

My pom:

<dependency>
 
<groupId>com.google.dagger</groupId>
 
<artifactId>dagger</artifactId>
 
<version>2.0-SNAPSHOT</version>
</dependency>
<dependency>
 
<groupId>com.google.dagger</groupId>
 
<artifactId>dagger-compiler</artifactId>
 
<version>2.0-SNAPSHOT</version>
 
<optional>true</optional>
</dependency>


[...]


<plugin>
 
<groupId>org.codehaus.mojo</groupId>
 
<artifactId>gwt-maven-plugin</artifactId>
 
<version>${gwtVersion}</version>
 
<executions>
 
<execution>
 
<goals>
 
<goal>compile</goal>
 
<goal>test</goal>
 
</goals>
 
</execution>
 
</executions>
 
<configuration>
 
<module>my.Module</module>
 
<extraJvmArgs>-Xmx2048M -XX:MaxPermSize=128M</extraJvmArgs>
 
<runTarget>index.html</runTarget>
 
<warSourceDirectory>${project.build.directory}/${tmpTargetDirectory}</warSourceDirectory>
 
<buildOutputDirectory>${project.build.directory}/${tmpTargetDirectory}/WEB-INF/classes/</buildOutputDirectory>
 
<hostedWebapp>${project.build.directory}/${tmpTargetDirectory}</hostedWebapp>
 
<webappDirectory>${project.build.directory}/${tmpTargetDirectory}</webappDirectory>
 
<webXml>${project.build.directory}/${tmpTargetDirectory}/WEB-INF</webXml>
 
<generateDirectory>src/main/java</generateDirectory>
 
<bindAddress>0.0.0.0</bindAddress>
 
<jsInteropMode>JS</jsInteropMode>
 
<closureCompiler>true</closureCompiler>
 
</configuration>
</plugin>


My issues are:

#1 generated sources aren't available for gwt compilation
running:

mvn compile

no errors are found in the compilation phase, dagger 2 generated sources land in target/generated-sources/annotations/

However running:

mvn compile gwt:compile

GWT compilation outputs the following error:

[ERROR] Errors in 'file:/path/to/project/src/main/java/path/to/project/client/ProjectEntryPoint.java'
[INFO] [ERROR] Line 79: No source code is available for type path.to.project.client.Dagger_ProjectComponent; did you forget to inherit a required module?
[INFO] [ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly



the problem seems related with the fact that GWT compilation isn't including the generated sources, as target/generated-sources/annotations/ isn't included in the classpath of the GWT compiler. The project class paths that are included in GWT compile command are:
/path/to/project/target/tmp/WEB-INF/classes
/path/to/project/target/src/main/java


#2 Eclipse "doesn't follow" Dagger 2 generate sources (not related with GWT per-si)
I configured Eclipse (Luna edition) with m2e-apt plugin (http://marketplace.eclipse.org/content/m2e-apt). My project works as a maven project in Eclipse.

However when I change something in dagger (Component, Module, etc.) the generated sources aren't immediately generated, I need to do "Maven > Update Project".

#3 Eclipse imports errors on Dagger 2 generated files open (not related with GWT per-si)
When I open a file that was generated by dagger 2 (in target/generated/sources/annotations) eclipse doesn't know how to import other sources from target/generated/sources/annotations which lead to several compile time errors in those generated files. Additionally, from that point on Eclipse confuses himself and makes the project source files (in src/main/java) that are referring to generated dagger files (e.g. Dagger_ProjectComponent) to also be in error (eclipse doesn't know anymore to import those symbols).

To overcome this I have to do "Maven > Update Project". Note, however, that every time I open a dagger2 generated file, this happens again.

--
Has someone succeeded getting dagger 2 and GWT (and Eclipse) working together smoothly?

Also, any help to overcome the reported issues would be great (specially issue #1).

Thank you,
Vasco

Vasco Andrade Silva

unread,
Feb 24, 2015, 8:54:04 AM2/24/15
to google-we...@googlegroups.com
Errata: 
"""
The project class paths that are included in GWT compile command are:
/path/to/project/target/tmp/WEB-INF/classes
/path/to/project/src/main/java

Jens

unread,
Feb 24, 2015, 8:54:57 AM2/24/15
to google-we...@googlegroups.com
You need to add the generated sources as Eclipse source folder to make them usable in Eclipse, e.g.: http://stackoverflow.com/questions/7160006/m2e-and-having-maven-generated-source-folders-as-eclipse-source-folders

Maybe the above also fixes the gwt:compile class path issue.

For successful GWT compilation you also need Dagger + javax.inject sources and a GWT module to make them available to GWT compiler. I have done an initial pull request for Dagger to simplify that in the future: https://github.com/google/dagger/pull/119

-- J.

Vasco Andrade Silva

unread,
Feb 24, 2015, 10:08:33 AM2/24/15
to google-we...@googlegroups.com
Jens thank you for your reply.

What's the best way to include your PR (https://github.com/google/dagger/pull/119) in a project?

Jens

unread,
Feb 24, 2015, 10:36:24 AM2/24/15
to google-we...@googlegroups.com

What's the best way to include your PR (https://github.com/google/dagger/pull/119) in a project?

Its a branch in my dagger fork that you can simply build and install into your local repo: https://github.com/jnehlmeier/dagger/tree/gwt-integration 

-- J.

Thomas Broyer

unread,
Feb 24, 2015, 12:07:04 PM2/24/15
to google-we...@googlegroups.com


On Tuesday, February 24, 2015 at 2:29:17 PM UTC+1, Vasco Andrade Silva wrote:
My issues are:

#1 generated sources aren't available for gwt compilation
running:

mvn compile

no errors are found in the compilation phase, dagger 2 generated sources land in target/generated-sources/annotations/

However running:

mvn compile gwt:compile

GWT compilation outputs the following error:

[ERROR] Errors in 'file:/path/to/project/src/main/java/path/to/project/client/ProjectEntryPoint.java'
[INFO] [ERROR] Line 79: No source code is available for type path.to.project.client.Dagger_ProjectComponent; did you forget to inherit a required module?
[INFO] [ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly



the problem seems related with the fact that GWT compilation isn't including the generated sources, as target/generated-sources/annotations/ isn't included in the classpath of the GWT compiler. The project class paths that are included in GWT compile command are:
/path/to/project/target/tmp/WEB-INF/classes
/path/to/project/target/src/main/java



Which version of the maven-compiler-plugin are you using? m-compiler-p didn't add the generated-sources to the compileSourceRoots until version 3.2: https://jira.codehaus.org/browse/MCOMPILER-157, so you had to use the build-helper-maven-plugin to add it.

Vasco Andrade Silva

unread,
Feb 24, 2015, 8:58:56 PM2/24/15
to google-we...@googlegroups.com
Thank you Thomas and Jens.

Thomas, I was using v. 2.3.2 for maven-compiler-plugin, upgraded to 3.2 and everything worked without build-helper-maven-plugin as you described - thanks!

Jens, I did as you said and everything worked! I Hope that the Dagger 2 team merge your PR asap - thank you!

I couldn't make Eclipse work as good as it should (still have issues #2 and #3) and I'm still relying on "Maven > Update Project" - I'll deep dive in https://www.eclipse.org/m2e/documentation/m2e-execution-not-covered.html and see if I can make any progress.

Roger Küng

unread,
Aug 18, 2015, 9:32:01 AM8/18/15
to Google Web Toolkit
Could you please be a little more precise? I didn't manage to install the fork, and since the pull request still hasn't been accepted it would be great if you could elaborate more on this.

Jens

unread,
Aug 18, 2015, 10:13:59 AM8/18/15
to Google Web Toolkit
Could you please be a little more precise? I didn't manage to install the fork, and since the pull request still hasn't been accepted it would be great if you could elaborate more on this.

Since my fork is already 6 month old and I haven't rebased my changes yet it is probably best if you just clone the google dagger repository. Once you have done that you can do

git fetch origin pull/119/head:gwt-integration

This should give you a new local branch named gwt-integration which contains the changes of my pull request. Now you can decide if you want to rebase that local branch on a release tag (e.g. dagger-2.0.1) or on the master branch. Finally you just build dagger using mvn package or mvn install.

-- J.

alucard

unread,
Sep 23, 2015, 12:18:58 AM9/23/15
to GWT Users
Are there any examples/tutorials on how to use Dagger2 with GWT. It would be very helpful. 

Jens

unread,
Sep 23, 2015, 8:07:10 AM9/23/15
to GWT Users

Are there any examples/tutorials on how to use Dagger2 with GWT. It would be very helpful. 

You need to inherit the Dagger GWT module and then you can use any Dagger 2 tutorial as there is no difference in usage with GWT.

-- J. 
Reply all
Reply to author
Forward
0 new messages