构建后嵌入资源?
Embedding resources after build?
我目前有一个构建设置如下,允许我将所有引用 DLL 作为嵌入资源嵌入到我的程序集中。这在 AfterResolveReferences
目标上运行并且完美无缺。它还允许我生成一个不需要任何额外的 DLL 来启动的可执行文件(因为它在运行时加载这些)。
现在,我还想包括 PDB 信息。我已经对所有引用的程序集执行了此操作,但不是我正在构建的程序集,因为那是(出于明显的原因)在该目标 之后生成的。
回顾一下:
- 我正在建设 AssemblyA.exe。
- 它有 AssemblyB.dll 和 AssemblyC.dll 作为参考,因此它们在构建期间作为嵌入资源包含在 AssemblyA.exe 中。
- 构建 AssemblyA.exe 后,MSBuild 还会生成一个 AssemblyA.pdb 文件。
- 这也是我想将 AssemblyA.pdb 作为嵌入资源嵌入到 AssemblyA.exe 中的地方。
这有可能吗?我知道这可能会触发双重构建。
如果双重编译不是问题,您可以创建自己的目标,通过 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>
我最终将以下内容写入我的项目文件 - 完美运行。它做了 double-build,但它有效。
<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>
我目前有一个构建设置如下,允许我将所有引用 DLL 作为嵌入资源嵌入到我的程序集中。这在 AfterResolveReferences
目标上运行并且完美无缺。它还允许我生成一个不需要任何额外的 DLL 来启动的可执行文件(因为它在运行时加载这些)。
现在,我还想包括 PDB 信息。我已经对所有引用的程序集执行了此操作,但不是我正在构建的程序集,因为那是(出于明显的原因)在该目标 之后生成的。
回顾一下:
- 我正在建设 AssemblyA.exe。
- 它有 AssemblyB.dll 和 AssemblyC.dll 作为参考,因此它们在构建期间作为嵌入资源包含在 AssemblyA.exe 中。
- 构建 AssemblyA.exe 后,MSBuild 还会生成一个 AssemblyA.pdb 文件。
- 这也是我想将 AssemblyA.pdb 作为嵌入资源嵌入到 AssemblyA.exe 中的地方。
这有可能吗?我知道这可能会触发双重构建。
如果双重编译不是问题,您可以创建自己的目标,通过 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>
我最终将以下内容写入我的项目文件 - 完美运行。它做了 double-build,但它有效。
<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>