Build only one project in a solution from command line

C#Visual StudioMsbuild

C# Problem Overview


I have a solution with lots of solution folders with lots of c# projects inside them.

How do I build/rebuild only one of those projects from command line?

I guess there's some way to do it using msbuild but I don't know anything about msbuild.

Thanks!

C# Solutions


Solution 1 - C#

Given a solution file with projects in it, and you want to build / rebuild one project.

This webpage on MSDN lists exactly what you have to do:

http://msdn.microsoft.com/en-us/library/ms171486.aspx

So given a solution file mysolution.sln with projects:

  • foo.vcxproj
  • bar.vcxproj
  • baz.vcxproj

where they all depend on each other in bottom to top order. So that baz is most independent, bar depends on baz and foo depends on bar.

If you want to build foo then you do:

MSBuild mysolution.sln /target:foo

The other answers here didn't account about dependencies. Sure msbuild.exe will build a single project file (i.e. foo.vcxproj), but it would fail if bar and baz were not built yet. In order to build multiple projects and get the independent projects built first you have to pass in the solution file (After all the OP did mention this was part of a solution file). Then pass in the project name and a target delimited by a colon.

MSBuild mysolution.sln /target:foo:Rebuild

Big assumption here. I'm assuming that the project name $(ProjectName) matches that of the file name.

Edit (from comment): If you happen to have dots (.) in the project name, you'll need to replace them with an underscore (_).

Solution 2 - C#

You can simply call msbuild and pass it the .csproj/.vbproj project file that you want to build, and it will do only that one.

So something like:

cd \MySolution
msbuild .\Project1\Project1.csproj

Solution 3 - C#

You can consult this reference to learn more about using MSBuild from the command-line. Here is an example of what you need:

MSBuild.exe MyProject.proj /t:rebuild

Solution 4 - C#

Posting as information to future seekers

set MSBuildEmitSolution=1

https://stackoverflow.com/a/40372894/826862

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmmutilvaView Question on Stackoverflow
Solution 1 - C#C JohnsonView Answer on Stackoverflow
Solution 2 - C#Joe EnosView Answer on Stackoverflow
Solution 3 - C#BernardView Answer on Stackoverflow
Solution 4 - C#freshprinzeView Answer on Stackoverflow