You have reached the blog of Keith Elder. Thank you for visiting! Feel free to click the twitter icon to the right and follow me on twitter.

ILMerge – How to Merge Assemblies After A Build

Posted by Keith Elder | Posted in .Net | Posted on 02-11-2010

The other day I mentioned I was working on an API to communicate with Sonic, an enterprise messaging system.  After getting the API to the point where I wanted to send it to some other developers I wanted to package everything up as on DLL.  ILMerge to the rescue!

Sonic provides their own DLLs that were ported from Java for .NET developers.  There are 7 DLLs you have to reference in order to communicate with Sonic.  Here’s how this can be done using ILMerge by simply modifying the project file using an after build target.

The following below does a few things.

  1. It takes all of the assemblies that are outputted to the build directory and passes that to the ILMerge command.
  2. Once they are merged the files are deleted.
   1: <Target Name="AfterBuild">

   2:     <CreateItem Include="@(ReferenceCopyLocalPaths)" Condition="'%(Extension)'=='.dll'">

   3:       <Output TaskParameter="Include" ItemName="IlmergeAssemblies" />

   4:     </CreateItem>

   5:     <Exec Command="&amp;quot;$(MSBuildProjectPath)..\..\ThirdPartyAssemblies\Ilmerge\Ilmerge.exe&amp;quot; /allowDup /log /keyfile:$(AssemblyOriginatorKeyFile) /out:@(MainAssembly) &amp;quot;@(IntermediateAssembly)&amp;quot; @(IlmergeAssemblies->'&quot;%(FullPath)&quot;', ' ')" />

   6:     <Delete Files="@(ReferenceCopyLocalPaths->'$(OutDir)%(DestinationSubDirectory)%(Filename)%(Extension)')" />

   7:   </Target>

Very easy as you can see although the syntax is a little finicky.  What does this mean? It means that developers can just add the one DLL to their project and everything works. 

Hopefully this will help someone as well.

Comments (4)

Sorry it didn’t format correctly, but anywhere you see &quot; just take it out and if the other side has a ” then change it to a ” to make it parse correctly. Hope that helps.

I wish I would have known about @(ReferenceCopyLocalPaths) 6 months ago. I was using some gross logic to loop through all referenced assemblies. I’m sure I’ll be using that in the future. Thanks for the tip!

Code posted is full of & and "e;
Tryng removing them, code don’t works.
Please help me! 🙂

We use this tool/technique on some libraries that we distribute to third parties. Good tool/tip.

Write a comment