views:

76

answers:

2

I am using MSBuild/yuicompressor to combine and minify JavaScript.

As part of this process, I want to modify my script references so they have a timestamp in the querystring. That way, a user always gets the non-cached version of the file when a new release is published. For example:

<script type="text/javascript" src="/scripts/combined-minified.js?20100727" />

I am using FileUpdate from MSBuildCommunityTasks to update the <script> reference, but it does not have a timestamp:

<FileUpdate
      Files="@(includeFile)"
      Regex="#scriptfiletoken#"
      ReplacementText="&lt;script type='text/javascript' src='/scripts/combined-minified.js' /&gt;"
      />

What is the best way to output this timestamp using MSBuild?

A: 

This method worked from me:

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>

<Target Name="MyTarget">
    <!-- Build timestamp. -->
    <Time>
      <Output TaskParameter="Month" PropertyName="Month" />
      <Output TaskParameter="Day" PropertyName="Day"  />
      <Output TaskParameter="Year" PropertyName="Year" />
    </Time>

    <!-- ....... -->    

    <!-- Add timestamp to includeFile -->
    <FileUpdate
      Files="@(includeFile)"
      Regex="#scriptfiletoken#"
      ReplacementText="&lt;script type='text/javascript' src='/scripts/combined-minified.js?$(Year)$(Month)$(Day)' /&gt;"
      />
</Target>
frankadelic
A: 

Interesting answer you've provided, Frankadelic. Personally, we have an entry in our web.config that we postpend to all our css/images. so if we decide to update a single artifact (ie. css/js.min file or image) we don't need to build. we just update the value in the config file.

lastly, that value can be anything -- not just a date. but we also use dates (or the website version .. which is more or less a date) as the key.

Pure.Krome