John
John

Reputation: 1477

How do I force MSBuild to clean or rebuild?

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?

Upvotes: 41

Views: 51608

Answers (3)

Yohan
Yohan

Reputation: 410

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.

Upvotes: 2

Geograph
Geograph

Reputation: 2604

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

Upvotes: 55

skolima
skolima

Reputation: 32714

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.

Upvotes: 37

Related Questions