18

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?

Darrell Brogdon
  • 6,843
  • 9
  • 47
  • 62
  • that should be the default configurations of tomcat. Please look at this thread http://stackoverflow.com/questions/3942655/getting-tomcat-to-reload-a-web-app-with-a-static-context – carlosavoy Feb 21 '13 at 13:20

7 Answers7

3

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.

Zenil
  • 1,491
  • 3
  • 12
  • 21
  • 1
    @darrell not sure if evaluated the above option ? If all you need is for the tomcat in your development environment to point to a folder other than the default "webapps", the above solution should be good enough.. – Zenil Feb 21 '13 at 02:30
  • OP wants to do this _without_ restarting tomcat. – Raad Feb 21 '13 at 15:20
  • @Raad This is a one time setup. Basically telling tomcat to look in to his development environment directory instead of default "webapps" directory. After that no restart required , no deploy or copy required.. – Zenil Feb 21 '13 at 18:21
3

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

Manish Singh
  • 3,463
  • 22
  • 21
2

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.

Community
  • 1
  • 1
cowls
  • 24,013
  • 8
  • 48
  • 78
  • 1
    This is one solution to the problem, Apache is a web server, Tomcat a Java web container. You would not use Tomcat as a stand alone web server in a production environment anyway, so must developers use Apache + Tomcat. A downvote means the answer is NOT useful. – cowls Feb 21 '13 at 21:22
  • My apologies. I think I misunderstood the purpose of Tomcat. – Darrell Brogdon Feb 21 '13 at 21:57
1

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>
MickJ
  • 2,127
  • 3
  • 20
  • 34
1

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

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
0

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.

Susanne Muris
  • 276
  • 2
  • 10
-2

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.

passion
  • 44
  • 1
  • But what if I don't want to have to run an IDE? I'm a UI developer and won't be doing any Java coding so I'd prefer to use TextMate to edit the HTML/CSS/JS. – Darrell Brogdon Feb 12 '13 at 03:43