Home > Software design >  Inconsistencies between Apache Velocity output from production (ubuntu) and local (windows)
Inconsistencies between Apache Velocity output from production (ubuntu) and local (windows)

Time:05-19

I am using apache velocity to generate rtf output from a tomcat web application using user defined templates. The webapp's dependencies are managed by maven, with velocity 2.3 defined in the pom.

While modifying one of my templates I discovered I could use multiline comments (#* *#) to "indent" the template making it much more readable. I have a local version of my webapp running on windows for testing purposes which I continuously uploaded my template to as I made changes to make sure this wasn't breaking functionality, and it worked as expected. However, when I went to upload it to the production version the output had a huge number of new lines in it where I had added the comments. I've also checked encoding of the template file (it is as far as I can tell UTF-8 at all stages).

Minimal example (arg0 is undefined and therefore this always goes to the else case):

#if ($arg0 && $arg0.equals("top"))
#***##set ($IDAlignment = "top")
#else
#***##set ($IDAlignment = "left")
#end
$IDAlignment

The production version of my application generates the following:


left

The local version:

left

Removing the second "indenting" removes the superfluous new line in the output, the first has no effect either way (I assume because velocity is never reaching that branch). Likewise, adding an additional comment to the end of the line removes the extra new line (#***##set ($IDAlignment = "left")##).

As stated my application's dependencies are controlled by maven so they should be running the same version of velocity, however further outside that the environments differ - e.g. the tomcat versions don't match exactly nor do the java environments. Could this be making a difference? I'm confused and a little concerned how any of this could change how velocity is reading my templates unless there is something huge that I have missed.

Edit: I updated tomcat to 9.0.62 on both versions, new lines are still happening on "production" (now a testing clone of the original container) but not on local. A coworker running his local version out of the linux filesystem side of WSL also does not have the extra new line issue.

Edit 2: I cleared the server's maven cache which also had no effect.

The newline characters are behaving slightly differently than I originally thought, however I don't think they are the problem. The template file has \r\n line endings in it both locally and on the server, and the output from the server reflects this. If I do change it to have only \n it has the same issue, just changing the definition of the extra newlines in the output to \n, which is what I would expect for legitimate newlines in my template.

The issue appears to be in velocity's discarding of a newline character (of either type) after a # directive. If I edit the fourth line of the template to be #***##set ($IDAlignment = "left")#end the issue also disappears. Without indents any new lines after a #set are ignored, however, with indents for some reason they are no longer escaped on my server copy but not on my local.

Edit 3: I bypassed tomcat/apache/browser handling of the export and just printed the raw output from velocity into my debug log, the result definitely has a new line immediately after running template.merge(context, sw), which means its coming from velocity.

CodePudding user response:

I found the way to standardise the output of both installations was to add the following line to my Velocity calling code:

Velocity.setProperty("space.gobbling", "bc");

This does not explain why it was behaving differently while running the same velocity version with the same defaults so there may be a deeper issue involved, however this solved the immediate problem.

  • Related