How to Integrate ILMerge into Visual Studio Build Process to Merge Assemblies?

75,143

Solution 1

The article Mixing Languages in a Single Assembly in Visual Studio seamlessly with ILMerge and MSBuild at http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx demonstrates how to use ILMerge and MSBuild within a Visual Studio Project.

Solution 2

The "MSBuild ILMerge task" (or MSBuild.ILMerge.Task) NuGet package makes this process quite simple. It defaults to merging any "copy local" references into your main assembly.

Note: Although the packages have similar names, this one is different from ILMerge.MSBuild.Tasks that Davide Icardi mentioned in his answer. The one I'm suggesting here was first published in August 2014.

Solution 3

Here an alternative solution:

1) Install ILMerge.MSBuild.Tasks package from nuget

PM> Install-Package ILMerge.MSBuild.Tasks

2) Edit the *.csproj file of the project that you want to merge by adding the code below:

  <!-- Code to merge the assemblies into one:setup.exe -->
  <UsingTask TaskName="ILMerge.MSBuild.Tasks.ILMerge" AssemblyFile="$(SolutionDir)\packages\ILMerge.MSBuild.Tasks.1.0.0.3\tools\ILMerge.MSBuild.Tasks.dll" />
  <Target Name="AfterBuild">
    <ItemGroup>
      <MergeAsm Include="$(OutputPath)$(TargetFileName)" />
      <MergeAsm Include="$(OutputPath)LIB1_To_MERGE.dll" />
      <MergeAsm Include="$(OutputPath)LIB2_To_MERGE.dll" />
    </ItemGroup>
    <PropertyGroup>
      <MergedAssembly>$(ProjectDir)$(OutDir)MERGED_ASSEMBLY_NAME.exe</MergedAssembly>
    </PropertyGroup>
    <Message Text="ILMerge @(MergeAsm) -&gt; $(MergedAssembly)" Importance="high" />
    <ILMerge InputAssemblies="@(MergeAsm)" OutputFile="$(MergedAssembly)" TargetKind="SameAsPrimaryAssembly" />
  </Target>

3) Build your project as usual.

Solution 4

Some more information that might be useful to some people implementing Scott Hanselman's solution.

When I first set this up it would complain about not being able to resolve references to System.Core, etc. It is something to do with .NET 4 support. Including a /lib argument pointing to the .NET 4 Framework directory fixes it (in fact just include the $(MSBuildBinPath)).

/lib:$(MSBuildBinPath)

I then found that IlMerge would hang while merging. It was using a bit of CPU and a lot of RAM but wasn't outputting anything. I found the fix on stackoverflow of course.

/targetplatform:v4

I also found that some of the MSBuild properties used in Scott's blog article relied on executing MsBuild from the project's directory, so I tweaked them a bit.

I then moved the targets & ilmerge.exe to the tools folder of our source tree which required another small tweak to the paths...

I finally ended up with the following Exec element to replace the one in Scott's original article:

<Exec Command="&quot;$(MSBuildThisFileDirectory)Ilmerge.exe&quot; /lib:$(MSBuildBinPath) /targetplatform:v4 /out:@(MainAssembly) &quot;$(MSBuildProjectDirectory)\@(IntermediateAssembly)&quot; @(IlmergeAssemblies->'&quot;%(FullPath)&quot;', ' ')" /> 

UPDATE I also found Logic Labs answer about keeping the CopyLocal behaviour and just excluding ilMerged assemblies from CopyLocal essential if you are using Nuget packages. Otherwise you need to specify a /lib argument for each package directory of referenced assemblies that aren't being merged.

Solution 5

One issue I found with the article at: http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx.

If you have any references that you do not wish to ILMerge then the code in the article fails because it overrides the default CopyLocal behaviour to do nothing.

To fix this - Instead of:

<Target Name="_CopyFilesMarkedCopyLocal"/> 

Add this entry to the targets file instead (.NET 3.5 only) (to filter out the non-ilmerge copylocal files, and treat them as normal)

<Target Name="AfterResolveReferences">
    <Message Text="Filtering out ilmerge assemblies from ReferenceCopyLocalPaths" Importance="High" />
    <ItemGroup>
        <ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.IlMerge)'=='true'" />
    </ItemGroup>
</Target>
Share:
75,143

Related videos on Youtube

AMissico
Author by

AMissico

Done

Updated on July 05, 2022

Comments

  • AMissico
    AMissico almost 2 years

    I want to merge one .NET DLL assembly and one C# Class Library project referenced by a VB.NET Console Application project into one command-line console executable.

    I can do this with ILMerge from the command-line, but I want to integrate this merging of reference assemblies and projects into the Visual Studio project. From my reading, I understand that I can do this through a MSBuild Task or a Target and just add it to a C#/VB.NET Project file, but I can find no specific example since MSBuild is large topic. Moreover, I find some references that add the ILMerge command to the Post-build event.

    1. How do I integrate ILMerge into a Visual Studio (C#/VB.NET) project, which are just MSBuild projects, to merge all referenced assemblies (copy-local=true) into one assembly?

    2. How does this tie into a possible ILMerge.Targets file?

    3. Is it better to use the Post-build event?

  • AMissico
    AMissico about 14 years
    The article Mixing Languages in a Single Assembly in Visual Studio seamlessly with ILMerge and MSBuild at hanselman.com/blog/… is based on his blog and enhances the technique to allow you to selectively merge by setting ILMerge=True/False in the project file. The article is much more detailed then Jomo's blog entry.
  • AMissico
    AMissico about 14 years
    Easy to overlook his updated and more detailed blog entry, so I will reference it here clariusconsulting.net/blogs/kzu/archive/2009/02/23/….
  • Rohan West
    Rohan West about 14 years
    The article i referenced is more recent but less detailed, it has some interesting comments.
  • Logic Labs
    Logic Labs about 12 years
    There is an issue with this code, if you don't want to ILMerge all your references. See my answer
  • Jason Duffett
    Jason Duffett almost 12 years
    Oh yes! This is DEFINITELY needed if you are using Nuget, otherwise you'd have to specify the package contents folder of each referenced assembly that you are not merging. Thank you!
  • Eoin Campbell
    Eoin Campbell about 11 years
    It's a really pit this package isn't better maintained. I'd been using it but it's missing the ability to set TargetPlatform which is significant for .NET 4.5/.NET 4.0 compat.
  • Smith
    Smith about 11 years
    @davidlcardi how to i replace the targetfilename, dll_to_merge etc
  • Davide Icardi
    Davide Icardi about 11 years
    @Smith TargetFileName is already a msbuild variable so you don't need to replace it. The LIB1_To_Merge.dll are fixed names. Probably with some more complex msbuild scripts you can find all referenced assemblies, but I don't know how to do it.
  • Smith
    Smith about 11 years
    @DavideIcardi pls hel me see if you can respond to this stackoverflow.com/questions/15588086/…
  • Syroot
    Syroot over 9 years
    A solution as it should be! +1
  • Alek Davis
    Alek Davis over 9 years
    This should be the accepted solution. Makes it really painless. Thank you!
  • Ethenyl
    Ethenyl over 9 years
    Nice one :D Not perfect since it "forgot" to include extra files in the output directory but still good ^^
  • Remco
    Remco about 9 years
    Works very nice: include package, build and you are done! It is the on with an icon.
  • Haobo
    Haobo about 8 years
    Acturally I ended up with downloading the ILMerge.exe, adding it to source control and writing some command into Post-build Event
  • Javid
    Javid about 8 years
    I prefer this method a lot more than the most voted one. :)
  • MrDysprosium
    MrDysprosium over 7 years
    I can't find any documentation for MSBuild ILMerge, how do I use it? Does anyone have an example or instructions?
  • Holistic Developer
    Holistic Developer over 7 years
    @MrDysprosium See ILMerge.doc that is included with the ILMerge NuGet package. Since "MSBuild ILMerge task" has a dependency on ILMerge, after adding it I also have the ILMerge package. For example, I can find the doc at <solution>\packages\ILMerge.2.14.1208\tools\ILMerge.doc.
  • takrl
    takrl about 7 years
    @WernerCD Edited the answer to provide a fixed link via internet archive. And here's the second one.
  • Metoniem
    Metoniem almost 5 years
    This is way easier than I had ever imagined! Thanks :)
  • Jim
    Jim almost 5 years
    This is nice! No editing *.csproj files with this solution.
  • Steven Lemmens
    Steven Lemmens about 4 years
    Is there a way to also make this sign my assembly using my key file somehow?
  • Standard
    Standard almost 4 years
    Has this to be configured somewhere? I installed it by packages.config but it still doesn't work without the SSH dll I need.
  • Holistic Developer
    Holistic Developer almost 4 years
    @wernersbacher not sure what your other DLL is, but ILMerge will only handle managed code. If you have a native code dependency, you'd still have to ship that separately.
  • devonuto
    devonuto almost 3 years
    How do you exclude one of the PackageReferences from the ILMerge? Seems to have issues with Log4net, but I can't find how to not package that one up?