55

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!

Liam
  • 27,717
  • 28
  • 128
  • 190
mmutilva
  • 18,688
  • 22
  • 59
  • 82
  • something like "msbuild TargetProj.csproj /t:rebuild" – driushkin Apr 05 '11 at 20:31
  • 4
    Warning: the accepted answer and the comment above is a dangerous answer. It does not account for solution-dependencies or any targeting that relies on the solution being loaded, even if the goal was to build only a single project. Only do this if you know you can safely isolate your build-flow for a truly atomic project. – kayleeFrye_onDeck Mar 26 '19 at 20:55

4 Answers4

125

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 (_).

max630
  • 8,762
  • 3
  • 30
  • 55
C.J.
  • 15,637
  • 9
  • 61
  • 77
  • 1
    +1 for the answer, and it seems to work well. However, I just tried that scenario, with one dependency relying on another dependency, and running `msbuild` against only the project I cared about still works properly. Apparently msbuild is smart enough to build the dependencies in order. So either solution works equally well from what I can tell. – Joe Enos May 23 '13 at 20:22
  • 21
    Also, if the project is in a solution folder, you need to specify it in the target name, like `/target:myfolder\foo` – Benoit Blanchon Apr 09 '15 at 12:57
  • 41
    If you happen to have dots (.) in the project name, you'll need to replace them with an underscore (_). Once again, MS documentation utterly lacking. – Pedro Pombeiro Jul 03 '15 at 20:26
  • 1
    If you are unsure how the target name might be, the following stackexchange posting will help you: https://stackoverflow.com/a/484528 – user1225999 Sep 21 '17 at 13:03
  • 3
    One other note that makes this a better answer, if the build has pre/post events that utilize variables (e.g. $(SolutionDir)) this method works. The other methods do not supply all of those variables. – palehorse Mar 30 '18 at 17:14
  • I tried this in Azure Devops build.. My solution has periods.. **/My.Project/My.Project.csproj.. I sent that to Azure it worked with the periods, built the specified project, and built it's dependency projects. Tho the order thing I dont understand because project dependencies are not linear like that. – Tim Davis Jun 15 '20 at 05:58
30

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
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • 2
    I've tried that and get: error MSB4019: The imported project "C:\Windows\Microsoft.NET\Framework\v3.5\Workflow.Targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk. – mmutilva Apr 05 '11 at 20:44
  • 1
    Are you using the right msbuild version? Make sure you open the Visual Studio 2010 Command Prompt, so it doesn't use an old .NET Framework builder. If that's not the problem, then there might be a problem with the Workflow installation itself, and I wouldn't know where to go from there. – Joe Enos Apr 05 '11 at 22:40
  • That's it! I was running from Windows Command Prompt which apparently is running other msbuild version installed. How do I locate the proper msbuild.exe I need to run? Because I want to create a compilation .bat and run it from Windows Command Prompt (not VS2010 Command Prompt). Note: the reason for this is that it's a huge solution and I only want to build a couple of projects as part of a deployment script (to deploy to a QA environment from my development workstation). – mmutilva Apr 06 '11 at 16:01
  • 3
    The msbuild is just in your .NET framework directory (probably C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319). So you can add this directory to your global PATH (and remove the other framework versions), or even better would be to just use the full filepath when calling it in your batch file, like `C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\msbuild.exe myProject.csproj` – Joe Enos Apr 06 '11 at 16:36
  • 3
    I would suggest you do your builds through a continuous integration server such as Jenkins. This will allow you to organize your builds better and have more insight into whether they build successfully or not. – Bernard Apr 06 '11 at 16:39
  • In my case I changed to – Sven Sönnichsen May 24 '11 at 15:36
  • 5
    This didn't account for any unbuilt dependencies that a solution file might build. – C.J. May 23 '13 at 17:34
  • This way imported variables are incorrect for me, like target directory etc. – max630 Oct 10 '15 at 20:30
  • 1
    This is a dangerous answer, and potentially ruinous and churn-causing for brittle automation that relies on solution-pathing for targets/vars. – kayleeFrye_onDeck Mar 26 '19 at 20:53
  • /p:BuildProjectReferences=false is helpful when you want to build the only project without dependencies – Boogier Sep 23 '19 at 05:21
6

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
Bernard
  • 7,908
  • 2
  • 36
  • 33
  • I've tried that and get: error MSB4019: The imported project "C:\Windows\Microsoft.NET\Framework\v3.5\Workflow.Targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk – mmutilva Apr 05 '11 at 20:45
  • 2
    This is only telling half the story. The OP said a project in a solution file. A solution file may list extra dependencies that need to be built first. If you are building from scratch and none of the dependencies exist yet, the build can fail. – C.J. May 23 '13 at 17:23
1

Posting as information to future seekers

set MSBuildEmitSolution=1

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

Community
  • 1
  • 1
freshprinze
  • 134
  • 2
  • 10