0

I have a C# project (vs2013) and for this project, i want to do always a clean solution before the build. Now i can (again and again) choose clean project before building, but this should be possible using a Target tag in the .csproj file. I started looking on the internet and came up with a solution that did not work:

<Target Name="Clean"
       BeforeTargets="Build">
  <Message Text="### Start Clean before build"
           Importance="high" />
  <Exec Command="CALL &quot;$(MSBuildBinPath)\msbuild.exe&quot; &quot;$(MSBuildProjectFullPath)&quot; /t:Clean" />
  <Message Text="### Finished Clean before build"
           Importance="high" />
</Target>

But this results in an infinite loop, starting msbuild over and over again.

Dennis
  • 1,810
  • 3
  • 22
  • 42

1 Answers1

2

You've defined the target recursively. Don't name it "Clean", name it "PreBuildClean" or something similar.

Also you don't have to call out to msbuild externally using Exec. You can use the CallTarget task to invoke a target directly.

<Target Name="PreBuildClean"
   BeforeTargets="Build">
  <Message Text="### Start Clean before build"
           Importance="high" />
  <CallTarget Targets="Clean" / >
  <Message Text="### Finished Clean before build"
           Importance="high" />
</Target>

The Rebuild target is just Clean followed by Build, so I'm not sure why you'd need this, unless it is only for one specific project in the solution.

Mike Zboray
  • 39,828
  • 3
  • 90
  • 122