开发者

Creating MSBuild target hooks

开发者 https://www.devze.com 2023-02-14 12:54 出处:网络
Can someone please point me to a reference about target hooks in MSBuild? I\'m looking for something that will let me define targets to run before and after a specified target.I know this can be done

Can someone please point me to a reference about target hooks in MSBuild?

I'm looking for something that will let me define targets to run before and after a specified target. I know this can be done using the DependsOnTargets property but I开发者_运维问答've seen references to using target hooks and I'd like to explore that area.

Thanks, Zain


A good list of built-in overridable build process hooks can be found here. For custom targets, the only thing I can think of is to use either the DependsOnTarget attribute (like you mentioned) or the BeforeTargets/AfterTargets attribute (like @Ritch Melton mentioned.) Be careful, the BeforeTargets/AfterTargets are only available in MSBuild 4.0


If you understand the idea behind DependsOnTargets then open up the Microsoft.Common.targets file in the .Net SDK directory (C:\Windows\Microsoft.NET\Framework\v3.5). That file defines the build process for the MSBuild task and .Net projects created by Visual Studio. Look for tags called BeforeXXXX, and AfterXXXX. BeforeBuild and AfterBuild are referenced in the default.csproj file - Snippet:

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
     <!-- To modify your build process, add your task inside one of the targets below and uncomment it.  Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
-->
</Project>

There are others, like Clean, Rebuild, etc..

Define a Target (or Targets) to execute inside those Target elements, like this (Creates a directory, or list of directories based on the value in the Directories Property:

    <Target Name="CreateDir">
    <MakeDir Directories="D:\Dogs.txt"/>
</Target>

Then include those Targets in the BeforeXXX Target:

       <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    <Target Name="BeforeBuild" BeforeTargets="CreateDir">
    </Target>
 </Project>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号