How to Integrate ILMerge into Visual Studio Build Process to Merge Assemblies?
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) -> $(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=""$(MSBuildThisFileDirectory)Ilmerge.exe" /lib:$(MSBuildBinPath) /targetplatform:v4 /out:@(MainAssembly) "$(MSBuildProjectDirectory)\@(IntermediateAssembly)" @(IlmergeAssemblies->'"%(FullPath)"', ' ')" />
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>
Related videos on Youtube
Comments
-
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.
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?
How does this tie into a possible ILMerge.Targets file?
Is it better to use the Post-build event?
-
Zain Ali over 10 yearsYou can also use "Post Build String" to do that as mentioned [Here][1] [1]: stackoverflow.com/questions/2961357/…
-
AMissico about 14 yearsThe 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 about 14 yearsEasy 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 about 14 yearsThe article i referenced is more recent but less detailed, it has some interesting comments.
-
Logic Labs about 12 yearsThere is an issue with this code, if you don't want to ILMerge all your references. See my answer
-
Jason Duffett almost 12 yearsOh 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 about 11 yearsIt'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 about 11 years@davidlcardi how to i replace the targetfilename, dll_to_merge etc
-
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 about 11 years@DavideIcardi pls hel me see if you can respond to this stackoverflow.com/questions/15588086/…
-
Syroot over 9 yearsA solution as it should be! +1
-
Alek Davis over 9 yearsThis should be the accepted solution. Makes it really painless. Thank you!
-
Ethenyl over 9 yearsNice one :D Not perfect since it "forgot" to include extra files in the output directory but still good ^^
-
Remco about 9 yearsWorks very nice: include package, build and you are done! It is the on with an icon.
-
Haobo about 8 yearsActurally I ended up with downloading the ILMerge.exe, adding it to source control and writing some command into Post-build Event
-
Javid about 8 yearsI prefer this method a lot more than the most voted one. :)
-
MrDysprosium over 7 yearsI can't find any documentation for MSBuild ILMerge, how do I use it? Does anyone have an example or instructions?
-
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 about 7 years@WernerCD Edited the answer to provide a fixed link via internet archive. And here's the second one.
-
Metoniem almost 5 yearsThis is way easier than I had ever imagined! Thanks :)
-
Jim almost 5 yearsThis is nice! No editing
*.csproj
files with this solution. -
Steven Lemmens about 4 yearsIs there a way to also make this sign my assembly using my key file somehow?
-
Standard almost 4 yearsHas 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 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 almost 3 yearsHow 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?