Setting up an msbuild script system
msbuild is not all that hard, but it does come with a whole slew of parameters. You don’t want to memorize them all. Thus, define a good ol’ batch file to launch msbuild:
set msbuild20=C:\Windows\Microsoft.NET\Framework\v2.0.50727\MSBuild.exe
set msbuild35=C:\Windows\Microsoft.NET\Framework\v3.5\MSBuild.exe
set msbuild40=C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe
set msbuild=%msbuild40%
set log_quiet=/v:q
set log_minimal=/v:m
set log_normal=/v:n
set log_detailed=/v:d
set log_diag=/v:diag
set _loglevel=%log_normal%
set projectfile=build.msproj
:start
set target=
if "%1" != "" set target=/t:%1
%msbuild% %_loglevel% %target% %projectfile%
shift
if "%1" == "" goto nomoreparams
goto start
:nomoreparams
if ERRORLEVEL 1 goto error
echo Build completed. Congratulations!
goto end
:error
echo There was an error. Sorry it didn't work out.
goto end
:end
This simple little file will allow you to type things like build debug or build release without worrying exactly how to specify the target. If you want to make it extra nice, control log level with an environment variable, by adding the following line:
if "loglevel%" != "" set _loglevel=%loglevel% <-- new line
simply set loglevel=/v:n from command line to change the log level.
You may also want to pass in some variables, like current directory
That will give you the property $(StartDir) available inside your project file.
Speaking of which, here’s a minimal sample:
ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
</Project>
Admittedly very bare bone.
Let’s add some meat:
DefaultTargets="Showhelp"
ToolsVersion="4.0"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CR>%0A</CR>
</PropertyGroup>
<Target Name="ShowHelp">
<Message Text="Available targets"/>
<Message Text="* @(_availableTargets, '$(CR)* ')"/>
</Target>
</Project>
If you follow the good practice of adding new targets to the item list _availableTargets, you have a help page showing if you pass in no parameters.
<_availableTargets Include="Debug"/>
<_availableTargets Include="Release"/>
<_availableTargets Include="Clean"/>
</ItemGroup>
</Project>
Obviously we’ll have to write the actual targets too, but so far we’ve seen that
* We can add things to a list.
* Display that list with a different separator than semi-colon.
* Define properties with escaped characters (by using the hexadecimal ascii values prefixed by a percent character).
It’s far from complete, but it’s a very good start!
–Jesper Hogstrom