40

I am using MSBuild from a script to compile my project. I have noticed that it just does a build and not a clean/rebuild.

I have the following:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build" ToolsVersion="4.0">
<Target Name="Build">
    <MSBuild Projects="$(MSBuildProjectDirectory)\myproj.csproj" Targets="Build" Properties="OutputPath=$(MSBuildProjectDirectory)\..\bin\" />
 </Target>

How do I force a clean/rebuild?

1
  • What file is this? Mar 14, 2019 at 5:12

3 Answers 3

53

"%windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild" Project.sln /p:Configuration=Release /t:Rebuild

3
  • In the question, msbuild is used in a project file. Not directly in the command prompt.
    – warunapww
    May 7, 2015 at 21:30
  • In question "I am using msbuild from a script to compile my project" from a script better use command prompt
    – Geograph
    May 9, 2015 at 6:38
  • 3
    Whether or not it answers the original question, the snippet helped me get where I needed to go.. thanks!
    – madannes
    Oct 20, 2016 at 16:30
37

Change

<MSBuild Projects="$(MSBuildProjectDirectory)\myproj.csproj"
  Targets="Build"
  Properties="OutputPath=$(MSBuildProjectDirectory)\..\bin\" />

to

<MSBuild Projects="$(MSBuildProjectDirectory)\myproj.csproj"
  Targets="Clean;Build"
  Properties="OutputPath=$(MSBuildProjectDirectory)\..\bin\" />

or

<MSBuild Projects="$(MSBuildProjectDirectory)\myproj.csproj" Targets="Rebuild"
  Properties="OutputPath=$(MSBuildProjectDirectory)\..\bin\" />

For more details, look at the MSBuild Task documentation.

2
  • Seems obvious when you see the answer. Should prob have got that one. Thanks for the answer
    – John
    Sep 19, 2012 at 14:20
  • How about the new csproj? Feb 4, 2018 at 6:36
1

Always Try to use

<MSBuild Projects="$(MSBuildProjectDirectory)\myproj.csproj"
  Targets="Clean;Build"
  Properties="OutputPath=$(MSBuildProjectDirectory)\..\bin\" />

Because according to this MSDN article, when you have multiple projects in your solution using "Rebuild" as the target, may cause clean and build to happen in parallel manner. Which may cause problems in project dependencies.

But when you use "Clean and Build" as the target, build will start only after all the projects are cleaned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.