2011-12-06 38 views
6

Đây là build của tôi kịch bản:MSBuild và tạo ZIP file

<?xml version="1.0" encoding="utf-8" ?> 
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/> 
    <PropertyGroup> 
    <!-- Path where the solution file is located (.sln) --> 
    <ProjectPath>W:\Demo</ProjectPath> 
    <!-- Location of compiled files --> 
    <DebugPath>W:\Demo\bin\Debug</DebugPath> 
    <ReleasePath>W:\Demo\bin\Release</ReleasePath> 
    <!-- Name of the solution to be compiled without the .sln extension -->   <ProjectSolutionName>DemoTool</ProjectSolutionName> 

    <!-- Path where the nightly zip file will be copyd --> 
    <NightlyBuildPath>W:\Nightly_Builds\Demo</NightlyBuildPath> 
    <!-- Name of the nighly zip file (YYYYMMDD_NightlyZipName.zip, date added automatically) --> 
    <NightlyZipName>Demo</NightlyZipName> 
    </PropertyGroup> 

    <ItemGroup> 
    <!-- All files and folders from ./bin/Debug or ./bin/Release what will be added to the nightly zip --> 
    <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" /> 
    <ReleaseApplicationFiles Include="$(ReleasePath)\**\*.*" Exclude="$(ReleasePath)\*vshost.exe*" /> 
    </ItemGroup> 

    <Target Name="DebugBuild"> 
    <Message Text="Building $(ProjectSolutionName) Debug Build" /> 
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Clean" Properties="Configuration=Debug"/> 
    <MSBuild Projects="$(ProjectPath)\$(ProjectSolutionName).sln" Targets="Build" Properties="Configuration=Debug"/> 
    <Message Text="$(ProjectSolutionName) Debug Build Complete!" /> 
    <CallTarget Targets="CreateNightlyZip" /> 
    </Target> 

    <Target Name="CreateNightlyZip"> 
    <PropertyGroup> 
     <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate> 
    </PropertyGroup> 
    <MakeDir Directories="$(NightlyBuildPath)"/> 
    <Zip Files="@(DebugApplicationFiles)" 
      WorkingDirectory="$(DebugPath)" 
      ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip" 
      ZipLevel="9" /> 
    </Target> 
</Project> 

Kịch bản của tôi hoạt động hoàn hảo, chỉ có một vấn đề lạ. Khi tôi xây dựng một dự án lần đầu tiên và không có thư mục \bin\Debug và được tạo trong quá trình xây dựng, nhưng tệp ZIP vẫn trống. Chạy kịch bản lệnh xây dựng lần thứ hai khi thư mục \bin\Debug hiện đang có sẵn với tệp được tạo sau đó tệp được thêm vào ZIP.

Điều gì có thể gây ra sự cố khi chạy tập lệnh lần đầu tiên tệp ZIP trống?

Trả lời

10

Sự cố nằm trong bộ sưu tập mục DebugApplicationFiles. Nó được tạo trước khi xây dựng được gọi. Di chuyển mục tiêu DebugApplicationFiles vào mục tiêu CreateNightlyZip. Cập nhật kịch bản của bạn theo cách này:

<Target Name="CreateNightlyZip"> 
    <PropertyGroup> 
     <StringDate>$([System.DateTime]::Now.ToString('yyyyMMdd'))</StringDate> 
    </PropertyGroup> 
    <ItemGroup> 
     <DebugApplicationFiles Include="$(DebugPath)\**\*.*" Exclude="$(DebugPath)\*vshost.exe*" /> 
    </ItemGroup> 
    <MakeDir Directories="$(NightlyBuildPath)"/> 
    <Zip Files="@(DebugApplicationFiles)" 
     WorkingDirectory="$(DebugPath)" 
     ZipFileName="$(NightlyBuildPath)\$(StringDate)_$(NightlyZipName).zip" 
     ZipLevel="9" /> 
</Target> 
3

Nếu PowerShell 5.0 hoặc cao hơn có sẵn, bạn có thể sử dụng lệnh PowerShell trực tiếp.

<Target Name="Zip" BeforeTargets="AfterBuild"> 
    <ItemGroup> 
    <ZipFiles Include="$(OutDir)release\file1.exe" /> 
    <ZipFiles Include="$(OutDir)release\file2.exe" /> 
    </ItemGroup> 
    <Exec Command="PowerShell -command Compress-Archive @(ZipFiles, ',') $(OutDir)release\zippedfiles.zip" /> 
</Target> 
+0

Điều này thậm chí hoạt động với các thư mục con. Chúng được nén bằng cấu trúc thư mục gốc. – kiewic

+1

@kiewic Bạn đã làm cách nào để thực hiện việc này? Trong trường hợp của tôi, vì MSBuild mở rộng nhóm mục, một danh sách các đường dẫn được phân tách bằng dấu phẩy được cung cấp cho dòng lệnh và Nén-Lưu trữ sẽ cố gắng đặt mọi đường dẫn được cung cấp cho thư mục gốc. Vì vậy, tôi nhận được một kho lưu trữ phẳng. Tôi muốn sử dụng nhóm mặt hàng (thay vì cung cấp mẫu trực tiếp cho Nén-Lưu trữ) vì tôi có rất nhiều loại trừ, v.v. – realMarkusSchmidt

+0

@realMarkusSchmidt bạn có thể giữ cấu trúc ban đầu không? Tôi muốn đạt được điều tương tự. – kerzek

Các vấn đề liên quan