We will zip up the outputs of the build so they can be easily distributed. To do this, the Zip build task that is part of the “MsBuld.Community.Tasks” library will be used. If you are not familiar with this library and how to setup then please read through my two previous posts to get up to speed.
- Create a Build File for a Visual Studio Solution - MsBuild Series
- Mercurial Revision No to Version your AssemblyInfo - MsBuild Series
- A zip of all binaries needed to run the application
- A zip of all source code needed to compile the application.
1) Change the output directory to “build\bin”
Firstly we are going to change our build output directory inside visual studio so that the binaries are placed into a new folder called bin under the build directory “[project]\Build\Bin”. Having a sub directory removes the clutter from the root of our build directory so we can place our zip files there.Below is the screen shot of the new location being set inside visual studio for our automated_build configuration.
2) Creating the Zip-Binaries target
We have already included the “MSBuild.Community.Tasks” targets in our build file, so only thing we need to do before using the zip task is to ensure that we have copied the ICSharpCode.SharpZipLib.dll binary into our “Tools\MsBuildCommunityTasks” directory. This binary is included in the MsBuildCommunityTasks download.Now declare the Zip-Binaries target as follows
Notes on the Zip-Binaries target<Target Name="Zip-Binaries"> <ItemGroup> <BinDirectoryFiles Include="$(BuildOutputDir)\bin\**" /> </ItemGroup> <Zip Files="@(BinDirectoryFiles)" WorkingDirectory="$(BuildOutputDir)\bin\" ZipFileName=".\$(BuildOutputDir)\Jobping.StickyBeak_Binaries_$(Major).$(Minor).$(Build).$(Revision).zip" /> </Target>
- BinDirectoryFiles – this variable references all files below the "build\bin” directory. These are the files that we will zip.
- The WorkingDirectory attribute for the zip task specifies the root directory for the zip file. We speicify the “build\bin” directory as the root for our zip
- We use the version information to name the resultant zip file.
2) Creating the Zip-Source target
Apart from the binaries we also want to distribute the source code. So, we are going to declare a Zip-Source target for our build file as follows:<Target Name="Zip-Source">Notes on the Zip-Source target
<ItemGroup> <SourceFilesExclude Include="**\.hg\**" /> <SourceFilesExclude Include="build**" /> <SourceFilesExclude Include="**\.*" /> <SourceFilesExclude Include="**\bin\**" /> <SourceFilesExclude Include="**\obj\**" /> <SourceFilesExclude Include="**\Test\**" /> <SourceFilesExclude Include="**\TestResults\**" /> <SourceFilesExclude Include="**\*.user" /> <SourceFilesExclude Include="**\*.suo" /> <SourceFilesExclude Include="**\*.cache" /> <SourceFilesExclude Include="**\*.vsmdi" /> <SourceFilesExclude Include="**\*.testsettings" /> </ItemGroup> <ItemGroup> <SourceFiles Include="**\*.*" Exclude="@(SourceFilesExclude)" /> </ItemGroup> <Zip Files="@(SourceFiles)" ZipFileName=".\$(BuildOutputDir)\Jobping.StickyBeak_Source_$(Major).$(Minor).$(Build).$(Revision).zip" /> </Target>
- We generally want to zip up everything, so we include all files by using the wildcard “**\*.*” which means all files in all directories.
- We specify a list of excludsions, these are source control files and other files that are generated or not necessary in order to build our application.
- Getting the exclude list correct requires some testing, you should test your source zip by extracting it and making sure you can compile the solution.
3) Incorporating the Zip tasks into our default build
Since we can now created the two zip targets all we need to do is have our default build target depend on these targets, as follows:<Target Name="Build" DependsOnTargets="Clean; VersionSolutionInfo; Compile; Zip-Source; Zip-Binaries">That’s it. Below is a screen shot of the kicking off a build and the resultant output in the build directory.
<Message Text="Clean, VersionSolutionInfo, Compile, Zip-Source, Zip-Binaries"/> </Target>
Screen shot of our ready to ship zips:
Resources:
- Main MsBulid File - Main build with targets : clean, compile & build. Imports all other build files.
- Version MsBuild File - Contains targets from a previous post (to auto version AssemblyInfo from mercurial)
- Zip MsBuild File - Contains targets from this post (to create zips)
- MSBuild Project File Schema Reference - Microsoft documents on MsBuild
- Create a Build File for a Visual Studio Solution - MsBuild Series
- Mercurial Revision No to Version your AssemblyInfo - MsBuild Series
- Zipping Build Outputs - MsBuild Series
- NuPack packaging - MsBuild Series
- NUnit Testing - MsBuild Series (coming later)
- Web Projects and AspNetComplier - MsBuild Series (coming later)
- Cruise Control - MsBuild Series (coming later)
1 comment:
The Source Pack & Go can not only clean up the solution and its projects but also package them into a dated zip so as to back up or mail. You may want to try it:
http://visualsmarter.blogspot.com/2015/10/source-pack-go-of-visual-smarter.html
Post a Comment