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
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;
}
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.
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>
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
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.jetty
(I am not aware if it's possible to embed tomcat
as well)sbt
, but you should read the tutorial.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:
Older or newer versions should also do the work.
Now your Project is ready to go:)
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!