Eclipse 3.4 GWT 1.6 项目 - 如何从其他项目引用源代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/951926/
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
Eclipse 3.4 GWT 1.6 project - how to reference source from other projects?
提问by Boden
I've got a GWT 1.6 project in Eclipse 3.4 and am trying to reference source from another (non-GWT) project in my workspace. I've added the project to my build path, but I can't run the application in hosted mode. When I do, I get a "no source code is available" error.
我在 Eclipse 3.4 中有一个 GWT 1.6 项目,我正在尝试从我的工作区中的另一个(非 GWT)项目中引用源代码。我已将该项目添加到我的构建路径,但我无法在托管模式下运行该应用程序。当我这样做时,我收到“没有可用的源代码”错误。
I've done some searching and reading and have tried a few things that others have suggested (including creating a jar from the dependent project and linking to it), but frankly nothing has worked.
我已经做了一些搜索和阅读,并尝试了其他人建议的一些事情(包括从依赖项目创建一个 jar 并链接到它),但坦率地说没有任何效果。
If you're actually doing this, could you please help me out with a simple step-by-step setup? I'd really appreciate it, thanks!
如果你真的这样做,你能帮我做一个简单的分步设置吗?我真的很感激,谢谢!
采纳答案by Upgradingdave
I have 2 Eclipse Projects. One is gwt project and one is not. Here's the directory structure that works for me:
我有 2 个 Eclipse 项目。一个是gwt项目,一个不是。这是对我有用的目录结构:
workspace
-- gwt-project
-- src/main/java
-- com.gwt.GwtProjectModule
-- GwtProjectModule.gwt.xml
-- non-gwt-project
-- src/main/java
-- com.nongwt.package.that.contains.source.you.need
-- nongwt.gwt.xml
-- com.nongwt.package.that.contains.source.you.need.client
nongwt.gwt.xml tells gwt to look inside "client" package, here's what it looks like:
nongwt.gwt.xml 告诉 gwt 查看“client”包的内部,它是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 1.7.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/1.7.0/distro-source/core/src/gwt-module.dtd">
<module rename-to='nongwt'>
<inherits name='com.google.gwt.user.User' />
<source path="client" />
</module>
GwtProjectModule.gwt.xml can then inherit source code from nongwt. Here's the relevant line from inside GwtProjectModule.gwt.xml:
然后 GwtProjectModule.gwt.xml 可以从 nongwt 继承源代码。这是 GwtProjectModule.gwt.xml 中的相关行:
<inherits name="com.nongwt.package.that.contains.source.you.need.nongwt" />
Make sure you include non-gwt-project inside gwt-project's classpath in eclipse. It's the normal drill; right click on gwt-project, choose properties, choose "Java Build Path", click "Projects" tab, and "non-gwt-project"
确保在 Eclipse 中将非 gwt-project 包含在 gwt-project 的类路径中。这是正常的练习;右键单击gwt-project,选择属性,选择“Java Build Path”,单击“Projects”选项卡,然后选择“non-gwt-project”
Or, instead of including non-gwt-project in gwt-project's classpath as a project reference, you can also jar the contents of non-gwt--project, ensure that you include the source in the jar, and then include the jar in gwt-project's classpath.
或者,除了在gwt-project的classpath中包含non-gwt-project作为项目引用之外,还可以将non-gwt--project的内容进行jar包,确保jar中包含源码,然后将jar包含在gwt-project 的类路径。
Good Luck!
祝你好运!
回答by Fabrice Dewasmes
I know this post is quite old but as I spent quite a lot of time on this issue and finally found the way to do it, I thought I might share the answer :
我知道这篇文章已经很老了,但由于我在这个问题上花了很多时间,终于找到了解决方法,我想我可以分享答案:
once you've created your launch configuration, open it run>run configurations... go to classpath tab and select user entries. add advanced>folder and select the source folder of your other module project. If as me you've separated the module conf file in a src/main/resources folder you have to add it as well.
创建启动配置后,打开它运行>运行配置...转到类路径选项卡并选择用户条目。添加高级>文件夹并选择其他模块项目的源文件夹。如果像我一样将模块 conf 文件分隔在 src/main/resources 文件夹中,则还必须添加它。
should work.
应该管用。
回答by Steve Armstrong
The client-side code in your GWT project (the classes under the client package) can't reference any classes that aren't in a GWT module.
GWT 项目中的客户端代码(客户端包下的类)不能引用不在 GWT 模块中的任何类。
If you've got code in another project that you want to reference from client code in your GWT project, you need to:
如果您希望从 GWT 项目中的客户端代码引用另一个项目中的代码,则需要:
- Make sure it's all "GWT safe", which means it doesn't reference any JRE classes that aren't emulated by GWT (http://code.google.com/webtoolkit/doc/1.6/RefJreEmulation.html), or reference any classes that reference JRE classes not emulated
- Make sure all referenced classes are within a GWT module. This means putting a MyOtherProject.gwt.xml file in your other project, and all the referenced classes must be under a client subpackage
- Make your GWT project inherit from the other project. So add the following to your GWT project's gwt.xml module file:
- 确保它都是“GWT 安全的”,这意味着它不引用任何未被 GWT 模拟的 JRE 类(http://code.google.com/webtoolkit/doc/1.6/RefJreEmulation.html),或引用任何引用未模拟的 JRE 类的类
- 确保所有引用的类都在 GWT 模块中。这意味着在你的另一个项目中放置一个 MyOtherProject.gwt.xml 文件,并且所有引用的类都必须在一个客户端子包下
- 使您的 GWT 项目从其他项目继承。因此,将以下内容添加到 GWT 项目的 gwt.xml 模块文件中:
<inherits name='com.yourCompany.otherProject.MyOtherProject' />
<继承名称='com.yourCompany.otherProject.MyOtherProject' />
回答by Steve Armstrong
Boden: add the following to your module file
Boden:将以下内容添加到您的模块文件中
<source path=""></source>
in addition to your other sources, eg:
除了您的其他来源,例如:
<source path=""></source>
<source path="com.acme.otherpackage"></source>
then the compiler won't complain.
那么编译器就不会抱怨了。
Atleast that's how I solved it. Not sure if using path="" allows inclusion of everything, I'm assuming it's the default value when no sources are specified.
至少我是这样解决的。不确定使用 path="" 是否允许包含所有内容,我假设它是未指定来源时的默认值。
回答by ouba64
This is a fantastic solution of your problem proposed by Sven Buschbeck (must a norwegian, lol!), worked for me!
这是 Sven Buschbeck(必须是挪威人,哈哈!)提出的问题的绝佳解决方案,对我有用!
When work-ing on sev-eral large scale projects (in Eclipse), it's con-ve-nient and of course more effi-cient to share and reuse code via com-mon libraries. While those are in an early stage they need to be changed a lot and there-fore it's handy to link projects in instead of cre-at-ing new jars each time the library has been updated. Unfor-tu-nately, this stan-dard approach for Java devel-op-ment in Eclipse does not work that straight for-ward as with plain old Java projects, it requires three steps in total:
在处理多个大型项目时(在 Eclipse 中),通过公共库共享和重用代码很方便,当然也更有效。虽然这些都处于早期阶段,但它们需要进行大量更改,因此每次更新库时,链接项目而不是创建新 jar 很方便。不幸的是,这种在 Eclipse 中进行 Java 开发的标准方法不像普通的旧 Java 项目那样直接有效,它总共需要三个步骤:
- Link the library project to all rel-e-vant projects (“Project Pref-er-ences” -> “Java Build Path” -> “Projects” -> “Add…”)
- Then, add the client-side code of the library (by adding it as a mod-ule.) There-fore, edit the gwt.xml of your appli-ca-tion and add for exam-ple where Super-Lib is the file name of the gwt.xml file in you library project and before that is the pack-age it lies in.
Add the code to the project by link-ing a source folder. Unfor-tu-nately, this is required if you do not want to write an Ant script to com-pile your appli-ca-tion. (If you pre-fer Ant check this out) I don't like the idea of using such a script because if you for-get to run it each time you make changes, you will end up in confusion—let's go for the con-ve-nient, auto-matic way then.
Add a folder to your appli-ca-tion project; open the “advanced” sec-tion in the folder cre-ation dia-log, select “Link to alter-nate loca-tion” and pick the source folder (usu-ally “src”) of your library project. (Hint: if you work within a team using a ver-sion-ing sys-tem, you can cre-ate a vari-able for that folder and use this instead. This way, each of your col-leagues can put the library project in a dif-fer-ent folder and accom-mo-date for that by adjust-ing the vari-able :) )
Right click the folder, “Build Path” -> “Use as Source Folder”. Done. Sur-pris-ingly, the GWT plu-gin for Eclipse does not honor the project link-ing, thus all the ref-er-ences need to be made explicit or you will end up with lots of the fol-low-ing: ClassNotFoundException.
- 将库项目链接到所有相关项目(“项目偏好”->“Java 构建路径”->“项目”->“添加...”)
- 然后,添加库的客户端代码(通过将其添加为模块。)因此,编辑您的应用程序的 gwt.xml 并添加例如其中 Super-Lib 是库项目中 gwt.xml 文件的文件名,在此之前是它所在的包。
通过链接源文件夹将代码添加到项目中。不幸的是,如果您不想编写 Ant 脚本来编译您的应用程序,则这是必需的。(如果你更喜欢 Ant 检查这个)我不喜欢使用这样一个脚本的想法,因为如果你每次进行更改时都忘记运行它,你最终会感到困惑 - 让我们继续-ve-nient,然后是自动方式。
将文件夹添加到您的应用程序项目中;打开文件夹创建对话框中的“高级”部分,选择“链接到备用位置”并选择库项目的源文件夹(通常是“src”)。(提示:如果您在使用版本系统的团队中工作,您可以为该文件夹创建一个变量并使用它。这样,您的每个同事都可以将库放在项目在不同的文件夹中,并通过调整变量来适应它:))
右键单击文件夹,“构建路径”->“用作源文件夹”。完毕。令人惊讶的是,Eclipse 的 GWT 插件不支持项目链接,因此所有引用都需要明确,否则您将得到很多以下内容:类未找到异常。
回答by Carnell
GWT doesn't know about that other code because it is not in the client directory of your project. You need to add the source path for the other code to the .gwt.xml file. Just added it to the xml as follows
GWT 不知道其他代码,因为它不在您项目的客户端目录中。您需要将其他代码的源路径添加到 .gwt.xml 文件中。只需将其添加到xml中,如下所示
<source path="common" />
commonis the directory where the extra code is for this example.
common是此示例的额外代码所在的目录。
Check out the XML Element Referencesection of this doc
查看此文档的XML 元素参考部分
回答by Sid Datta
Edit: Found a eclipse fix. Run config > Classpath > Advanced > Add folder > otherproject/src .
编辑:找到了一个 eclipse 修复程序。运行 config > Classpath > Advanced > Add folder > otherproject/src 。
Reason: Eclipse adds the bin folders of exported projects. GWT needs the src folders.
原因:Eclipse添加了导出项目的bin文件夹。GWT 需要 src 文件夹。
Elaborating on my comment.
详细说明我的评论。
I am using gwt 2.8 on Eclipse Neon, Java 1.8.0_92. Referring to another project in eclipse fails to launch devmode, because it cannot find the source for referred files from the other project.
我在 Eclipse Neon、Java 1.8.0_92 上使用 gwt 2.8。在 Eclipse 中引用另一个项目无法启动 devmode,因为它找不到来自另一个项目的引用文件的源。
What worked:
什么工作:
Switched to 'ant devmode' completely. Made the following changes to build.xml:
完全切换到“ant devmode”。对 build.xml 进行了以下更改:
<target name="gwtc" ...>
<java ...>
<classpath>
<pathelement location="src"/>
<pathelement location="../otherproject/src"/><!-- Added -->
...
...
<target name="devmode" ...>
<java ...>
<classpath>
<pathelement location="src"/>
<pathelement location="../otherproject/src"/><!-- Added -->
Now do a production build with 'ant' or start devmode with 'ant devmode'.
现在使用“ant”进行生产构建或使用“ant devmode”启动devmode。
Other things I noticed
我注意到的其他事情
Using the method in the accepted answer, otherproject/src/foo.gwt.xml is picked up (complains if not available), and static values from classes are picked up. Nothing else is. Very weird.
I understand that the DevMode/gwtc executables pick up sources from their class path. So all that is needed is for eclipse to add referred projects to their class path. I was not able to achieve this but this seems possible. FIXED see top.
使用接受的答案中的方法,otherproject/src/foo.gwt.xml 被拾取(如果不可用,则抱怨),并拾取来自类的静态值。别的都不是。很奇怪。
我知道 DevMode/gwtc 可执行文件从它们的类路径中获取源代码。因此,Eclipse 所需要的只是将引用的项目添加到它们的类路径中。我无法实现这一目标,但这似乎是可能的。固定见顶部。
回答by Luke
In your gwt project, go to properties, Java build path, source, click "link source" and point to your non-gwt project source package that you wish to include.
在您的 gwt 项目中,转到属性、Java 构建路径、源代码,单击“链接源代码”并指向您希望包含的非 gwt 项目源代码包。