Подтвердить что ты не робот

Как использовать задачу Zip для расширения MSBuild?

Я решил использовать задачу MSBuild Extension Zip, чтобы сжать часть моего исходного кода в каждой сборке.

Однако это не работает:

<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>

Появилось следующее сообщение об ошибке:

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 <UsingTask> in the project file, or in the *.tasks files located in the "c:\Windows\Microsoft.NET\Framework\v4.0.30319" directory.

Я не знаю, что вызывает эту ошибку? Любая идея?

4b9b3361

Ответ 1

Пример Задачи сообщества MSBuild:

<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>

Если вам нужно больше примеров, вот полный рабочий файл MSBuild из одного из моих проектов.

Ответ 2

Здесь альтернатива Задачи сообщества MSBuild. Если вы используете .net 4.5.1, вы можете встроить функции System.IO.Compression в UsingTask. В этом примере используется 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>