Search code examples
gwt

GWT module xml source element to specify single class


I have a GWT application (FooGwtApp) and a library module (FooLib) used as a dependency in FooGwtApp. The package structure of FooLib looks like this:

packageFoo.ImportantClass
packageFoo.UnimportantClass
packageBar.OtherClass

I want ImportantClass (and only ImportantClass) to be compiled to JS by the GWT compiler. Moving ImportantClass to another package is not an option.

I created ImportantClass.gwt.xml within packageFoo with the following content:

<module>
    <inherits name="com.google.gwt.user.User"/>
    <source path="" includes="**/ImportantClass*"/>
</module>

Next I put an inherited reference to the ImportantClass module definition in FooGwtApp.gwt.xml (this seems to work: the IDE recognizes it, and is able to parse the reference to ImportantClass.gwt.xml).

Now If I put references to ImportantClass into FooGwtApp's client code, the GWT compiler fails, because it does not find ImportantClass on the source path:

No source code is available for type packageFoo.ImportantClass; did you forget to inherit a required module?

I likely messed up sommething in the source path / includes attribute in ImportantClass.gwt.xml - either defining the current package as root package with path="" is not a valid notation or something's wrong with the includes attribute. Or both. Or neither.

Can you give me a clue about where it all went wrong?


Solution

  • It turns out the problem was not in ImportantClass.gwt.xml, but in other Maven related stuff:

    • ImportantClass.gwt.xml should be placed under src/main/resources/packageFoo, not src/main/java/packageFoo, otherwise it won't be packaged into the binary jar.

    • GWT compiler compiles from Java source to Javascript source. This means we don't just need ImportantClass.class in FooLib.jar, but also its source. Best solution for this is to use maven-source-plugin in FooLib's pom.xml and also to import the FooLib dependency into FooGwtApp with sources classifier.

    On the latter topic, see the following SO answers:

    Maven: Distribute source code with with jar-with-dependencies

    How to properly include Java sources in Maven?

    After fixing the above problems, the source path declaration present in the question works.