Embedding resources after build?

I currently have a build setup as follows, allowing me to embed all references DLLs as embedded resources in my assembly. This operates at the AfterResolveReferences target and works flawlessly. It also allows me to produce a single executable which doesn't need any additional DLLs to launch (since it loads these at runtime).

Now, I would like to include the PDB information as well. I already do this with all referenced assemblies, but not the assembly I am building, since that is (for obvious reasons) produced after that target.

To recap:

  1. I am building AssemblyA.exe.
  2. It has AssemblyB.dll and AssemblyC.dll as references, so these are included in AssemblyA.exe as embedded resources during build.
  3. After building AssemblyA.exe, MSBuild also produces a AssemblyA.pdb file.
  4. This is where I want to then also embed AssemblyA.pdb into AssemblyA.exe as embedded resource.

Is that possible somehow? I am aware that this may trigger a double-build.

Upvotes: 2

Views: 3487

Answers (2)

I ended up writing the following to my project file - works flawlessly. It does a double-build, but it works.

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

Upvotes: 3

Fabian
Fabian

Reputation: 2086

If a double compile is not a problem you can create your own target, compile to a temporay folder via msbuild task and then embed the files you need from this temporary folder.

You have to do a rebuild because otherwise it will cache the assemblies.

Your target to compile in the .proj file would look like this:

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

Files that are included as EmbeddedResoucre in BeforeBuild target in the project:

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

Upvotes: 1

Related Questions