3

I am using MSBuild to run some management scripts, so no compiler or linker is involved here and I noticed that the build system skips target which ran successfully in a target.

How do I force to run a target every time?

Sample:

<Project ToolsVersion="4.0" DefaultTargets="Foo" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <Target Name="Foo" >
        <CallTarget Targets="Bar" />
        <CallTarget Targets="Bar" />
    </Target>

    <Target Name="Bar" >
         <Message Text="Bar" />
    </Target>
</Project>   

Calling

C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe msbuild.xml /v:d

Results in

Bar:
  Bar

so it is not called twice, /v:d says that second call was skipped, but it is not clear how to avoid this without creating Targets Bar1 and Bar2

Is there a way to execute target Bar two times in Foo?

Searching for that revolved around cl.exe, xcopy etc detecting no changes but this is MSBuild only

Samuel
  • 6,126
  • 35
  • 70
  • 1
    ok just figured this is a duplicate: [How to invoke the same msbuild target twice?](http://stackoverflow.com/questions/1332731/how-to-invoke-the-same-msbuild-target-twice) – stijn Aug 17 '14 at 12:41

1 Answers1

4

Tagets are only called once in a build unless something changed, so if you want the same target to be invoked multiple times you have to trick MsBuild into this by altering properties. This can be done like this:

<Target Name="Foo" >
  <MSBuild Projects="$(MsBuildThisFile)" Targets="Bar" Properties="Dummy=1" />
  <MSBuild Projects="$(MsBuildThisFile)" Targets="Bar" Properties="Dummy=2" />
</Target>

<Target Name="Bar" >
  <Message Text="Bar" />
</Target>

However as the 'trick' indicates: there is almost always a more 'MsBuild' way of doing this (usually batching over an ItemGroup), so if you make your question more specicic there might be a better answer as well.

stijn
  • 34,664
  • 13
  • 111
  • 163