Thursday, May 20, 2010

MSBuild: Targeting a Configuration from the Command Line

Update: Read the comments for some good tips from other readers.

Firing up Visual Studio to simply build a solution is nuts. Do it from the Visual Studio Command Prompt instead and save yourself time.

What I always forget, though, is how to specify a release build, ergo I’m writing this blog post so I can quickly find it the next time I need it.

From the command line, use the “/p:” switch and pass the value “Configuration=Release” like so:

msbuild CommunityServer.Sync.sln /p:Configuration=Release

I’m also in the habit of first doing a clean build before the release build to ensure I’ve got no leftover cruft:

msbuild CommunityServer.Sync.sln /target:clean

(Actually, I tie all this stuff together with SlickRun shortcuts and batch files, which is even better!)

4 comments:

Unknown said...

One thing to keep in mind - when running clean, you'll want to specify "/p:Configuration=Release" otherwise you'll clean the Debug configuration.

Paul said...

Jim, if you go with something like:
msbuild CommunityServer.Sync.sln /p:Configuration=Release /t:Clean;Build

This will run a clean and then a full build of the solution due to the specified targets switch. If you just go with Clean you won't get build binaries, just removal of the last compilation.

Jim Holmes said...

@Sean & @Paul -- thanks for the clarifications. I was a dork to miss the important issue of specifying WHICH config to clean!

stej said...

I had problems with dependencies when I used just sln file as a source for msbuild.
In this case msbuild wasn't able to figure out what project depends on the other. That's why for each such a sln file I have something like

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<Target Name="CreateProjects" Outputs="@(Projects)">
<CreateItem
Include="{0}">
<Output ItemName="Projects" TaskParameter="Include"/>
</CreateItem>
</Target>

<Target Name="Build" DependsOnTargets="CreateProjects">
<Message Text="Projects (pseudo)order: %(Projects.FileName)"></Message>
<MSBuild
Projects="@(Projects)"
Properties="Configuration={1}"
Targets="Build" StopOnFirstFailure="true">
</MSBuild>
</Target>

</Project>

(copied from psake build. {0} is replaced with actual projects paths)

Subscribe (RSS)

The Leadership Journey