How to run a clean build for a particular project from a solution in visual studio

Visual StudioCommand LineMsbuild

Visual Studio Problem Overview


Suppose I need to build a whole solution (which has multiple projects) in command line, is it possible to run a clean build for a particular projects and run an incremental build for the rest of the project?

Thanks.

Visual Studio Solutions


Solution 1 - Visual Studio

Use msbuild and pass the Clean and Rebuild targets:

msbuild path\to\solution\yoursolution.sln /t:Clean;Rebuild

Or if you only want to rebuild a single project:

msbuild path\to\project\yourproject.csproj /t:Clean;Rebuild

msbuild is available in the Windows SDK or the Visual Studio Command prompt.

Solution 2 - Visual Studio

MSBuild.exe MultiProjectSolution.sln /t:"ProjectName:clean"

Will clean only the specified project in your solution.

Solution 3 - Visual Studio

Just a small refinement for using in PowerShell:

pathTo\MSBuild.exe pathTo\solutionfile.sln /target:"clean;Build"

Source For Different Target

Solution 4 - Visual Studio

Cleaning up specific projects:

msbuild MyProjectFile1 /t:Clean
msbuild MyProjectFile2 /t:Clean
...

This has to be repeated for all projects you need to clean, because MSBuild only accepts single project on command line.

Build whole solution incrementally:

msbuild MySolutionFile

Note, that this will build default configuration/platform for the projects and solution, which is often Debug/AnyCPU or Debug/Win32. If you want specific configuration/platform, you have to add parameters like this: /p:Configuration=Release /p:Platform=x64 to every msbuild command line.

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
QuestionKintarōView Question on Stackoverflow
Solution 1 - Visual StudiojrummellView Answer on Stackoverflow
Solution 2 - Visual StudioCrypthView Answer on Stackoverflow
Solution 3 - Visual StudioBashir MomenView Answer on Stackoverflow
Solution 4 - Visual Studioseva titovView Answer on Stackoverflow