2012-10-05 22 views
18

Her yapıdaki bazı kaynak kodlarımı sıkıştırmak için MSBuild Extension'ın Zip görevini kullanmaya karar verdim. Ancak MSBuild eklentisinin Zip görevi nasıl kullanılır?

bu değil çalışır:
<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> 

aşağıdaki hata iletisi aldım:

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.

bu hataya neden bilmiyorum? Herhangi bir fikir? MSBuild Community Tasks için

+0

MSBuild Uzantısı Paketi * olması * için * var mı? Bunu hiç kullanmadım, ancak bunun yerine [MSBuild Community Tasks] (https://github.com/loresoft/msbuildtasks) için size bir örnek verebilirim. –

+0

Bu paket olmak zorunda değil. Sadece doğru dosyaları sıkıştırmak zorunda :) – Zsolt

cevap

32

Örnek:

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

Daha fazla örnek gerekirse, benim projelerden birinden here is a complete working MSBuild file.

+0

Teşekkürler! İyi çalıştı! – Zsolt

+0

Gerçekten bu projenin fiziksel düzeni gibi. .NET ile yıllarca çalıştım ve varsayılan VS kurulumundan farklı bir şey yapmayı hiç düşünmemiştim. Gelecekteki projeleri yapılandırmanın yolları için kesinlikle burada bir araya geleceksiniz. Kesinlikle şeylere bakma yolumu değiştirir. –

+0

Tam olarak benim düzenimde ne demek bu kadar özel mi demek istiyorsun? Bütün özel projelerim buna benziyor, ama dürüstçe ... şimdiye kadar tipik proje planım hakkında "özel" bir şey düşünmedim. –

4

MSBuild Community Tasks için bir alternatif. .net 4.5.1 kullanıyorsanız, UsingTask numaralı telefona System.IO.Compression işlevlerini yerleştirebilirsiniz. Bu örnekte ZipFile.CreateFromDirectory kullanılmıştır.

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