Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use MSBuild extension's Zip task?

Tags:

.net

msbuild

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?

like image 458
Zsolt Avatar asked Oct 05 '12 15:10

Zsolt


People also ask

How do I create a zip file in MSBuild?

In order to zip your files, you need to specify which files you want to zip. In the script below, I create an ItemGroup called ZipFiles, this includes all the subdirectories (**) and files (*. *) from my Release directory which is my build output folder.

What is MSBuild Extension Pack?

The MSBuild Extension Pack is the successor to the FreeToDev MSBuild Tasks Suite and provides a collection of over 170 MSBuild tasks designed for the . NET 3.5 Framework.


2 Answers

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.

like image 165
Christian Specht Avatar answered Oct 16 '22 20:10

Christian Specht


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>
like image 24
jcox Avatar answered Oct 16 '22 19:10

jcox