问题描述
DependsOnTargets
和AfterTargets
有什么区别?我无法区分这两个.
What is the difference between DependsOnTargets
and AfterTargets
?I can not distinguish these two.
推荐答案
DependsOnTargets
定义必须先执行的目标.
<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>
-
DependsOnTargets
-
BeforeTargets
- 目标
-
AfterTargets
DependsOnTargets
BeforeTargets
- The target
AfterTargets
BeforeTargets
和AfterTargets
可用于扩展现有的构建过程.
例如,使用此属性,您可以在 CoreCompile (在Microsoft.CSharp.targets 中定义)之前轻松地执行目标.否则,您将不得不覆盖属性CoreCompileDependsOn
.
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
.
没有AfterTargets
,如果没有定义扩展点(在目标末尾的CallTarget
具有可以覆盖的属性),您将无法轻松地执行另一个目标.
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
和AfterTargets
时,执行顺序为:
When DependsOnTargets
, BeforeTargets
and AfterTargets
are used on the same target, the order of execution is :
<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
这篇关于'DependsOnTargets'和'AfterTargets'有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!