POSTS

Minimizing Wicket HTML/CSS Turnaround

Many Apache Wicket projects I have seen are setup with a build tool (maven,ant) that builds the entire projects and then performs an exploded deployment into a web container (tomcat, jboss, jetty). When a change in the web app is done it then needs to be redeployed into the container, which in turn triggers a cycle of shutdown and restart of the web app inside the container.

This is how it needs to be for code changes (even if there are workarounds like JRebel from ZeroTurnaround) but when you are only changing to the HTML or CSS this is a real pain. Especially if the project takes time to startup when deployed and you have to wait 30 seconds to see that table header to be correctly padded and aligned (and then discover that it doesn’t in IE).

In ruby-land there is a tool called watchr that watches a directory and triggers a ruby callback whenever any file matching a regexp is changed. So what we will do is to set it up to watch the source tree and copy the changed resource files to the exploded deployment. When Wicket is run in Development mode it will reload markup files when they are changed.

First, (provided you have ruby installed, if not, the first step is to install ruby) install the ruby gem watchr

$ gem install watchr

Then create a watcher script that reacts on HTML/CSS changes and copies those (this example is for a maven/scala project that with the jetty-plugin running the web app exploded in target/app-1.0-SNAPSHOT):

RESOURCES="src/main/scala"
def copy_resource(file)
  resourceWithPackageDir = file[RESOURCES.length, file.length]
  destination = "target/classes/#{resourceWithPackageDir}"
  system("cp -rv #{file} #{destination}")
end
watch("#{RESOURCES}.*\.(html|css)") {|match| copy_resource(match[0])}

Of course you can create more elaborate copy-scripts if you have a special setup, for example if you do put your CSS in a separate directory from the source tree etc.

Place the watchr script in the root directory of the project and run with

$ watchr my_auto_deploy_script.watchr

Presto, your changes to the HTML are copied directly when saved in the editor and are available on the next page reload in the wicket application.

Update it turns out that newer versions of Wicket does not have the markup reloading enabled by default. This can be changed by setting a reload timeout in the application setup method. Something like this (only setting it when in wicket “development” mode)

if (getConfigurationType() == RuntimeConfigurationType.DEVELOPMENT) {
  getResourceSettings().setResourcePollFrequency(Duration.ONE_SECOND)
}