Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 perfectly, only there is one strange problem. When i build a project first time and there is no \bin\Debug folder and its created during the build, but the ZIP file still comes empty. Running the build script second time when the \bin\Debug folder is now in place with builded files then the file are added to the ZIP.

What could be the problem that running script first time the ZIP file is empty?

like image 929
hs2d Avatar asked Dec 06 '11 09:12

hs2d


People also ask

What is MSBuild script?

The Microsoft Build Engine is a platform for building applications. This engine, which is also known as MSBuild, provides an XML schema for a project file that controls how the build platform processes and builds software. Visual Studio uses MSBuild, but MSBuild doesn't depend on Visual Studio.

How do I run MSBuild task?

To execute a task in an MSBuild project file, create an element with the name of the task as a child of a Target element. If a task accepts parameters, these are passed as attributes of the element.


1 Answers

The problem is in the DebugApplicationFiles item collection. It is created before the build is invoked. Move the DebugApplicationFiles into CreateNightlyZip target. Update your script this way:

<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>
like image 60
Ludwo Avatar answered Oct 20 '22 17:10

Ludwo