views:

637

answers:

3

While the J2EE module reference feature allows your to create common Java library projects, I can't find a neat way to do this for web content.

I have common JSPs, CSS files, JavaScript libraries and even descriptor fragments that I would like to use across a number of Dynamic Web Projects, so that these artefacts are edited i only one place, but will be exported into each of the Dynamic WebProject WAR files.

I am surprised that I can't find a way to promote reusability in the web space without writing my own scripts and hooking into the export process.

Is there a way to do this? Thanks. Matt.

+2  A: 

I believe writing your own scripts (e.g. custom build steps using ant, easily configurable in eclipse) is among the more practical solutions.

You could also create multiple webapps and reference their respective resources - this would be easy with css, images and the like, not so much with jsp that need to access your code being available.

I can imagine some tricks with tomcats crossContext="true" option for the context (e.g. webapplication), enabling one application to access classes in an other application, but haven't tried them yet.

Sorry - I believe that's not the answer you'd like to hear...

Olaf
So did I, but it's not comfortable at all.
Manrico Corazzi
+1  A: 

If you're using Subversion, you could use svn:externals to reference the common files in both projects. Then you can edit the common files in one place, and if you do a svn up all will be synchronized.

Paul Fisher
A: 

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.

Steve