MSBuild。在构建之前创建 EmbeddedResource

我想在编译主单元之前在程序集中嵌入本地引用。但是写的目标不起作用。


  <Target Name="EmbedLocal" BeforeTargets="CoreCompile">

    <Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>    

    <ItemGroup>

      <EmbeddedResource Include="@( ReferencePath->WithMetadataValue( 'CopyLocal', 'true' )->Metadata( 'FullPath' ) )"/>

    </ItemGroup>

    <Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />

  </Target>

@(EmbeddedResource) 此时包含有效的路径列表。


更新:

现在我的导入文件包含:


<Project ToolsVersion="$(MSBuildToolsVersion)" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>

    <EmbedLocalReferences Condition=" '$(EmbedLocalReferences)' == '' ">True</EmbedLocalReferences>

  </PropertyGroup>


  <Target Name="EmbedLocal" BeforeTargets="ResolveReferences" Condition=" '$(EmbedLocalReferences)' == 'True' ">

    <Message Text="Run EmbedLocal for $(MSBuildProjectFullPath)..." Importance="high"/>

    <ItemGroup>

      <EmbeddedResource Include="@(ReferenceCopyLocalPaths->WithMetadataValue( 'Extension', '.dll' )->Metadata( 'FullPath' ))">

        <LogicalName>%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>

      </EmbeddedResource>

    </ItemGroup>   

    <Message Text="Embed local references complete for $(OutputPath)$(TargetFileName)." Importance="high" />

  </Target>

</Project>

它工作正常。输出程序集包含所有 .dll 引用作为 EmbeddedResource。


守候你守候我
浏览 327回答 1
1回答

慕村225694

MSBuild。在构建之前创建 EmbeddedResource您可以尝试对csproj 文件使用BeforeBuild操作以包含嵌入的资源:&nbsp; <Target Name="BeforeBuild">&nbsp; &nbsp; ...&nbsp;&nbsp; &nbsp; <ItemGroup>&nbsp; &nbsp; &nbsp; <EmbeddedResource Include="..."/>&nbsp; &nbsp; </ItemGroup>&nbsp; &nbsp; ...&nbsp; </Target>现在 MSBuild 会将此文件作为嵌入资源添加到您的程序集中。更新:谢谢@Martin Ullrich。他指出了正确的方向,我们可以用<Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">在Directory.Build.props来解决这个问题。您可以检查它是否适合您。&nbsp; <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">&nbsp; &nbsp; ...&nbsp;&nbsp; &nbsp; <ItemGroup>&nbsp; &nbsp; &nbsp; <EmbeddedResource Include="..."/>&nbsp; &nbsp; </ItemGroup>&nbsp; &nbsp; ...&nbsp; </Target>
打开App,查看更多内容
随时随地看视频慕课网APP