【问题标题】:MSBuild. Create EmbeddedResource before build微软构建。在构建之前创建 EmbeddedResource
【发布时间】:2018-07-31 07:19:12
【问题描述】:

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

  <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 引用。

【问题讨论】:

    标签: c# visual-studio msbuild csproj


    【解决方案1】:

    MS 构建。在构建之前创建 EmbeddedResource

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

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

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

    更新:

    感谢@Martin Ullrich。他指出了正确的方向,我们可以在Directory.Build.props 中使用&lt;Target Name="EmbedLocal" BeforeTargets="PrepareForBuild"&gt; 来解决这个问题。您可以检查它是否适合您。

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

    【讨论】:

    • 我有几个项目想要包含这个逻辑。我不想在它更改时复制粘贴它。而“BeforeBuild”的使用取决于在主项目中插入导入的位置(仅在导入CSharpTargets之后)。
    • @R.Savelyev,知道了。如果不想一一修改项目,我们可以使用Directory.Build.props or Directory.Build.targets,但是&lt;Target Name="BeforeBuild"&gt;不能被MSBuild触发,我们必须使用&lt;Target Name="EmbedLocal" BeforeTargets="CoreCompile"&gt;。但是您会遇到原始问题,无法将资源嵌入到您的程序集中。
    • 如果您还设置了&lt;Link&gt; 元数据,它会改变吗?它会自动添加到 PrepareForBulid 目标中以退出 EmbeddedResource 项目。将目标标记为BeforeTargets="PrepareForBuild" 可能会有所帮助。 (顺便说一句,现在不鼓励将目标命名为 BeforeBuild,使用 BeforeTargets="BeforeBuild" / AfterTargets="AfterBuild" 就像新项目系统在 15.3+ 中所做的那样)
    • @R.Savelyev,Martin Ullrich 指出了正确的方向,我已经对其进行了测试,它工作正常,您可以检查它是否适合您,有关更多详细信息,请参阅更新的答案。
    • @MartinUllrich,你是对的。我已经用 BeforeTargets="PrepareForBuild" 对其进行了测试,它工作正常。在您告诉我之前,我不知道BeforeBuild 是气馁的,这里有任何文档或博客来解释这个原因,我想了解很多,请您为我提供一些有关它的链接吗?非常感谢:)。
    猜你喜欢
    • 2020-05-20
    • 2014-09-12
    • 1970-01-01
    • 2018-06-03
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    相关资源
    最近更新 更多