Clobber and MSBuild

Jul 18, 2011 at 10:19 AM


I'm currently using the MSBuild task to generate the minified versions of my scripts and css

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\MicrosoftAjax\ajaxmin.tasks" />
  <Target Name="AfterBuild">
      <JS Include="**\*.js" Exclude="**\*.min.js;" />
      <CSS Include="**\*.css" Exclude="**\*.min.css" />
    <AjaxMin JsSourceFiles="@(JS)" JsSourceExtensionPattern="\.js$" JsTargetExtension=".min.js" CssSourceFiles="@(CSS)" CssSourceExtensionPattern="\.css$" CssTargetExtension=".min.css" />

However this doesn't seem to overwrite the minifed files if they already exist. So basically if you ever make any modifications to the source js file the new minified version will not be produced, this seems strange to me.. You can only ever build your minified versions once?

Is there the -Clobber option available in the MSBuild task? Or is there an alternative that can be used?

Jul 18, 2011 at 9:24 PM

I get newly-minified files whenever I change the sources and do a rebuild. I see a couple code-paths in the task that might result in unchanged files:

  1. if the destination file exists but is marked as read-only, the task [supposedly] logs a message and does not update the file.
  2. if there were any errors minifying the file, it doesn't update the output file.
  3. if there were any exceptions updating the file (like UnauthorizedAccessException), it [obviously] won't actually update the output file.

Numbers 2 and 3 both output error messages in the Error List window. I tried setting the read-only flag and building my project, but I didn't see any messages generated (I think it should at least generate a warning).

So the question to you is: is the read-only flag set on your output files? If so, does resetting that flag cause your build output to be generated again? If that helps, it might be a short-term work-around for you. I'm working on a revamped build task right now; I'll include your scenario in the testing process to make sure it works properly.

For 4.25 I've updated the task so that the message is a warning and therefore is actually displayed (VS doesn't seem to display the "messages"). Also added a "Clobber" attribute (default is false) that can be set to true to override the "don't write read-only files" rule -- but be careful setting that value! One can accidentally cream one's source files if not careful. 


Jul 18, 2011 at 9:39 PM

Just released 4.25, which changes the "message" to a "warning" so you will know when it doesn't minify because the file is read-only, and added a Clobber attribute to the build task that will write to the file even if it is marked read-only.

Jul 19, 2011 at 12:16 PM

Fantastic! The files were in fact readonly, however since the message was written to the output window i never noticed. Look forward to trying the new version. Is the new build task you are working on looking to closer align the command line switches as options available in task?