代码之家  ›  专栏  ›  技术社区  ›  R. Savelyev

MSBug。在生成之前创建EmbeddedResource

  •  1
  • R. Savelyev  · 技术社区  · 7 年前

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

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

    它很好用。输出程序集包含作为EmbeddedResource的所有.dll引用。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Leo Liu    7 年前

    MSBug。在生成之前创建EmbeddedResource

    你可以试着用 预先建造 对csproj文件的操作,以包括嵌入的资源:

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

    现在,msbuild会将此文件作为嵌入资源添加到程序集中。

    更新:

    谢谢@martin ullrich。他指出了正确的方向,我们可以用 <Target Name="EmbedLocal" BeforeTargets="PrepareForBuild"> Directory.Build.props 以解决此问题。你可以检查一下它是否适合你。

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