How do I zip an output folder in MSBuild? For the filename I need to use a variable that gets set elsewhere.
2 Answers
"MSBuild.Community.Tasks.Zip" is one way. WorkingCheckout and OutputDirectory are not defined.
But you can get the drift below.
The below will get all files that are not .config files for my zip.
Note "Host" is my custom csproj folder name, yours will be different.
<ItemGroup>
<ZipFilesHostNonConfigExcludeFiles Include="$(WorkingCheckout)\Host\bin\$(Configuration)\**\*.config" />
</ItemGroup>
<!-- -->
<ItemGroup>
<ZipFilesHostNonConfigIncludeFiles Include="$(WorkingCheckout)\Host\bin\$(Configuration)\**\*.*" Exclude="@(ZipFilesHostNonConfigExcludeFiles)" />
</ItemGroup>
<MSBuild.Community.Tasks.Zip Files="@(ZipFilesHostNonConfigIncludeFiles)" ZipFileName="$(OutputDirectory)\MyZipFileNameHere_$(Configuration).zip" WorkingDirectory="$(WorkingCheckout)\Host\bin\$(Configuration)\" />
<!-- -->
Here is the other main-stream option:
If you want to package all files and without preserving folder structure.
<ItemGroup>
<ZipFiles Include="$(OutDir)\**\*.*" />
</ItemGroup>
<Target Name="AfterBuild" Condition="'$(Configuration)'=='Release'">
<Zip ZipFileName="$(OutDir)\output.zip" WorkingDirectory="$(OutDir)" Files="@(ZipFiles)" Flatten="True" Quiet="true" />
</Target>
If you want to preserve folder structure
<Target Name="AfterBuild" Condition="'$(Configuration)'=='Release'">
<Zip ZipFileName="$(OutDir)\output.zip" WorkingDirectory="$(OutDir)" Files="@(ZipFiles)" Flatten="False" Quiet="true" />
</Target>
Flatten="True" means that all directories will be removed and the files will be placed at the root of the zip file.
WorkingDirectory is the base of the zip file. All files will be made relative from the working directory.