
Collect a Visual Studio project into a single file using ILMerge

Example
Relatively speaking after:
compile App \ App.csproj dir App \ bin \ ReleaseIt turns out:
App.exe dep1.dll dep2.dllWe only need one self-sufficient
App.exeThat is, containing in itself
dep1.dll
and dep2.dll
on Habré there is already a solution with embedding dependencies in resources, here I will show how to do this using ILMerge and Post Build Event in Visual Studio.
Source code
- github: github.com/gzvulon/ILMerge-Example
- Solution archive: github.com/gzvulon/ILMerge-Example/zipball/v1.0.0
- bat file: github.com/gzvulon/ILMerge-Example/blob/master/ILMerge/merge_all.bat
Instruments
ILMerge - A program from Microsoft Research, which actually has the required functionality.
merge_all.bat - we will use it in the Post-build event.
Training
Download and install ILMerge .
Put
%PROGRAMFILES%\ILMerge\ILMerge.exe
in the folder ${SolutionDir}ILMerge\
In the same place, create the file merge_all.bat
Add a line to [Project-> Properties-> Build Events-> Post-build event]:
"$(SolutionDir)\ILMerge\merge_all.bat" "$(SolutionDir)" "$(TargetPath)" $(ConfigurationName)
Contents merge_all.bat
All assemblies from $ output will merge into one. If Debug configuration is in $ output \ Output, if Release then as a result there will be only one file in $ output. Information about how the merger went and what problems were written in Visual Studio Output. Comments explain what happens inside. For example, in this case, the .NET 4 platform is selected.
@ECHO OFF
rem # set .NET version and output folder name
set net="v4, C:\Windows\Microsoft.NET\Framework\v4.0.30319"
set output=Output
rem # process arguments
set ILMergeSolution=%1ILMerge\ILMerge.exe
rem # determine programm files of x86 for 32 and 64 Platform
IF EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES(x86)%
IF NOT EXIST "%PROGRAMFILES(x86)%" set prorgammFiles=%PROGRAMFILES%
rem # if ILMerge.exe not in the $(SolutionDir)ILMerge\
rem # then try to use installed in prorgammFiles
IF EXIST %ILMergeSolution% set ILMerge="%ILMergeSolution%"
IF NOT EXIST %ILMergeSolution% set ILMerge=%prorgammFiles%\Microsoft\ILMerge\ILMerge.exe
set target_path=%2
set target_file=%~nx2
set target_dir=%~dp2
set ConfigurationName=%3
rem # set output path and result file path
set outdir=%target_dir%%output%
set result=%outdir%\%target_file%
rem # print info
@echo Start %ConfigurationName% Merging %target_file%.
@echo Target: %target_path%
@echo target_dir: %target_dir%
@echo Config: %ConfigurationName%
rem # recreate outdir
IF EXIST "%outdir%" rmdir /S /Q "%outdir%"
md "%outdir%"
rem # run merge cmd
@echo Merging: '"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"'
"%ILMerge%" /wildcards /targetplatform:%net% /out:"%result%" %target_path% "%target_dir%*.dll"
rem # if succeded
IF %ErrorLevel% EQU 0 (
rem # clear real output folder and put there result assembly
IF %ConfigurationName%==Release (
del %target_dir%*.*
del %target_dir%*.dll
del %target_dir%*.pdb
del %target_dir%*.xml
del %target_dir%*.*
copy %result% %target_dir%
rmdir /S /Q %outdir%
set result=%target_path%
@echo Result: %target_file% "-> %target_path%"
) ELSE (
@echo Result: %target_file% "-> %result%" )
set status=succeded
set errlvl=0
) ELSE (
set status=failed
set errlvl=1
)
@echo Merge %status%
exit %errlvl%
UPD:
License
From ILMerge :
Commercial use permitted:
The language of ILMerge's license has raised many questions. In a nutshell: commercial use is permitted, redistribution is not. Read the license carefully for full details since I am not (nor do I wish to be!) A lawyer.
That is, you can embed it in the assembly process, but you can’t distribute it with your product.
The open question remained about the license of the merged dlls.
Minuses
- beliakov : ILMerge memory eats like a hippo ... On a large number of assemblies
- braindamaged : ignores the key, which causes it, in turn, to ignore the assemblies and namespaces that are in the dependencies.
Similar tools
- aspnet_merge For ASP.NET
- NGen Create a local cache with dll
Related Materials
- Embedding in resources - habrahabr.ru/blogs/personal/67836
- As Visual Studio Project Target - http: //www.hanselman.com/blog/MixingLanguag ...
- Another Target - http://blogs.clariusconsulting.net/kzu/how-to-merge-your-referenced-assemblies-into-the-output-assembly-for-improved-usability/
- Best practices - stackoverflow.com/questions/9376/ilmerge-best-practices
- Simplest .bat - peyotest.blogspot.com/2010/03/using-ilmerge-in-post-build-step.html
- Advanced .bat - www.tanguay.info/web2008/codeExample.php?id=671