Jonah Bishop
Jonah Bishop

Reputation: 12581

How to redefine what "Rebuild Solution" does in Visual Studio?

I am in the process of converting several projects from Visual Studio 2005 to Visual Studio 2012. While doing this, I noticed that the "Rebuild Solution" option has changed subtly for multi-project solutions (I found this subtle behavior change thanks to two very helpful questions here on SO). VS 2005 seemed to do this on a Rebuild:

  1. Clean projects A + B
  2. Build project A
  3. Build project B

VS 2012 appears to do this:

  1. Clean project A
  2. Build project A
  3. Clean project B
  4. Build project B

This is problematic when the two projects are interdependent, especially when the output binaries are placed in the same folder. Is there a way that I can redefine what "Rebuild Solution" does for a given project, so that it mirrors the 2005 behavior?

Upvotes: 2

Views: 1016

Answers (1)

stijn
stijn

Reputation: 35901

Not in an easy way. Rebuilding a solution through VS or msbuild on the commandline actually consists of converting the solution and project files to a temporary msbuild file then calling msbuild with the Rebuild target in that file. This Rebuild target happens to be defined similar to

<ItemGroup>
  <AllProjects Include="Project A"/>
  <AllProjects Include="Project B"/>
</ItemGroup>

<Target Name="Rebuild">
  <MSBuild Projects="@(AllProjects)" Targets="Rebuild"/>
</Target>

Hence it will always result in Clean/Build per project. What you want would be

<Target Name="Rebuild" DependsOnTargets="Clean;Build"/>

which would effectively call Clean first for all projects, then Build for all projects.

However I do not think there is a way to interact with the conversion process of the solution to the msbuild file, nor to redefine what Rebuild Solution does in VS (well maybe there is but it is not straightforward).

A poor man's hack would be to define an External Tool in VS ('Tools->External Tools...->Add'): set 'Command' to your msbuild command, set 'Title' to 'CustomRebuild', set 'Arguments' to '$(SolutionDir)\$(SolutionFileName) /t:Clean;Build /p:Configuration=Debug;Platform=Win32'. Then you can invoke that 'Tools->CustomRebuild' command (also via keyboard etc) and it will do what you want. However only for Debug|Win32 of course. To get rid of that problem you could write a custom extension since there you can access the current configuration and platform (afaik) and then make it programmatically build the solution.

Variation on this theme: add an empty Makefile project to your solution (right-click->Add->New project->General->Makefile project) and in it's properties set the 'Rebuild All Command Line' to msbuild $(SolutionPath) /t:Clean;Build /p:Configuration=$(Configuration);Platform=$(Platform). Then open Configuration Manager and exlude that project from the build (in order to avoid an infinite loop). Now if you right-click the makefile project and select 'Project Only->Rebuild only' it will effectively rebuild the entire solution - but now with the currently selected configuration and platform.

Upvotes: 2

Related Questions