Instead of generating a random String during the Maven build, you can just use the version number. This is different for every releases and will fit your use-case.
For this to work, you can use the filtering capabilites of the maven-war-plugin
. In your JSP, declare the placeholder ${project.version}
:
<link rel="stylesheet" type="text/css" href="/css/master.css?cache=${project.version}" media="all"/>
Then, during the Maven build, you can filter this file and Maven will automatically replace this placeholder with the current version.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<webResources>
<resource>
<directory>src/main/webapp</directory>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
If you really want to generate a random String, you can use the maven-antrun-plugin
with the following task (taken from this answer):
<scriptdef name="generateguid" language="javascript">
<attribute name="property" />
<![CDATA[
importClass( java.util.UUID );
project.setProperty( attributes.get( "property" ), UUID.randomUUID() );
]]>
</scriptdef>
<generateguid property="guid1" />
<echo message="${guid1}" />
The property guid1
will then hold your random String, be sure to set exportAntProperties
to true
in the plugin configuration. You can then apply the same logic as above (resource filtering) with the ${guid1}
placeholder instead of ${project.version}