19

虽然 J2EE 模块参考功能允许您创建通用 Java 库项目,但我找不到为 Web 内容执行此操作的简洁方法。

我有通用的 JSP、CSS 文件、JavaScript 库甚至是描述符片段,我想在多个动态 Web 项目中使用它们,因此这些人工制品只在一个地方编辑,但将被导出到每个动态 WebProject WAR文件。

令我惊讶的是,如果不编写自己的脚本并挂钩到导出过程中,我就无法找到一种方法来提高网络空间的可重用性。

有没有办法做到这一点?谢谢。马特。

4

4 回答 4

13

I've been wrestling with the same problem for a long time, and finally stumbled onto a good solution: "Linked Folders". This is an Eclipse feature that works similarly to symlinks -- it allows you to map a single physical folder into multiple projects. The nice thing about this solution is that it doesn't require any special build steps, so Eclipse can automatically deploy changes to your local Tomcat server for testing.

My configuration is as follows: I have an Eclipse project named "SharedContent". Inside the WebContent directory, is a subdirectory "shareRoot". All reusable files -- .jsp, .css, etc. -- are located somewhere under SharedContent/WebContent/shareRoot. This is a Dynamic Web Project so that all of the appropriate editors are enabled, but I never actually build or deploy it.

In my other projects, I add a Linked Folder pointing to shareRoot. In Eclipse 3.6, the steps are:

  1. In the Package Explorer, right-click on the WebContent directory of the project that needs to include the reusable files.

  2. Select New -> Folder.

  3. Click the "Advanced>>>" button.

  4. Select the "Link to alternate location (Linked Folder)" radio button.

  5. In the textbox just under this, click Browse.

  6. In your source tree, navigate to SharedContent/WebContent/shareRoot. Then click OK/Open/Finish buttons until all the dialogs go away.

The shared content is now mapped into your project. Annoyingly, you have to include "shareRoot/" in the URL when referencing these files, but you can work around this using your favorite URL rewriting filter.

You can use a similar trick in the Java source tree to map Java files into multiple projects. I've started doing that, instead of building the shared code into a .jar file, because it avoids the need to rebuild the .jar every time you want to test a change in your local Tomcat server.

于 2010-10-27T22:53:47.860 回答
3

我相信编写自己的脚本(例如,使用 ant 的自定义构建步骤,在 eclipse 中易于配置)是更实用的解决方案之一。

您还可以创建多个 webapps 并引用它们各自的资源——这对于 css、图像等来说很容易,而不是 jsp 需要访问您的可用代码。

我可以想象一些使用tomcats crossContext="true" 选项作为上下文(例如webapplication)的技巧,使一个应用程序能够访问另一个应用程序中的类,但还没有尝试过。

对不起 - 我相信这不是你想听到的答案......

于 2008-12-14T18:02:12.593 回答
3

如果您使用的是 Subversion,您可以使用svn:externals来引用两个项目中的公共文件。然后您可以在一个地方编辑公共文件,如果您这样做,svn up所有文件都会被同步。

于 2008-12-14T18:13:06.813 回答
-2

哈哈,我给你迟到的惊喜

我是中国人,我的英语很差,但我可以帮助你。

Olaf Kock 是对的,但他的回答并不完美。

这是完美的答案。你必须参考以下网址,它可以帮助你。

点击这里

在 url 页面中,属性内容框的示例不准确,请关注我:

如果需要共享文件:

a.jsp  svn://myhome.com/svn/myproject/trunk/a.jsp

如果需要共享文件夹:

xml  svn://myhome.com/svn/myproject/trunk/xml
于 2013-03-14T08:49:38.333 回答