Question

So, I have used MSBuild but this was years ago.

I want to create a Release build for a solution where once built, it will copy all files into a variable set folder "ReleaseDrop" and zip up the contents. Before zipping, I want to make sure it copies only the necessary files (i.e no pdb, no sln, no csproj, no .cs files (but .cshtml is allowed) or only certain directories and exclude other directories within a directory.

how can I do this?

Was it helpful?

Solution

This should be a start. It specifies a bunch of files to include in a release, copies them to a directory and zips them. For the zip part I used MSBuild Extension Pack since I have it installed anyway, but you could just as well use a prtable version of 7z or so and incoke it with the Exec task.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >

<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>

<!--default values for properties if not passed-->
<PropertyGroup>
  <ProjectDir Condition="'$(ProjectDir) == ''">C:\Projects\MyProject</ProjectDir>
  <ReleaseDrop Condition="'$(ReleaseDrop) == ''">c:\Projects\MyProject\ReleaseDrop</ReleaseDrop>
</PropertyGroup>

<!--build list of files to copy-->
<ItemGroup>
  <SourceFiles Include="$(ProjectDir)\bin\*.exe" Exclude="$(ProjectDir)\bin\*test*.exe"/>
  <SourceFiles Include="$(ProjectDir)\bin\*.cshtml" />
</ItemGroup>

<!--copy files-->
<Target Name="CopyFiles">
  <MakeDir Directories="$(ReleaseDrop)" />
  <Copy SourceFiles="@(SourceFiles)" DestinationFolder="$(ReleaseDrop)" />
</Target>

<!--after files are copied, list them then zip them-->
<Target Name="MakeRelease" DependsOnTargets="CopyFiles">
  <ItemGroup>
    <ZipFiles Include="$(ReleaseDrop)\*.*"/>
  </ItemGroup>
  <Zip ZipFileName="$(ReleaseDrop)\release.zip" Files="@(ZipFiles)" WorkingDirectory="$(ReleaseDrop)"/>
</Target>

</Project>

can be invoked like

msbuild <name of project file> /t:MakeRelease /p:ProjectDir=c:\projects
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top