My favorites | Sign in
Project Logo
                
Search
for
Updated Oct 02, 2007 by neoeinstein
Labels: Phase-Deploy, Featured
HowToUse  
How to use ILMerge Tasks in your MSBuild or NAnt build file

Introduction

ILMerge Tasks is a sibling pair of projects that allow you to access ILMerge during your build, enabling you to incorporate referenced .NET assemblies into a combined assembly, removing the need to distribute external assemblies. For example, suppose your program makes use of a zip library, but you want to distribute a single executable. ILMerge allows you to combine your executable and the zip library into a single assembly. You can then distribute that assembly without requiring the end-user to have a copy of the zip library.

Using in a Build

In the case of both MSBuild and NAnt, referencing the ILMerge task is relatively simple, requiring the ILMerge.*.Tasks.dll file to be available and adding a line to the build file.

NAnt

To use the ILMerge task in NAnt, you will need to add the following line to the build file:

<loadtasks assembly="NAnt.NAnt.Tasks.dll" />

The syntax to use ILMerge is then as follows:

<ilmerge outputfile="Combined.Assembly.exe">
    <inputassemblies>
        <include name="Primary.Assembly.exe" />
        <include name="ZipLib.dll" />
    </inputassemblies>
</ilmerge>

MSBuild

To use the ILMerge task in MSBuild, you will need to add the following line to the build file:

<UsingTask TaskName="ILMerge.MSBuild.Tasks.ILMerge"
  AssemblyFile="ILMerge.MSBuild.Tasks.ILMerge"/>

The syntax to use ILMerge is then as follows:

<ItemGroup>
    <MergeAsm Include="Primary.Assembly.exe" />
    <MergeAsm Include="ZipLib.dll" />
</ItemGroup>

<ILMerge InputAssemblies="@(MergeAsm)" OutputFile="Combined.Assembly.exe" />

Common Attributes

The ILMerge task attributes used in both the NAnt and MSBuild tasks are the same, with the caveat that the NAnt schema prefers all-lowercase attribute names and MSBuild prefers PascalCase attribute names. The names below use the MSBuild schema. To use them in NAnt, just lowercase the attribute name.

AttributeFile

The assembly from which to load all assembly-level attributes. Mutually exclusive with CopyAttributes (If both are set, an error will be logged).

Closed

CopyAttributes

DebugInfo

When set, any .pdb files that are found for the input assemblies are preserved into a .pdb file for the target assembly.

ExcludeFile

Has no effect if Internalize == false

Internalize

LibraryPath

Log

LogFile

Has no effect if Log == false

OutputFile

KeyFile

InputAssemblies

Target

Valid values are (lowercased in NAnt): WinExe, Exe, Library, SameAsPrimaryAssembly


Comment by JezzaTinks, Jan 03, 2008

To get the ILMerge MSBuild task to work I had to change your example to:

<UsingTask TaskName="ILMerge.NAnt.Tasks.ILMerge" AssemblyFile="C:\Program Files\Microsoft\ILMerge\ILMerge.MSBuild.Tasks.dll" />

<ItemGroup>
<MergeAsm Include="$(OutputPath)$(TargetFileName)" /> <MergeAsm Include="$(OutputPath)ZipLib.dll" />
</ItemGroup>
<Target Name="ILMerge">
<ILMerge.NAnt.Tasks.ILMerge InputAssemblies?="@(MergeAsm?)" OutputFile?="$(OutputPath?)Combined.Assembly.exe" TargetKind?="SameAsPrimaryAssembly?" />
</Target>
Comment by krouskop, Apr 02, 2008

I don't believe your NAnt example is correct.

After experimenting, I think a correct example would be:

<loadtasks assembly="ILMerge.NAnt.Tasks.dll" />
<ilmerge outputfile="Combined.Assembly.exe">
    <assemblies>
        <include name="Primary.Assembly.exe" />
        <include name="ZipLib.dll" />
    </assemblies>
</ilmerge>
Comment by krouskop, Apr 02, 2008

As a follow up to my previous comment, is it possible to tell the NAnt task where it can locate the ILMerge.exe?

I was unable to fully get the NAnt task to work because I couldn't figure out how to do so. Perhaps the NAnt task could try loading the .exe from the default install path for ILMerge (C:\Program Files\Microsoft\ILMerge)

Comment by bryanbcook, Apr 24, 2008

I downloaded ILMerge from the Microsoft site (version 2.8.422.0) -- the NAnt library is tied to 2.0.7.228.

I guess I have to download the source and recompile. You should consider bundling ILMerge.exe with your download.

Comment by azamsharp, Jul 24, 2008

Nope did not work with Nant! Said something about could not load the ILMerge.NAnt.Tasks.dll assembly!

Comment by esuyer, Aug 02, 2009

I had the same problem. I think it's because my tools\ directory and consequently ilmerge.exe is a network share, and .net is having a hard time finding this reference. I wrote my own version of ILMergeTask which extends ExternalProgramBase?. So you can pass it the location of the ilmerge.exe.

http://esuyer.blogspot.com/2009/07/custom-nant-ilmerge-task-no-assembly.html http://esuyer.blogspot.com/2009/07/custom-nant-ilmerge-task-no-assembly.html

No assembly required. You can paste this right into your NAnt script.


Sign in to add a comment
Hosted by Google Code