13

What is the ideal way to configure Tomcat to serve directly from my project's directory inside of my workspace? (related)

I want my static web resources to be instantly available anytime I make a change to them without having to publish them out to a separate directory. It seems like the best way to accomplish this is to serve files directly rather than a published/deployed copy of the file.


Alternatively, I'm open to other options (different application servers or IDEs) that make developing Java web applications much quicker. Publishing and redeploying every small change is eating too much time.

I've tried JRebel and FileSync (and they both work to a certain degree) but they try and patch a problem that shouldn't exist.

9 Answers 9

9
+75

I've never really liked the WTP plugin that gets bundled with Eclipse.

In the past I've had a lot of success with the Sysdeo Tomcat plugin for Eclipse.

It uses the compiled classes that Eclipse builds for you, so when you make an interface-compatible change (like changing some stuff inside a method), this gets deployed immediately without requiring a restart. Changing method signatures or adding new methods within a class do require a restart, but since there's no lengthy WAR-building step, the whole build/deploy cycle is reduced anyway.

Additionally, the Sysdeo plugin uses the static assets from your workspace, so no need to copy or deploy these. Just make a change, refresh your browser, and you see the change right away.

Unfortunately it looks like development of the plugin ground to a halt a couple of years ago. The latest supported version of Eclipse, according to the matrix on their website, is 3.6. According to this page however, the plugin still works with Eclipse 4.2 (Juno).

Hope this helps. Using Sysdeo is really a much nicer experience than WTP!

1
  • It seems like a good option - the only thing that concerns me is the support going forward. Jul 8, 2013 at 14:54
5

I do not want to come off as promoting anything, but Netbeans IDE supports all of these out of the box, no plugins necessary, it also includes a pre configured tomcat;

It supports copying static resources on save, so there is no need to rebuild your whole application:

enter image description here

As for any java changes, if you are in debug mode there is an Apply Code Changes button, which will rebuild and deploy your changed files (assuming no structural changes were made)

or alternatively use JRebel (for deploying changed java files) which has excellent features

4
  • I'm fine with trying any setup but it sounds like "copy static resources on save" is similar to the eclipse/tomcat/wtp setup that I have right now where it "publishes automatically" (aka copies) to a separate directory. Normally it takes 5-10 seconds for it to recognize then copy (and a couple browser refreshes) which is the current position I am in right now. Do you notice any lag time between saving and seeing the changes in your browser? Jul 8, 2013 at 13:25
  • Not at all, I am currently using eclipse right now (being forced by my company), but have been used to netbeans for the last 5 years, I have tried everything imaginable to make eclipse behave the way I am used to (quick response times when saving) but could not get it right. Either way your best bet is to check it out for yourself, I have never waited more than 2-3 seconds for a save to reflect with netbeans. I also need to note that the project was large :)
    – epoch
    Jul 8, 2013 at 13:29
  • sorry to bump, but just wanted to hear if you have had success with this?
    – epoch
    Jul 17, 2013 at 15:29
  • No problem, I actually didn't try it yet. I'm sort of bummed there wasn't a better way to serve an application straight from the source directory (just like you would in PHP). I've kind of accepted that Java applications have to be deployed to a separate directory. Jul 17, 2013 at 17:00
5

I am pretty sure that Maven can do this kind of stuff. Maybe there are even Archetypes for Tomcat which have this kind of behaviour already configured.

I am using a Maven Archetype together with JBoss AS. When I change my .css or .xhtml file or any other static resources and save it in Eclipse, the resources are immediatally synchronzied with my deployment.

Edit:

Here is a quick tutorial for my solution:

What I am using:

  • JBoss AS 7.1.1
  • Eclipse Juno for Java EE developers

Older or newer versions should also do the work.

  1. Install JBoss Tools Plug-In over the Eclipse Marketplace in Eclipse IDE
    This will install several Plug-Ins to your IDE, like the JBoss AS Server Connector or Maven.
  2. Add JBoss Repositories to your Maven configuration
    Window -> Preferences -> Maven -> User Settings -> open file
    Add the repositories from JBoss Maven Getting started
  3. Add a JBoss Server Runtime
    Window -> Preferences -> Servers -> Runtime Environments
    Follow the Wizard, pretty standard
  4. Add a Serverinstance from the Eclipse Servers Tab
  5. Create a new Maven Project
    Take care that the checkbox Create a simple project(skip archetype) is NOT checked
    Choose the Archetype jboss-javaee6-webapp-blank-archetype 7.1.3.CR7

Archetype

Now your Project is ready to go:)

2
  • Sounds promising, do you have an example? Jul 8, 2013 at 13:27
  • This still copies the changed .css or .xhtml to a deployment directory on save, correct? Jul 8, 2013 at 17:42
2

It's unclear if you are using the Eclipse WTP approach with Tomcat or some custom deployment strategy.

I switched away from running Tomcat in Eclipse because of unreliable re-publishing of changed JARs. I'm now using Jetty via the Run Jetty Run Eclipse plug-in (available in the Eclipse Marketplace). This appears to run from the workspace and does not do the republish-to-a-deployment-directory approach that Eclipse uses with Tomcat.

You don't get dynamic code reload with this option, but you can add that feature back with JRebel.

Going further left-field, switch to a JVM language/framework that supports seeing your changes instantly without re-publishing/restarting such as:

  • Play 2 (Scala and Java)
  • Grails (Groovy)
  • Vert.X (various JVM languages).
2
  • You're right - I am using an Eclipse WTP approach w/ Tomcat and it's just not working out (we can't be friend anymore). Not having dynamic code reload isn't a big concern. I'm much more interested in having to not publish non-compiled files constantly. This sounds like a good option. Do you know if the app will still be portable between Jetty/Tomcat (and any other app server)? Jul 3, 2013 at 13:10
  • Yes - I develop with Jetty, but we deploy to Tomcat (it's still a WAR file we build/deploy).
    – SteveD
    Jul 3, 2013 at 14:09
1

I suggest using sbt together with its web plugin.

The key feature of sbt is the continuous integration mode: This basically means, sbt fires a command, if a file changes.

So normal sbt commands (related with the web plugin) are

container:start
container:stop
container:reload / ... container:reload

but using it in the continuous integration mode (a command starting with ~)

~;container:start; container:reload /

it will automatically reload a web application when source code or any other files have changed.

So I just save servlets, Java sources, Scala sources whatever, and hit reload in the browser, and that really saves a lot of time.

This is the Grails feeling, but with Java as the main programming language.

Additional information

  • I have sbt running in a terminal (I am not aware if there is a plugin for Eclipse to start it from within Eclipse)
  • sbt can compile both Java and Scala projects out of the box.
  • The web plugin is using jetty (I am not aware if it's possible to embed tomcat as well)
  • There is no need to know Scala to configure sbt, but you should read the tutorial.
  • This solution is not tied to the IDE; so it works without an IDE as well (just from the command line).
1
  • Downvoter, please add a comment. The question was explicitly open to "other options (application servers)".
    – Beryllium
    Jul 7, 2013 at 14:16
1

This depends on whether you object to taking some action, like hitting Publish, or if you object to the copy happening even if it's transparent. Static content like CSS, HTML or JavaScript should get automatically deployed (i.e. copied) when you save, assuming you have "Automatically publish when resources change" under "Publishing" selected in Tomcat's server configuration. You should refresh in your browser and see the changes, barring some browser caching.

If you truly want Tomcat to look directly at your project's static files, then run Tomcat with nothing deployed (unless you have Java classes to deploy in the same project) and edit the server.xml file under Servers > Apache Tomcat v7.0 at localhost-config and add some Context elements under Host like this:

<Context docBase="C:\path\workspace\project\WebContent\css" path="/project/css" reloadable="true" />
0

By default it shares only the apps located in $CATALINA_HOME/webapps. You can change it in the $CATALINA_BASE/conf/server.xml in the <Host />.

Check the documentation for attribute appBase.

Hope it helped.

0

Use grizzly webserver. It's completely implemented in java, hence you have full plattform independency and you can start it directly from your workspace without configuring any external programs. It's easy to deploy resources statically. You just have to derive javax.ws.rs.core.Application and add your resources as in the example.

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

/**
 * Application class that contains resources for the RESTful web service.
 *
 */
public class MyApplication extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(com.rest.test.SomeClass.class);
        return s;
    }
}

This is needed to configure the servlet adapter. It's also possible to add resources dynamically. But I can't tell you how fast the updating of the resources is with the dynamic approach. Anyway there is enough documentation available online.

private static GrizzlyWebServer getServer(int port,
    String webResourcesPath, String contextPath) {
    GrizzlyWebServer gws = new GrizzlyWebServer(port, webResourcesPath);

    ServletAdapter sa = new ServletAdapter();

    /* here they are added statically */
    sa.addInitParameter("javax.ws.rs.Application", "com.rest.MyApplication");


    sa.setContextPath(contextPath);
    sa.setServletInstance(new ServletContainer());
    sa.setProperty("load-on-startup", 1);
    gws.addGrizzlyAdapter(sa, new String[] { contextPath });

    return gws;

}

-1

Normally webapplications are hosted under webapps directory, but its also possible to configure an external directory as a webapplication host directory in tomcat. You can simply set your eclipse workspace project output directory as an application base. This can be done either by using Tomcat's manager application to deploy an application from an external directory or by simply editing server.xml (which is under conf directory) to define your application like below:

....

<Context docBase="D:\your\eclipse\workspace\project\WebContent" path="/projectbaseurl"   reloadable="true"/>

</Host>
</Engine>
</Service>
</Server>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.