【问题标题】:Embedding resources after build?构建后嵌入资源?
【发布时间】:2016-01-16 18:12:46
【问题描述】:

我目前有如下构建设置,允许我将所有引用 DLL 作为嵌入资源嵌入到我的程序集中。这在AfterResolveReferences 目标上运行并且完美无缺。它还允许我生成一个不需要任何额外 DLL 来启动的单个可执行文件(因为它在运行时加载这些)。

现在,我还想包含 PDB 信息。我已经对所有引用的程序集执行此操作,但不是我正在构建的程序集,因为(出于显而易见的原因)这是在该目标之后生成的。

回顾一下:

  1. 我正在构建 AssemblyA.exe。
  2. 它有 AssemblyB.dll 和 AssemblyC.dll 作为引用,因此它们在构建期间作为嵌入式资源包含在 AssemblyA.exe 中。
  3. 在构建 AssemblyA.exe 之后,MSBuild 还会生成一个 AssemblyA.pdb 文件。
  4. 这也是我想将 AssemblyA.pdb 作为嵌入式资源嵌入到 AssemblyA.exe 的地方。

这有可能吗?我知道这可能会触发双重构建。

【问题讨论】:

    标签: c# msbuild


    【解决方案1】:

    我最终将以下内容写入我的项目文件 - 完美无缺。它进行了双重构建,但它可以工作。

      <Target Name="Prebuild">
        <CallTarget Targets="Clean" />
        <MSBuild Projects="$(SolutionPath)" Targets="Build" Properties="Configuration=Debug;IgnoreRecursion=true" />
      </Target>
      <Target Name="BeforeBuild">
        <ItemGroup>
          <_IgnoreRecursion Include="$(IgnoreRecursion)"/>
        </ItemGroup>
        <CallTarget Targets="Prebuild" Condition="'%(_IgnoreRecursion.Identity)' != 'true'" />
        <CreateItem Include="$(TargetDir)\**\*.*">
          <Output TaskParameter="Include" ItemName="OutputFiles" />
        </CreateItem>
        <ItemGroup>
          <EmbeddedResource Include="@(OutputFiles)" Condition="('%(OutputFiles.Extension)' == '.dll' Or '%(OutputFiles.Extension)' == '.pdb')">
            <LogicalName>%(OutputFiles.DestinationSubDirectory)%(OutputFiles.Filename)%(OutputFiles.Extension)</LogicalName>
          </EmbeddedResource>
        </ItemGroup>
        <Message Importance="high" Text="Embedding: @(OutputFiles->'%(Filename)%(Extension)', ', ')" />
      </Target>
    

    【讨论】:

      【解决方案2】:

      如果双重编译不是问题,您可以创建自己的目标,通过 msbuild 任务编译到临时文件夹,然后从该临时文件夹中嵌入您需要的文件。

      您必须进行重建,否则它将缓存程序集。

      您要在 .proj 文件中编译的目标如下所示:

       <Target Name="YourBuild">
           <MSBuild Projects="YourProject.csproj" Targets="Build"
                Properties="Configuration=Debug;OutputPath=tmp"/>
           <MSBuild Projects="YourProject.csproj" Targets="Rebuild"
                Properties="Configuration=Debug"/>
       </Target>
      

      在项目的 BeforeBuild 目标中作为 EmbeddedResoucre 包含的文件:

      <Target Name="BeforeBuild">
          <ItemGroup>
            <YourFiles Include="tmp\*.pdb" />
          </ItemGroup>
          <ItemGroup>
              <EmbeddedResource  Include="@(YourFiles ->'%(Relativedir)%(filename)%(extension)')"/>
          </ItemGroup> 
        </Target>
      

      【讨论】:

      • 非常有趣!是否有可能以某种方式不让它成为目标的一部分?
      • 我正在考虑这个问题。问题是您需要以某种方式在同一个项目中重建项目。这不可能,我对此表示怀疑。在单独的构建文件中进行发布对我来说似乎更干净。
      • 与同一进程中的最后一个构建相比,如果至少一个构建属性发生了变化,MSBuild 确实会执行新构建(而不跳过它)。
      猜你喜欢
      • 2011-10-03
      • 1970-01-01
      • 2012-06-12
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多