Darrell Brogdon
Darrell Brogdon

Reputation: 6973

How to change HTML/JS/CSS without restarting Tomcat?

Is it possible to configure Tomcat such that it looks in a separate location for my 'webapp' directory while in development?

I have my git source in one directory and tomcat installed in another. I don't want to have to make changes in two places, or manually copy files over, or rebuild/deploy every time I make a simple change to CSS or Javascript.


EDIT - 2/21/2013

Unfortunately none of the suggestions worked and I suspect it may be because I didn't provide enough information about how I have things laid out.

I have Tomcat installed in a directory of my home directory (I'm on a Mac) called "Development".

/Users/dbrogdon/Development/apache-tomcat-7.0.35

I have my git source next to that.

/Users/dbrogdon/Development/myproject

In the myproject directory, my actual web files are located in:

/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp

When I compile I put the appname.war into

/Users/dbrogdon/Development/apache-tomcat-7.0.35/webapps

Based on that updated information, what should I be providing as my docBase in either my conf/server.xml or conf/Catalina/localhost/appname.xml?

Upvotes: 18

Views: 21103

Answers (7)

rogerdpack
rogerdpack

Reputation: 66711

If I understand the question right, you just want tomcat's webapps/MY_WEBAPP to point to inside where you checked out your git repository, so you can modify them "once". Assuming you're only editing static resources, I don't think tomcat caches them so you may be able to get away with creating a symlink from tomcat's webapps/ to your git folder location. Or symlink from webapps/x/y back into your git equivalent folder (or even specific files).

I've also heard rumor that tomcat can "hotswap" JSP classes and static, so in case it's relevant: how to enable hot deploy in tomcat

Upvotes: 1

cowls
cowls

Reputation: 24334

You can use a local Apache server, and rewrite the HTML/CSS/JS/ files to be picked up from a separate location.

See this question for an example how to rewrite JS files: HOWTO: rewrite requests for images, css and js to different folders for each application?

Now you can configure Apache to go to Tomcat for the tomcat URLs, but perform these rewrites in the apache config and css/js/html files can be picked up from a different folder entirely.

You might be able to use this Tomcat Filter: http://tuckey.org/urlrewrite/, though you would still need an apache web server to host the static files, you could use Tomcat to access the main tomcat URLs and add redirects to your apache instance for all the static files. This saves having to configure mod_jk or something with Apache.

Upvotes: 2

MickJ
MickJ

Reputation: 2197

Update your server.xml and modify the "host" for your application for two purposes:

1) To point to your git source directory for tomcat to start your app from. This would avoid having to make changes at two places or copying over changes.

Change Required: Update the "host" for your application and set the 'appBase' to your git source directory.

<Host name="localhost"  appBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp"
            unpackWARs="true" autoDeploy="true">

2) Add a separate location for serving your static content. This would help you to avoid restarting tomcat everytime you change your css/js

Change Required: Add contexts for serving your js and css

<Host name="localhost"  appBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp"
            unpackWARs="true" autoDeploy="true">
    <Context docBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp/js/" path="/js/*" />
    <Context docBase="/Users/dbrogdon/Development/myproject/application/appname/src/main/webapp/css/" path="/css/*" />
</Host>

Upvotes: 1

Manish Singh
Manish Singh

Reputation: 3499

Possible ways to do what you are looking forward to

  • Configuring Apache web server for serving static content and routing the dynamic content to Tomcat server. This is done using mod jk connector which proxies the JSP request to tomcat but by passes this proxy for serving static content like image,js etc

Done using in worker.properties

<Location *>
SetHandler jakarta-servlet
SetEnvIf REQUEST_URI \.(html|png|gif|jpg|css|js)$ no-jk
</Location>

This might not be advisable as it changes all your deployment architecture

  • The solution that Zenil has proposed configuring server.xml in your conf

The limitation will be that all the applications that is deployed in your tomcat will be served from the D:/newWebApps.This might not serve your purpose as you need only the application that is in your Source repository to be mapped with your tomcat.

  • I think the below will be more appropriate for you in the given scenario

Create a file applicationName.xml & define following

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="D:\newWebApps" path="/js/*" />

and put this file in your \conf\Catalina\localhost directory. The tomcat will pick this up automatically

Now you can access this by

http://servername:8080/applicationName/js/anything.js

Refer this http://tomcat.apache.org/tomcat-7.0-doc/config/context.html for more details on setting docBase outside tomcat default diretories

Upvotes: 3

Zenil
Zenil

Reputation: 1571

In your tomcat installation, under directory "conf", you will find a file server.xml. In that file search for the tag "Host". You will find something like

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

Change appbase to the parent folder of your web application directory.So say your application "myapp" directory is under say c:\git\source , put appBase as "c:\git\source"

Restart your tomcat. Now your tomcat will look inside "c:\git\source" for all the web applications it has to deploy instead of webapps

Link to tomcat configuration describing appbase usage http://tomcat.apache.org/tomcat-7.0-doc/config/host.html

The Application Base directory for this virtual host. This is the pathname of a directory that may contain web applications to be deployed on this virtual host. You may specify an absolute pathname, or a pathname that is relative to the $CATALINA_BASE directory. See Automatic Application Deployment for more information on automatic recognition and deployment of web applications. If not specified, the default of webapps will be used.

Upvotes: 3

Susanne Muris
Susanne Muris

Reputation: 276

Using a separate location should be possible by defining an additional context. See http://www.coderanch.com/t/474965/Tomcat/Deploying-application-webapps-tomcat and http://tomcat.apache.org/tomcat-6.0-doc/config/context.html.

Upvotes: 0

passion
passion

Reputation: 44

when the git sourcecode is opened in IDE, after adding the tomcat server, You can configure the 'Runtime Environment' of tomcat to refer to the one in your tomcat directory.

Upvotes: -2

Related Questions