Symbols count in article: 849Reading time: 5 mins.
In part 1 of this blog series we had a look at a very basic MSBuild script which enables us to compile our projects. For doing this I provided you guys with a simple batch file to make it even more simple to execute the build script.
In this episode we will focus on cleaning the folders before building again and getting the Nuget packages using Nuget package restore. The latter I will explain further for you first.
Nuget package restore can be enabled in Visual studio. A best practice is to enable this always, because then we won’t have to put all Nuget packages in source control. By enabling it, every team member will download the missing packages automatically when building his project in Visual Studio. However the build server won’t do this by default so we create a custom target in our build script to make this happen.
This target will clean all our directories based on the project output folders for the given configuration. So if you provide debug it will clean the /bin/Debug folder. We use a ItemGroup to search for all the project files so we can use them in a msbuild task for cleaning the project outputs.
Please note we also recreate the reports directory where we will store our unit test reports etc. We will create a target for this in the following part of this blog series.
As you can see quotes are being encoded as " in out tasks.
Also for the LoadNugetPackages we use a ItemGroup to search for files called packages.config. Those files are used as a parameter for Exec task which will execute nuget.exe.
By adding both targets to our msbuild script we end up with following build script. Please note the _DependsOnTargets_ attribute added on our Compile target. This will make sure Clean and LoadNuGetPackages are executed first. When using Nuget package restore your solution will contain a folder called _.nuget_. In this folder the Nuget executable is located. Also make sure this executable is committed to your source control so the build server later on will have access to it.
``` xml ci.msbuild.xml <Projectxmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Compile"> <PropertyGroup> <Configuration>Debug</Configuration> <Platform>AnyCPU</Platform> <DefineSolutionProperties>false</DefineSolutionProperties>
<!-- General Paths --> <RootPath>$(MSBuildProjectDirectory)</RootPath> <SrcPath>$(RootPath)\src</SrcPath> <ReportsPath>$(RootPath)\reports</ReportsPath> <ToolsPath>$(RootPath)\tools</ToolsPath> <Packages>$(SrcPath)\packages</Packages> </PropertyGroup>