views:

70

answers:

2

Some of our CSS files contain parameters that can vary based on the deployment location (dev, QA, prod). For example:

 background: url(#DOJO_PATH#/dijit/themes...)

to avoid hardcoding a path to a particular CDN or locally-hosted Dojo installation.

These values are textually substituted with the real values by a deployment script, when it copies the contents of the webapp into the Tomcat webapps directory. That way the same deployment archive file (WAR + TAR file containing other configuration) can be deployed to dev, QA, and prod, with the varying parameters provided by environment-specific configuration files.

However, I'd like to make the contents of the WAR (including the templatized CSS files) independent of this in-house deployment script. Since we don't really have control over the deployment script, all I can think to do is configure Tomcat with #DOJO_PATH# etc. as environment variables in the application's context.xml, and use Tomcat to insert those parameters into the CSS at runtime.

I could make the CSS files into generated JSPs, but it seems a little ugly to me. Moreover, the substitution only needs to be done once per application deployment, so repeatedly dynamically generating the stylesheets using JSP will be rather wasteful.

Does anyone have any alternative ideas or tools to use for this? We're committed to Tomcat and to substituting these parameters at deployment or at runtime (that is, not at build time).

+1  A: 

What you're doing at the minute seems like the best solution to me.

You could easily write these files to be served by a Servlet and dynamically replace the contents of them using some view rendering technology such as Freemarker (or even a custom written templating system to replace the keywords, but there are costs associated with doing so.

Tomcat can serve these resources much more efficiently if they are truly static at runtime. Also if you front your Tomcat server with Apache then you can have Apache serve the static content without ever hitting your Tomcat server, thus keeping your JVM thread pool smaller and less highly contended.

Geoff
It occurs to me that it would be nice to have a deployment hook in Tomcat or JEE Servlet spec, to avoid needing to script this kind of parameterization manually.
Steven Huwig
What we normally end up doing is adding the separate files to the web archive during the build phase using a maven plugin. That way we just add -P <profile> to the build command, and it builds with the right files. We wouldn't normally consider doing that for CSS though, as the paths in the CSS should be the same relative path on whichever environment we deploy our apps to (as the things referenced from the CSS are part of the web application itself). Is your use case for having this url change because you want to put dojo on a CDN?
Geoff
More like we want to upgrade versions or switch CDNs without touching all of the CSS code that refers to stuff from Dojo themes. There are other parameterized values besides the Dojo URL, though thankfully few in the static files.We can't really use Ant or Maven substitution for the actual build, since the site files are deployed by another group on the QA and production servers, using their custom script. They deploy the same tarball in both QA and prod. We do use Maven/Ant to substitute on our local workstations, but it's annoying to have two ways to do things.
Steven Huwig
A: 

You could also have a single environment variable that pointed at an environment-specific configuration file, and have some startup scripts plug it together for you at that point. That way, the command line doesn't get out of control.

Dean J