MSBuild and creating ZIP files

Here is my build script:

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

My script works fine, there is only one weird problem. When I create a project for the first time, and there is no \bin\Debug folder and it was not created at build time, but the ZIP file is still empty. Running the build script a second time the \bin\Debug folder is now installed with embedded files, then the file is added to ZIP.

What could be the problem with running the script for the first time the zip file is empty?

+6
source share
2 answers

The problem is the collection of DebugApplicationFiles elements. It is created before the assembly is called. Move the DebugApplicationFiles to the CreateNightlyZip target. Update your script as follows:

 <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> 
+10
source

If powershell version 5.0 or higher is available, you can directly use the powershell command.

 <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> 
+3
source

Source: https://habr.com/ru/post/903176/


All Articles