24

I decided to use MSBuild Extension's Zip task to compress some of my source code at every build.

However, this not works:

<UsingTask TaskName="MSBuild.ExtensionPack.Compression.Zip" AssemblyFile="MSBuild.ExtensionPack.dll" />
<Target Name="AfterBuild">
    <CallTarget Targets="ZipSourceFiles" />
</Target>
<Target Name="ZipSourceFiles" Condition="'$(ConfigTransform)'=='ImRunningOnTheServer'">
    <MSBuild.ExtensionPack.Compression.Zip TaskAction="Create" CompressFiles="c:\source.txt" ZipFileName="C:\target.zip"/>
</Target>

I got the following error message:

The "MSBuild.ExtensionPack.Compression.Zip" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with in the project file, or in the *.tasks files located in the "c:\Windows\Microsoft.NET\Framework\v4.0.30319" directory.

I don't know what causes this error? Any idea?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Zsolt
  • 3,263
  • 3
  • 33
  • 48
  • Does it *have* to be the MSBuild Extension Pack? I never used that, but I could give you a working example for [MSBuild Community Tasks](https://github.com/loresoft/msbuildtasks) instead. – Christian Specht Oct 05 '12 at 15:10
  • It don't have to be this pack. It just have to zip the files correctly :) – Zsolt Oct 05 '12 at 15:15

2 Answers2

36

Example for MSBuild Community Tasks:

<Import Project="lib\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />

<Target Name="Zip">
        <CreateItem Include="YourSourceFolder\*.*" >
                <Output ItemName="ZipFiles" TaskParameter="Include"/>
        </CreateItem>
        <Zip ZipFileName="YourZipFile.zip" WorkingDirectory="YourSourceFolder" Files="@(ZipFiles)" />
</Target>

If you need more examples, here is a complete working MSBuild file from one of my projects.

Christian Specht
  • 35,843
  • 15
  • 128
  • 182
  • Really like the physical layout of that project. Been working with .NET for years and never thought to do anything different from the default VS setup. Will absolutely be rummaging through here for ways to structure future projects. Definitely changes my way of looking at things. – Dax Fohl Oct 05 '13 at 01:52
  • What exactly in my layout do you mean that's so special? All my private projects look similar to this, but honestly...until now I didn't consider anything about my typical project layout "special". – Christian Specht Oct 05 '13 at 16:55
  • @JaySullivan: What link? There are only two links in my answer, and both are working. – Christian Specht Jan 08 '16 at 22:17
  • Looks like @ChristianSpecht moved his stuff over to github.. His linked file is now here: https://github.com/christianspecht/recordset.net/blob/master/build.proj – Peter Drier Nov 12 '20 at 14:00
  • Yes, I had a lot of Mercurial repos on Bitbucket and I converted/moved them to GitHub when Bitbucket dropped Mercurial support. I fixed a ton of links in my SO answers, but apparently I missed some. I fixed the link in this answer! – Christian Specht Nov 12 '20 at 21:56
11

Here's an alternative to MSBuild Community Tasks. If you're using .net 4.5.1, you can embed the System.IO.Compression functions in a UsingTask. This example uses ZipFile.CreateFromDirectory.

<Target Name="Build">
  <ZipDir
    ZipFileName="MyZipFileName.zip"
    DirectoryName="MyDirectory"
  />
</Target>

<UsingTask TaskName="ZipDir" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
  <ParameterGroup>
    <ZipFileName ParameterType="System.String" Required="true" />
    <DirectoryName ParameterType="System.String" Required="true" />
  </ParameterGroup>
  <Task>
    <Reference Include="System.IO.Compression.FileSystem" />
    <Using Namespace="System.IO.Compression" />
    <Code Type="Fragment" Language="cs"><![CDATA[
      try
      {
        Log.LogMessage(string.Format("Zipping Directory {0} to {1}", DirectoryName, ZipFileName));
        ZipFile.CreateFromDirectory( DirectoryName, ZipFileName );
        return true;
      }
      catch(Exception ex)
      {
        Log.LogErrorFromException(ex);
        return false;
      }
    ]]></Code>
  </Task>
</UsingTask>
jcox
  • 786
  • 8
  • 11