为什么我的目标没有执行?

Why is my Target not executed?

我将以下目标文件导入到我的 .csproj 文件中,其中一个目标 (AfterAddPostAction) 从不触发。为什么不? (抱歉,它太冗长了,但 MSBuild 在抽象方面很糟糕,CallTask​​ 没有看到包含 CallTask​​ 元素的 Target 中设置的 属性 值。)

<?xml version="1.0" encoding="Windows-1252"?>

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

  <Target Name="EstablishLog">
    <MakeDir Condition="!Exists('$(MSBuildProjectDirectory)\Logs')" Directories=".\Logs"/>
    <PropertyGroup>
      <PowerShellExe Condition=" '$(PowerShellExe)'=='' ">%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe</PowerShellExe>
      <ScriptPath Condition=" '$(ScriptPath)'=='' ">C:\Users\Admin\Documents\GitHub\powershell-scripts\</ScriptPath>
      <LogState>$(ScriptPath)ProjectSnapShot.ps1</LogState>
      <DoPostAction>$(ScriptPath)postAction-BeforePublish.ps1</DoPostAction>
      <Switches>-NonInteractive -executionpolicy Unrestricted</Switches>
      <Arguments>&quot;&amp; { &amp;&apos;$(ScriptPath)&apos; } &quot;</Arguments>
    </PropertyGroup>
  </Target>

  <Target Name="AfterClean" DependsOnTargets="EstablishLog">
    <PropertyGroup>
      <LogFile >AfterClean$(ApplicationVersion).log</LogFile>
      <LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs$(LogFile)</LogFile>
      <Arguments>&quot;&amp; { &amp;&apos;$(LogState)&apos; } &quot;</Arguments>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) &gt; $(LogFile)" />
  </Target>

  <Target Name="BeforeBuild" DependsOnTargets="EstablishLog">
    <PropertyGroup>
      <LogFile >BeforeBuild$(ApplicationVersion).log</LogFile>
      <LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs$(LogFile)</LogFile>
      <Arguments>&quot;&amp; { &amp;&apos;$(LogState)&apos; } &quot;</Arguments>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) &gt; $(LogFile)" />
  </Target>

  <Target Name="AfterBuild" DependsOnTargets="EstablishLog">
    <PropertyGroup>
      <LogFile >AfterBuild$(ApplicationVersion).log</LogFile>
      <LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs$(LogFile)</LogFile>
      <Arguments>&quot;&amp; { &amp;&apos;$(LogState)&apos; } &quot;</Arguments>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) &gt; $(LogFile)" />
  </Target>

  <Target Name="BeforePublish" DependsOnTargets="EstablishLog">
    <PropertyGroup>
      <LogFile >BeforePublish$(ApplicationVersion).log</LogFile>
      <LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs$(LogFile)</LogFile>
      <Arguments>&quot;&amp; { &amp;&apos;$(LogState)&apos; } &quot;</Arguments>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) &gt; $(LogFile)" />
  </Target>

  <Target Name="AddPostAction" AfterTargets="BeforePublish" DependsOnTargets="EstablishLog">
    <PropertyGroup>
      <PostAction>FileCopyPDA.FileCopyPDA</PostAction>
      <Arguments>&quot;&amp; { &amp;&apos;$(DoPostAction)&apos; &apos;$(PostAction)&apos; $(Configuration)} &quot;</Arguments>
      <LogFile >AddPostAction$(ApplicationVersion).log</LogFile>
      <LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs$(LogFile)</LogFile>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) &gt; $(LogFile)" />
  </Target>

  <!--This one is never called-->
  <Target Name="AfterAddPostAction" DependsOnTargets="EstablishLog;AddPostAction">
    <PropertyGroup>
      <LogFile >AfterAddPostAction$(ApplicationVersion).log</LogFile>
      <LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs$(LogFile)</LogFile>
      <Arguments>&quot;&amp; { &amp;&apos;$(LogState)&apos; } &quot;</Arguments>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) &gt; $(LogFile)" />
  </Target>

  <Target Name="AfterPublish" DependsOnTargets="EstablishLog">
    <PropertyGroup>
      <LogFile >AfterPublish$(ApplicationVersion).log</LogFile>
      <LogFile Condition="Exists('$(MSBuildProjectDirectory)\Logs')">.\Logs$(LogFile)</LogFile>
      <Arguments>&quot;&amp; { &amp;&apos;$(LogState)&apos; } &quot;</Arguments>
    </PropertyGroup>
    <Exec Command="$(PowerShellExe) $(Switches) -command $(Arguments) &gt; $(LogFile)" />
  </Target>

</Project>

DependsOnTargets 是将任务链接到序列中的主要方式。但是,如果您通过 DependsOnTargets 实现序列 A->B->CB 取决于 A)并调用目标 A,则 BC 不会被执行。但是如果你调用C,那么AB都会被执行。

相反,AfterTargets属性中提到的目标A将在A执行后执行。

这就是为什么在您的情况下,如果您想使用 DependsOnTargets,执行哪个目标很重要。