本文介绍了MSBuild.在构建之前创建EmbeddedResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要在编译主机之前将本地引用嵌入到程序集中.但是书面目标不起作用.

I want to embed local references in the assembly before compiling the main unit. But the written target does not work.

  <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)目前包含有效的路径列表.

@(EmbeddedResource) at this moment contains valid list of paths.

更新:
现在,我的导入文件包含:

Update:
Now my import file contains:

<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.

It works fine. Output assembly contains all .dll references as EmbeddedResource.

推荐答案

您可以尝试对csproj文件使用 BeforeBuild 操作,以包括嵌入式资源:

You can try to use BeforeBuild action to the csproj file to include the embedded resources:

  <Target Name="BeforeBuild">
    ... 
    <ItemGroup>
      <EmbeddedResource Include="..."/>
    </ItemGroup>
    ...
  </Target>

现在,MSBuild会将此文件作为嵌入式资源添加到您的程序集中.

Now MSBuild will add this file as embedded resource into your assembly.

更新:

感谢@马丁·乌尔里希(Martin Ullrich).他指出了正确的方向,我们可以在Directory.Build.props中使用<Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">来解决此问题.您可以检查它是否适合您.

Thanks @Martin Ullrich. He pointed out the correct direction, we could use <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild"> in the Directory.Build.props to resolve this issue. You can check if it works for you.

  <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild">
    ... 
    <ItemGroup>
      <EmbeddedResource Include="..."/>
    </ItemGroup>
    ...
  </Target>

这篇关于MSBuild.在构建之前创建EmbeddedResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:35