MSBuild 3.5 just made my day

While doing some troubleshooting on a project I needed to manually copy some debug assemblies around after every build. But there were a lot of them, and whenever I need to do something repeatedly I try automate it with a script of some sort...because I'm lazy of course. There are a lot of choices today for a little quick and dirty script like this, Batch files, VBScript, Powershell, NAnt, but I thought I'd give MSBuild a try because I figured it would be the least amount of hassle, since moving files around is one of it's main duties during a build. The script was simple to write, but I hit a snag...the destination files were read only, and the copy errored out. MSBuild 2.0's copy task doesn't have an option to overwrite read only files. But MSBuild 3.5 does, so I learned a couple of tricks.

1. Run MSBuild.exe from C:\Windows\Microsoft.NET\Framework\v3.5 (instead of C:\Windows\Microsoft.NET\Framework\v2.0.50727). I have an external tool macro set up in my text editor (Textpad) to run the script currently being edited with a shortcut key, so this was simple to re-point.

2. Tell MSBuild you are using 3.5 features by adding ToolsVersion="3.5" to the Project element.

<Project DefaultTargets="Copy"
        xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
        ToolsVersion="3.5">

  <ItemGroup>
    <Assembly Include="Project1\bin\Debug\Project1.*"/>
    <Assembly Include="Project2\bin\Debug\Project2.*"/>
    <!-- etc... -->
  </ItemGroup>

  <Target Name="Copy">
    <Copy SourceFiles="@(Assembly)"
            DestinationFolder="Destination\bin"
            OverwriteReadOnlyFiles="True"/>
  </Target>

</Project>

 

5 Comments

  • Can you explain how @(Assembly) refers to the ItemGroup. Is a keyword or something you made up? Maybe that's what I'm missing.

  • Ryan,
    This is one of the things MSBuild has that I like better than NAnt. Within an ItemGroup, you make up your own item elements. Then refer to them collectively later with the @(...) syntax.
    Check out http://msdn.microsoft.com/en-us/library/ms164283.aspx where it explains "Using the notation @(myType) allows a collection of items of type myType to be expanded into a semicolon (;) delimited list of strings, and passed to a parameter" It's a very nice and compact syntax for batching things.

  • That does seem pretty convenient, though it also seems like it wold be easy to unintentionally add to a group later on in the script.

  • Ryan,

    If you run MSBuild from VS 2008 command prompt, you will always get 3.5 engine to do things for you.

  • Thanks for your post.

    I as receiving the following error:

    " error MSB4064: The "OverwriteReadOnlyFiles" parameter is not supported by the "Copy" task."

    This error was resolved by adding "ToolsVersion="3.5" attribute as you have mentioned.

    Thanks

Comments have been disabled for this content.