6

My velocity macros are being cached and I don't want them to be... not during development at least.

I've set the following properties in my properties file...

velocimacro.library.autoreload=true
file.resource.loader.cache=false
velocity.engine.resource.manager.cache.enabled=false

... but this doesn't seem to have done the trick

Using velocity properties, how can I configure velocity to not cache macros?

(I'm using velocity 1.6.4)

EDIT:

I don't think the line...

velocity.engine.resource.manager.cache.enabled=false

...is relevant to velocity

5 Answers 5

10

I have been having the same issue with NVelocity (C# port of velocity). Digging through their souce I found that the re-loading of the macros in the global name space are controlled by the following property.

properties.SetProperty(RuntimeConstants.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, true);

I havn't tested this with velocity but looking at their documentation the property exists and seem to do exactly what you need.

1
  • 4
    I don't know why but this has done the trick! Here are my properties: velocimacro.library.autoreload=true file.resource.loader.cache=false velocimacro.permissions.allow.inline.to.replace.global=true
    – Edd
    Oct 19, 2011 at 10:59
2

Looks like you cant do what you want. The only way I could get macro definitions to reload is to put them in their own library file and set the velocimacro.library.autoreload = true.

From http://velocity.apache.org/engine/devel/developer-guide.html

velocimacro.library = VM_global_library.vm

Multi-valued key. Will accept CSV for value. Filename(s) of Velocimacro library to be loaded when the Velocity Runtime engine starts. These Velocimacros are accessable to all templates. The file is assumed to be relative to the root of the file loader resource path.


velocimacro.library.autoreload = false

Controls Velocimacro library autoloading. When set to true the source Velocimacro library for an invoked Velocimacro will be checked for changes, and reloaded if necessary. This allows you to change and test Velocimacro libraries without having to restart your application or servlet container, just like you can with regular templates. This mode only works when caching is off in the resource loaders (e.g. file.resource.loader.cache = false ). This feature is intended for development, not for production.

1

I'm not sure this is possible if the macros are not in a velocity library and just in some template file.

However, in this case, if you just want to make development easier, you can just rename the macro (by doing a find/replace all and just adding a number to the end or something). Then you should be able to see the change straight away. You just have to remember to rename it back to what it's supposed to be when your finished!

0

You might need to set

file.resource.loader.modificationCheckInterval 

This tells velocity how often to check if the file has changed. I can't tell from the docs what the default is, but we have ours set to 2 in our dev env. It might just be that the default value for this prop is a high number or less than 0 which is essentially off, meaning it will never check for changes in your macro file.

5
  • Thanks... I had a quick look into this and it seems to only apply when caching is switched on. I gave a shot anyway but it had no effect
    – Edd
    Apr 6, 2011 at 15:01
  • How are you initializing Velocity? Are you positive your properties file is getting loaded? Velocity is notoriously tricky about it's resource class path.
    – Jberg
    Apr 6, 2011 at 15:58
  • I can see my properties in the Properties object and I'm loading them into the Velocity Engine by doing VelocityEngine velocityEngine = new VelocityEngine(properties); in java. I know that this is working for some of my properties i.e file.resource.loader.path
    – Edd
    Apr 6, 2011 at 17:07
  • How about velocity.log? Anything in there? I see "Velocimacro : autoload on" in my logs when I have that set to true. If it's truly not getting picked up properly you should see "Velocimacro: autoload off" That will at least tell you if that property is getting loaded properly. If it is, there might be some bugs in the version you're using. I just noticed this JIRA issue regarding macro reloading. issues.apache.org/jira/browse/VELOCITY-710 which applies to version 1.7 which is the latest (non beta) version
    – Jberg
    Apr 6, 2011 at 17:47
  • hmm... my velocity log is empty. The bug you've pointed out there isn't the same as my issue... in fact if i rename the macro then i do pick up my changes but that also means modifying every call to it
    – Edd
    Apr 14, 2011 at 9:40
0

You may want to use the #define directive instead of #macro. Theses references can change. With it you can also name a block of VTL code and call it how many times you need it. Define the macro arguments as variables in the same context and use them within the named block as if it were a macro. Both can solve common situations; but they are not equivalent. https://velocity.apache.org/engine/1.7/user-guide.html#define

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.