Tuesday, December 4, 2007

Ant's <pathconvert> task

I needed my webapp-specific log file to be placed into the Tomcat/Logs directory. The RollingFileAppender's File property in the log4j.properties file was configured to be replaced at build time with the path to the logfile (in this case Tomcat/Logs/xxx.log). I didn't find a way to reference environment variables directly from log4j.properties

The issue I ran into was that when I tried to reference the CATALINA_HOME environment variable the path to Tomcat was returned with backslashes:

<filterset id="variables.to.replace.when.configuring.tomcat">
<filter token="LOG_FILE" value="${env.CATALINA_HOME}/logs/${webapp.name}.log" />

So what I would get is C:\Tomcat/logs/xxx.log The problem with this was that once the build script would run, the log4j.properties RollingFileAppender.File ended up having C:Tomcat/logs/xxx.log which for obvious reasons did not work.

So what I need was a way to replace the backslashes in ${env.CATALINA_HOME} with the forward slashes.

The solution is to use Ant's pathconvert tasks:

<pathconvert dirsep="/" property="log">
<path path="${env.CATALINA_HOME}/logs/${webapp.name}.log"/>
<filterset id="variables.to.replace.when.configuring.tomcat">
<filter token="LOG_FILE" value="${log}" />

No comments: