MSBuild:如何在 Visual Studio 中的 Post Build 事件期间访问 Target 设置的 属性 值

MSBuild: How to access a property value set by a Target during the Post Build event in Visual Studio

我有一个调用批处理文件的 PostBuild 事件,我需要将特定参数传递到批处理文件中。此参数通过另一个任务填充,该任务通过在 PostBuildEvent 之前配置为 运行 的目标调用。

当使用元素作为部分的一部分显示时,我可以看到它成功显示。

但是 PostBuildEvent 下的 $(TargetFrameworkToolsFolderPath) 有一个“”值。有没有办法在 post 构建事件中访问此自定义 属性?

示例:

<UsingTask TaskName="GetTargetFrameworkToolsFolderName" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
   <ParameterGroup>
     <SDKFolderPath ParameterType="System.String" Required="true" />
     <TargetFrameworkVersionStr ParameterType="System.String" Required="true" />
     <TargetFrameworkToolsFolder ParameterType="System.String" Output="true" />
   </ParameterGroup>
   <Task>
     <Code Type="Fragment" Language="cs">
        TargetFrameworkToolsFolder = SDKFolderPath + "\" + "bin\NETFX " + TargetFrameworkVersionStr.Substring(1) + " Tools\";
     </Code>
   </Task>
</UsingTask>

<Target Name="FindTargetFrameworkToolsFolderPath" BeforeTargets="PostBuildEvent">
   <GetFrameworkSdkPath>
     <Output TaskParameter="Path" PropertyName="SdkPath" />
   </GetFrameworkSdkPath>
   <GetTargetFrameworkToolsFolderName SDKFolderPath="$(SdkPath)" TargetFrameworkVersionStr="$(TargetFrameworkVersion)">
     <Output PropertyName="TargetFrameworkToolsFolderPath" TaskParameter="TargetFrameworkToolsFolder"/>
   </GetTargetFrameworkToolsFolderName>
   <Message Text="$(TargetFrameworkToolsFolderPath)" Importance="normal" /> --> Displayed correctly here
</Target>

<PropertyGroup>
   <PostBuildEvent>
      call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.
   </PostBuildEvent>
</PropertyGroup>

But $(TargetFrameworkToolsFolderPath) under the PostBuildEvent has an "" value. Is there a way to access this custom property in the post build event?

实际上<PostBuildEvent>是一个属性,MSBuild先读取所有属性,然后执行所有目标。

如果将下面的这些放在定义 属性 TargetFrameworkToolsFolderPath 的目标之外,下面的这些将始终首先执行,正如预期的那样,TargetFrameworkToolsFolderPath 的值将为空。

为避免这种情况,您应该将 PostBuildEventTargetFrameworkToolsFolderPath 属性放在同一个目标中,并确保目标足够早地执行,例如 运行 在 PrepareForBuild目标。

<PropertyGroup>
   <PostBuildEvent>
      call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.
   </PostBuildEvent>
</PropertyGroup>

解决方案

试试下面这个:

 <UsingTask TaskName="GetTargetFrameworkToolsFolderName" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
    <ParameterGroup>
      <SDKFolderPath ParameterType="System.String" Required="true" />
      <TargetFrameworkVersionStr ParameterType="System.String" Required="true" />
      <TargetFrameworkToolsFolder ParameterType="System.String" Output="true" />
    </ParameterGroup>
    <Task>
      <Code Type="Fragment" Language="cs">
        TargetFrameworkToolsFolder = SDKFolderPath + "\" + "bin\NETFX " + TargetFrameworkVersionStr.Substring(1) + " Tools\";
      </Code>
    </Task>
  </UsingTask>
  <Target Name="MyFindTargetFrameworkToolsFolderPath" AfterTargets="PrepareForBuild">
    <GetFrameworkSdkPath>
      <Output TaskParameter="Path" PropertyName="SdkPath" />
    </GetFrameworkSdkPath>
    <GetTargetFrameworkToolsFolderName SDKFolderPath="$(SdkPath)" TargetFrameworkVersionStr="$(TargetFrameworkVersion)">
      <Output PropertyName="TargetFrameworkToolsFolderPath" TaskParameter="TargetFrameworkToolsFolder" />   
    </GetTargetFrameworkToolsFolderName>

    <PropertyGroup>
      <PostBuildEvent> call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.</PostBuildEvent>
    </PropertyGroup>
    <Message Text="$(TargetFrameworkToolsFolderPath)" Importance="normal" />   

  </Target>

希望对您有所帮助。