Featured Post

Programming Standards, Naming Conventions

It never ceases to amaze me how different naming conventions are from language to language.  It seems each language has their own coding convention or style that gets adopted over time.  More times than not these naming conventions are established by authors in books, magazines, and Internet articles. ...

Read More


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.

  • Anonymous

    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.

  • http://www.davidsilvasmith.com/ Anonymous

    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!

  • Anonymous

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

  • Anonymous

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