GWT - adding external java classes to client project
I have a GWT project. Client code is located in the "client" di开发者_如何学JAVAr. I want to attach an external java classes (mainly plain POJO DTO classes) that are in external directory. How to configure the gwt.xml file?
I get errors of this kind:
[ERROR] Errors in 'file:/C:/development/projects/CodeSpaces/LocateMe/LocateMeWeb/src/com/dominolog/locateme/client/LocateMeWeb.java' [ERROR] Line 56: No source code is available for type com.dominolog.locateme.model.dto.LocationInfo; did you forget to inherit a required module?
If you have the java source files you just need to add the directory to you .gwt.xml file. For instance if you had a subdirectory called shared you would add the following line:
<source path='shared'/>
The folder called shared would have to be one level under your main package. So if you project .gwt.xml file is at com.yourdomain.project the shared folder.package would be com.yourdomain.project.shared. Refer to the Source Path section at http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideModules
If you dont have the source and only have the classes you have to import a module as Hilbrand has stated.
Here is another solution that works http://www.gordonizer.com/2012/01/referencing-third-party-library-source.html .
I short (just in case this link also gets invalid some day):
- Suppose the external package containing the classes you want to add is
com.foo.bar.bat
. - In your project, you create package
com.foo.bar
(without the subpackagebat
). In your new package you create a new GWT module file
Foo.gwt.xml
with content like<module>
<source path="bat" />
</module>
(I have ommitted the XML header for sake of readability)
Finally add this new module to your main GWT module using the
inherit
tag:<inherits name="com.foo.bar.Foo"/>
Any Java classes to be used by GWT must have a module file and conform to a package structure that includes a sub package. See this stackoverflow answer for more details: Adding Java packages to GWT. In this case a module file (e.g. model.gwt.xml
) could be created in the directory com.dominolog.locateme.model
that contains the following content:
<module>
<source path="dto" />
</module>
Add a reference to this module file in your main module file and GWT will then take all classes in the com.dominolog.locateme.model.dto
package.
2 notes:
GWT will look at all classes in the directory (and subdirectories)
The classes in the package must be present in source files and may not contain any references to other libraries not parseable by GWT (This might be a limitation when in the dto classes annotations are used that refer to specific database usages).
Update: Rewritten answer to be more specific.
I'll try to be more specific. My workspace structure is: Workspace
- LocateMeModel
- LocateMeServer
- LocateMeMobile
- LocateMeWeb
The LocateMeWeb is a GWT application with standard project layout. In LocateMeModel I have Java classes that define dto (data transfer objects) that are common for LocateMeWeb, LocateMeMobile and LocateMeServer. Therefore, I need that GWT takes files from this directory (LocateMeModel) and compiles it as a client code. Is it possible at all? From what I see GWT allows only that the code MUST BE under client directory with GWT project.
精彩评论