eclipse GWT - 将外部 java 类添加到客户端项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2341384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 14:27:36  来源:igfitidea点击:

GWT - adding external java classes to client project

javaxmleclipsegwt

提问by cubesoft

I have a GWT project. Client code is located in the "client" dir. 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?

我有一个 GWT 项目。客户端代码位于“客户端”目录中。我想附加一个外部目录中的外部 java 类(主要是普通的 POJO DTO 类)。如何配置gwt.xml文件?

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?

[ERROR] '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; 您是否忘记继承所需的模块?

回答by Carnell

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:

如果您有 java 源文件,您只需要将目录添加到您的 .gwt.xml 文件中。例如,如果您有一个名为 shared 的子目录,您将添加以下行:

<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 Pathsection at http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideModules

名为 shared 的文件夹必须位于主包下的一级。因此,如果您的项目 .gwt.xml 文件位于 com.yourdomain.project,则共享 folder.package 将是 com.yourdomain.project.shared。请参阅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.

如果您没有源代码并且只有类,则必须按照 Hilbrand 的说明导入模块。

回答by rainer198

Here is another solution that works http://www.gordonizer.com/2012/01/referencing-third-party-library-source.html.

这是另一个有效的解决方案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 subpackage bat).
  • In your new package you create a new GWT module file Foo.gwt.xmlwith 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 inherittag:

    <inherits name="com.foo.bar.Foo"/>

  • 假设包含要添加的类的外部包是com.foo.bar.bat.
  • 在您的项目中,您创建包com.foo.bar(没有 subpackage bat)。
  • 在您的新包中,您创建一个新的 GWT 模块文件Foo.gwt.xml,其内容如下

    <module><source path="bat" /></module>

    (为了可读性,我省略了 XML 标头)

  • 最后使用以下inherit标签将此新模块添加到您的主 GWT 模块中:

    <inherits name="com.foo.bar.Foo"/>

回答by Hilbrand Bouwkamp

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.modelthat contains the following content:

GWT 使用的任何 Java 类都必须有一个模块文件并符合包含子包的包结构。有关更多详细信息,请参阅此 stackoverflow 答案:将 Java 包添加到 GWT。在这种情况下,model.gwt.xml可以在com.dominolog.locateme.model包含以下内容的目录中创建一个模块文件(例如):

<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.dtopackage.

在主模块文件中添加对此模块文件的引用,然后 GWT 将获取com.dominolog.locateme.model.dto包中的所有类。

2 notes:

2注意事项:

  1. GWT will look at all classes in the directory (and subdirectories)

  2. 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).

  1. GWT 将查看目录(和子目录)中的所有类

  2. 包中的类必须存在于源文件中,并且不能包含对 GWT 无法解析的其他库的任何引用(当在 dto 类中使用引用特定数据库用法的注释时,这可能是一个限制)。

Update:Rewritten answer to be more specific.

更新:重写答案更具体。

回答by cubesoft

I'll try to be more specific. My workspace structure is: Workspace

我会尽量说得更具体一些。我的工作区结构是:工作区

  1. LocateMeModel
  2. LocateMeServer
  3. LocateMeMobile
  4. LocateMeWeb
  1. 定位我的模型
  2. 定位服务器
  3. 定位我移动
  4. 定位我的网页

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.

LocateMeWeb 是具有标准项目布局的 GWT 应用程序。在 LocateMeModel 中,我有 Java 类定义了 LocateMeWeb、LocateMeMobile 和 LocateMeServer 常用的 dto(数据传输对象)。因此,我需要 GWT 从此目录 (LocateMeModel) 中获取文件并将其编译为客户端代码。有可能吗?从我所见,GWT 只允许代码必须位于 GWT 项目的客户端目录下。