2011-12-06 24 views
6

Aquí está mi escritura de la estructura:MSBuild y ZIP crear archivos

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

Mi script funciona perfectamente, solamente hay un problema extraño. Cuando construyo un proyecto por primera vez y no hay una carpeta \bin\Debug y se crea durante la compilación, pero el archivo ZIP aún está vacío. Al ejecutar el script de compilación por segunda vez cuando la carpeta \bin\Debug está ahora en su lugar con los archivos compilados, el archivo se agrega al ZIP.

¿Cuál podría ser el problema de ejecutar el script por primera vez, el archivo ZIP está vacío?

Respuesta

10

El problema está en la colección de elementos DebugApplicationFiles. Se crea antes de invocar la construcción. Mueva el DebugApplicationFiles al objetivo CreateNightlyZip. Actualice su script de esta manera:

<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

Si está disponible PowerShell 5.0 o superior, puede usar el comando powershell directamente.

<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

Esto incluso funciona con subdirectorios. Se comprimen utilizando la estructura de directorio original. – kiewic

+1

@kiewic ¿Cómo se las arregló para hacer esto? En mi caso, dado que MSBuild expande el grupo de elementos, se proporciona una lista de rutas separadas por comas a la línea de comandos, y Compress-Archive intentará poner cada ruta proporcionada en la raíz. Entonces obtengo un archivo comprimido. Quiero usar el grupo de elementos (en lugar de proporcionar un patrón directamente a Comprimir-Archivar) porque tengo muchas exclusiones, etc. – realMarkusSchmidt

+0

@realMarkusSchmidt ¿pudiste comprimir la estructura original? Me gustaría lograr lo mismo. – kerzek

Cuestiones relacionadas