Kauri Documentation
 PreviousHomeNext 
17.3 Add Caching to kauri moduleBook Index18 Packaging Kauri applications

17.3.2 Enable usage of semi-static resources

In the configuration below, we will only activate the caching of semi-static resources when kauri is not started in source mode (-s module-source-locations.xml), because during development it's most likely more convenient to be able to modify and test these resources without restarting let alone rebuilding the module.

17.3.2.1 i) router.groovy

Extend the above configuration:

// various static resources cached by the filter:
router() {
    
  String buildKey = localInSourceMode ? "nocaching" : confRegistry.getConfiguration("build", false).getChild("build", false).getChild("key",false).getValue();
  
  // semi-static resources
  directory(
      uri: "-" + buildKey + ".key/", 
      root: "module:/static-{build}.key"
      )
  
  // static resources
  directory(
      uri: "",
      root: "module:/static"
      )
}

17.3.2.2 ii) placeholder conf/build.xml

Create a folder conf under src/main/kauri, in which you create a placeholder file build.xml :

<!--
   | This placeholder file is replaced (generated) during the build process.
   |
   | This placeholder file is required when running this module from source.
   -->
<conf/>

17.3.2.3 iii) pom.xml

Add/configure the maven antrun plugin in your pom to generate the build key during the build process.

<!--  kauri module building :: producing a conf/build.xml -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>

      <id>KauriStaticResources</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <tasks>
          <mkdir dir="${project.build.directory}/classes/KAURI-INF/conf"/>
          <tstamp>
            <format property="build.time" timezone="GMT + 0" pattern="yyyyMMdd'T'HHmmss'Z'"/>
          </tstamp>
          <condition property="key" value="${project.version}-${build.time}" else="${project.version}">
            <contains string="${project.version}" substring="-SNAPSHOT" />
          </condition>
          <echo file="${basedir}/target/classes/KAURI-INF/conf/build.xml" encoding="utf8"><![CDATA[<?xml version="1.0"?>
<!-- generated by the module build process -->
<conf>
  <build>
    <id>${project.id}</id>
    <by>${user.name}</by>
    <time>${build.time}</time>
    <version>${project.version}</version>
    <key>${key}</key>
    <java>
      <vendor>${java.vendor}</vendor>
      <version>${java.version}</version>
    </java>    
  </build>
</conf>]]></echo>
        </tasks>
      </configuration>
    </execution>
  </executions>
</plugin>

17.3.2.4 iv) Usage in templates

It is convenient to keep the build key of your module in a template variable. You can do this by adding the following in e.g. your base template:

<t:init>
  <t:variable name="fooKey" value="${inSourceMode() ? 'nocaching' : conf('build','build/key')}" />
</t:init>

You can then access the semi-static resources as in the following example:

<script type="text/javascript" src="${publicUri('service:/fooRouter')}/static-${fooKey}.key/foo.util/bar.js"/>

Naturally this example requires that you place the resource bar.js in the folder src/main/kauri/static-{build}.key/foo.util .

 PreviousHomeNext 
17.3 Add Caching to kauri module18 Packaging Kauri applications