Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between 'DependsOnTargets' and 'AfterTargets'?

Tags:

msbuild

What is the difference between DependsOnTargets and AfterTargets? I can not distinguish these two.

like image 306
Nam G VU Avatar asked May 18 '10 08:05

Nam G VU


People also ask

What is MSBuild target?

A target element can have both Inputs and Outputs attributes, indicating what items the target expects as input, and what items it produces as output. If all output items are up-to-date, MSBuild skips the target, which significantly improves the build speed. This is called an incremental build of the target.

Will MSBuild compile without any target?

If MSBuild determines that any output files are out of date with respect to the corresponding input file or files, then MSBuild executes the target. Otherwise, MSBuild skips the target. After the target is executed or skipped, any other target that lists it in an AfterTargets attribute is run.


2 Answers

DependsOnTargets

Defines the targets that must be executed before the target can be executed.

<Target Name="DependsOn" DependsOnTargets="DependencyTarget1;DependencyTarget2">
  <Message Text="Target : DependsOn"/>
</Target>

<Target Name="DependencyTarget2">
  <Message Text="Target : DependencyTarget2"/>
</Target> 

<Target Name="DependencyTarget1">
  <Message Text="Target : DependencyTarget1"/>
</Target> 

Output
> Target : DependencyTarget1
> Target : DependencyTarget2
> Target : DependsOn

BeforeTargets and AfterTargets (Only available in MSBuild 4)

Indicates that the target should run before or after the specified target or targets.

<Target Name="BeforeAndAfter">
  <Message Text="Target : BeforeAndAfter"/>
</Target>

<!-- BeforeTarget1 will run BEFORE target "BeforeAndAfter" -->
<Target Name="BeforeTarget" BeforeTargets="BeforeAndAfter">
  <Message Text="BeforeTarget run before : BeforeAndAfter"/>
</Target> 

<!-- BeforeTarget1 will run AFTER target "BeforeAndAfter" -->
<Target Name="AfterTarget" AfterTargets="BeforeAndAfter">
  <Message Text="AfterTarget run after : BeforeAndAfter"/>
</Target> 

Output
> BeforeTarget run before : BeforeAndAfter
> Target : BeforeAndAfter
> AfterTarget run after : BeforeAndAfter
  • If you have multiples targets that should run before or after the same specified target, they will be executed in declaration order :

    <Target Name="BeforeAndAfter">
      <Message Text="Target : BeforeAndAfter"/>
    </Target>
    
    <!-- 
       BOTH BeforeTarget1 and BeforeTarget2 should run before target "BeforeAndAfter"
    -->
    <Target Name="BeforeTarget1" BeforeTargets="BeforeAndAfter">
      <Message Text="BeforeTarget1 run before : BeforeAndAfter"/>
    </Target> 
    
    <Target Name="BeforeTarget2" BeforeTargets="BeforeAndAfter">
      <Message Text="BeforeTarget2 run before : BeforeAndAfter"/>
    </Target> 
    

BeforeTargets and AfterTargets could be use to extend existing build process.

For example, with this attributes you can easily execute a target before CoreCompile (defines in Microsoft.CSharp.targets). Without that you'll have to override the property CoreCompileDependsOn.

Without AfterTargets you have no way to easily execute a target after another one if no extension point is defined (CallTarget at the end of the target with a property that you can override)

DependsOnTargets, BeforeTargets and AfterTargets execution order?

When DependsOnTargets, BeforeTargets and AfterTargets are used on the same target, the order of execution is :

  • DependsOnTargets
  • BeforeTargets
  • The target
  • AfterTargets

    <Target Name="MainTarget" DependsOnTargets="DefaultDependsOn">
      <Message Text="Target : MainTarget"/>
    </Target>
    
    <Target Name="DefaultDependsOn">
      <Message Text="Target : DefaultDependsOn"/>
    </Target>
    
    <Target Name="DefaultBeforeTarget" BeforeTargets="MainTarget">
      <Message Text="Target : DefaultBeforeTarget"/>
    </Target>
    
    <Target Name="DefaultAfterTarget" AfterTargets="MainTarget">
      <Message Text="Target : DefaultAfterTarget"/>
    </Target> 
    
    Output
    > Target : DefaultDependsOn
    > Target : DefaultBeforeTarget
    > Target : MainTarget
    > Target : DefaultAfterTarget
    
like image 157
Julien Hoarau Avatar answered Oct 19 '22 15:10

Julien Hoarau


More succinctly from this GitHub issue on Microsoft Docs:

<Target Name="x" DependsOnTargets="y" /> means:

If something wants to run x, y must run first.

<Target Name="a" AfterTargets="b" /> means:

If something runs b, then run a after it.

like image 34
aoetalks Avatar answered Oct 19 '22 13:10

aoetalks