Search code examples
msbuildmsbuildcommunitytasksyui-compressor

How to output a timestamp in MSBuild


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?


Solution

  • This method worked for 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>