ILMerge在MSBuild與ILMerge在批處理文件中運行


ILMerge

ILMerge是一個將多個.NET程序集合並到一個程序集中的實用程序。它可以免費使用,並以NuGet包的形式提供

如果您在使用它時遇到任何問題,請與我們聯系。(mbarnett at microsoft dot com)。但首先嘗試閱讀文檔

ILMerge接受一組輸入程序集並將它們合並到一個目標程序集中。輸入程序集列表中的第一個程序集是主程序集。當主程序集是可執行文件時,目標程序集將創建為具有與主程序集相同的入口點的可執行文件。此外,如果主程序集具有強名稱,並且提供了.snk文件,則使用指定的鍵重新簽名目標程序集,以使其具有強名稱。

ILMerge打包為控制台應用程序。但它的所有功能也可以通過編程方式獲得。

有幾個選項可以控制ILMerge的行為。有關詳細信息,請參閱該工具隨附的文檔。

目前的版本是2.14.1208(2014年12月8日創建)。注意:不再有在v1.1運行時中運行的ILMerge版本。

ILMerge在v4.0 .NET運行時中運行,但它也能夠使用它來合並來自其他框架版本的程序集/targetplatformoption。請參閱文檔。(但是,它只能為v2(及更高版本)程序集合並PDB文件。)

目前,ILMerge僅適用於基於Windows的平台。它還不支持Rotor或Mono。

如果使用ASP.NET v2.0,則它提供了一個工具(基於ILMerge)來組合在預編譯期間創建的程序集。您可以從ASP.NET網站獲取更多詳細信息。

安裝

ilmerge NuGet頁面所述,該軟件包可以從Visual Studio環境安裝。在Solution Explorer視圖中展開項目容器。右鍵單擊references並選擇Manage NuGet Packages

NuGet參考設置

確保Package source設置為nuget.org

NuGet包源

接下來,單擊Tools - NuGet Package Manager - Package Manager Console。確保Package source也設置為nuget.org

NuGet Pakage Manager Console source.PNG

要為項目安裝,請使用Install-Package命令:

Install-Package ilmerge -Version 3.0.21

用法

MSBuild

ILMerge可以使用NuGet包在MSBuild中使用:

< Project  Sdk = “ Microsoft.NET.Sdk ” >

  < ItemGroup >
    < PackageReference  Include = “ ILMerge ”  Version = “ 2.15.0 ” />
  </ ItemGroup >

  < Target  Name = “ ILMerge ” >
     <! - ILMergePath屬性指向ILMerge.exe控制台應用程序的位置- > 
    < Exec  Command = “ $(ILMergeConsolePath)/out:Merged.dll File1.dll File2.dll ” />
  </ Target >

</ Project >

編輯項目.csproj.vbproj文件(在相應<Project> .. </Project>標記內,通常在文件末尾。如果編譯特定目標,請使用顯式目錄,例如Bin\x64\Release

<ItemGroup>
    <PackageReference Include="ILMerge" Version="2.15.0" />
  </ItemGroup>

  <Target Name="ILMerge">
    <!-- the ILMergePath property points to the location of ILMerge.exe console application -->
    <Exec Command="$(ILMergeConsolePath) Bin\x64\Release\myapp.exe  /out:myapp.exe Bin\x64\Release\File1.dll Bin\x64\Release\File2.dll Bin\x64\Release\File3.dll " />
  </Target>

雖然XML文件中通常會忽略空格,但在這種情況下,確切的文本將作為DOS命令處理,因此為了提高可讀性,請使用克拉^(shift 6)行擴展器:

<ItemGroup>
    <PackageReference Include="ILMerge" Version="2.15.0" />
  </ItemGroup>

  <Target Name="ILMerge">
    <!-- the ILMergePath property points to the location of ILMerge.exe console application -->
    <Exec Command="$(ILMergeConsolePath) Bin\x64\Release\myapp.exe ^
    /out:myapp.exe ^
    Bin\x64\Release\File1.dll ^
    Bin\x64\Release\File2.dll ^ 
    Bin\x64\Release\File3.dll " />
  </Target>

DOS dir / b選項可以幫助列出所有依賴項:

dir bin\x64\Debug\*.dll /b

從Visual Studio Developer命令提示符:

#下載/安裝軟件包參考
msbuild / t:恢復

#運行ILMerge目標
msbuild / t:ILMerge

ILMerge在批處理文件中運行:

此處不需要Visual Studio Developer命令提示符,因為msbuild未使用。

@echo off

:: this script needs https://www.nuget.org/packages/ilmerge

:: set your target executable name (typically [projectname].exe)
SET APP_NAME=myapp.exe

:: Set build, used for directory. Typically Release or Debug
SET ILMERGE_BUILD=Debug

:: Set platform, typically x64
SET ILMERGE_PLATFORM=x64

:: set your NuGet ILMerge Version, this is the number from the package manager install, for example:
:: PM> Install-Package ilmerge -Version 3.0.21
:: to confirm it is installed for a given project, see the packages.config file
SET ILMERGE_VERSION=3.0.21

:: the full ILMerge should be found here:
SET ILMERGE_PATH=%USERPROFILE%\.nuget\packages\ilmerge\%ILMERGE_VERSION%\tools\net452
:: dir "%ILMERGE_PATH%"\ILMerge.exe

echo Merging %APP_NAME% ...

:: add project DLL's starting with replacing the FirstLib with this project's DLL
"%ILMERGE_PATH%"\ILMerge.exe Bin\x64\Release\%APP_NAME%  ^
  /lib:Bin\%ILMERGE_PLATFORM%\%ILMERGE_BUILD%\ ^
  /out:%APP_NAME% ^
  FirstLib.dll ^
  mylib1.dll ^
  Microsoft.lib2.dll ^
  SomeOtherLib.dll ^
  \otherlibdir\otherlib.dll 


:Done
dir %APP_NAME%
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM