2011-12-06 30 views
6

这里是我的构建脚本:的MSBuild和创建ZIP文件

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

我的脚本作品完美,只有有一个奇怪的问题。当我第一次创建一个项目,并且没有\bin\Debug文件夹及其在构建期间创建的文件夹,但ZIP文件仍然为空。当\bin\Debug文件夹现在具有建成文件时,第二次运行构建脚本,然后将该文件添加到ZIP。

运行脚本首次ZIP文件为空时会出现什么问题?

回答

10

问题出在DebugApplicationFiles项目集合中。它是在构建被调用之前创建的。将DebugApplicationFiles移至CreateNightlyZip目标。以这种方式更新您的脚本:

<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

如果powershell 5.0或更高版本可用,您可以直接使用powershell命令。

<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

这甚至适用于子目录。它们使用原始目录结构进行压缩。 – kiewic

+1

@kiewic你是如何设法做到这一点的?就我而言,由于MSBuild扩展了项目组,所以在命令行中提供了逗号分隔路径列表,Compress-Archive将尝试将每个路径提供给根目录。所以我得到一个扁平的档案。我想使用项目组(而不是直接向压缩存档提供模式),因为我有很多排除项等。 – realMarkusSchmidt

+0

@realMarkusSchmidt是否能够保持原始结构的压缩?我想达到同样的效果。 – kerzek