Those of you who are already experts in the usage of MSBuild will be just smiling about my Blogspot but for someone who hasn't worked with MSBuild till today (like me) I thought this howto would be very helpful.
Introduction to MSBuild
At this point I won't give you a great introduction into MSBuild. MSBuild is the basic for the development process of .NET Projects. There is another great Tutorial from Thorsten Hans. Please take a look on it if you would like to have a deeper few into the whole MSBuild problem.
Whats my problem
I had a specific problem for me. At our enterprise we use the TFS 2010 including the Teambuilds. Now I want to have a Buildscript on the client who is able to build every solutions local (we have a lot of - big project) and pass it to a folder. The TFS works quite similar and pass everything after the successful build into the droplocation. Thats what I want to build after.
I know my demo is very thin and includes just an ASP.NET MVC project with Unit Tests - unchanged standard:
Now we are going to talk about the very simple but very effective MSBuild:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build"> <PropertyGroup> <OutDir>$(MSBuildStartupDirectory)\OutDir\</OutDir> <SolutionProperties> OutDir=$(OutDir); Platform=Any CPU; Configuration=Release </SolutionProperties> </PropertyGroup> <ItemGroup> <Solution Include="..\MsBuildSample.sln"> <Properties> $(SolutionProperties) </Properties> </Solution> </ItemGroup> <Target Name="Build"> <MSBuild Projects="@(Solution)"/> </Target> </Project>
At the top you will find a PropertyGroup with the description about where to copy the solution into. Instead of searching the solution in the bin\release folder I want to find it in my "OutDir". I use the MSBuild Property MSBuild StartupDirectory to find the right Buildfiles.
In the "SolutionProperties" you will find a description the Solutions will be build after. Now I'm going to write over the OutDir with my own Property.
Now we need a Itemgroup. Of course it it possible to have more solutions into here.
At the end my target "build" follows which is my default target as well (look into line 1) With this I give MSBuild the order to build every selected project into the chosen solution.
I named the whole thing "BuildSolution.build". I think if you name it ".target"
than the Visual Studio IntelliSense will work as well but anyway you can name it what ever you want. I didn't recognize any restrictions.
Open the Buildscript
This is very easy. For example you could use the command prompt from Visual Studio 2010: "msbuild BuildSolution.build"
After building everything will be pasted into my OutDir.
Everything in one directory?
Thats not how it works. If I build class libraries then the .dlls will be pasted to the directory. But of course if I build several Windows Applications I don't want them to end as a mess. Good news: this isn't a problem on web sites:
In the folder _PublishedWebSites you will find every files you need:
fine-tuning: a batch file
As a little fine-tuning I created a batch file for not being forced to use the Visual Studio Command Prompt at every time:
The whole thing will work with another .Net version number as well.
What do we have now?
We have a script which will help us to build 1+n solutions and save them in a folder. With the help of the batch file it it also possible to open other stuff like for example MSDeploy. But in fact I'm not sure how MSBuild and MSDeploy will work together. A first sign would be this.
For what will this help?
My main thought was to rebuild the process local which the TFS creates on the
Buildserver with the help of TeamBuild. As soon as the compiled files are at the folder (DropLocation/OutDir) I start to build my deployment packages. As soon as I finished this I promise to blogg about it. ;)
Fine-tuning 2, open the batch file with visual stuido:
I included the Buildscript with the batch file into the solution. To get mark one it should be possible to run the batch in the solution explorer with a right click:
Alternative you could use this trick.