DependsOnTargetsAfterTargets 有什么区别?
我无法区分这两个。

最佳答案

取决于目标

定义目标 必须在 目标可以执行之前执行。

<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 和 AfterTargets(仅在 MSBuild 4 中可用)

指示目标应该在 之前或之后运行 指定的一个或多个目标。
<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
  • 如果你有多个目标应该在同一个指定目标之前或之后运行,它们将是 以声明顺序 执行:
    <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>
    
  • BeforeTargetsAfterTargets 可用于 扩展现有的构建过程

    例如,使用此属性,您可以轻松地在 CoreCompile (在 Microsoft.CSharp.targets 中定义)之前执行目标。否则,您将不得不覆盖属性 CoreCompileDependsOn

    如果没有 AfterTargets,如果没有定义扩展点,您将无法轻松地执行一个又一个目标(目标末尾的 CallTarget 具有您可以覆盖的属性)

    DependsOnTargets、BeforeTargets 和 AfterTargets 执行顺序?

    DependsOnTargetsBeforeTargetsAfterTargets 用于同一目标时,执行顺序为:
  • DependsOnTargets
  • BeforeTargets
  • 目标
  • 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
    
  • 关于msbuild - 'DependsOnTargets' 和 'AfterTargets' 有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2855713/

    10-13 06:19