-->

When & How to use ILMerge with Visual Studio Proje

2020-05-28 11:38发布

问题:

I'm developing a medium sized enterprise application. There are many projects / solutions to this. For example:

  • Company.Data
    • Company.Data.LinqToSql
  • Company.Entities (business objects)
  • Company.BLL

I then have some applications - for example a windows service: MyWindowsService.

When i deploy this (by creating a setup project) it installs a load of DLL's from the output of the above mentioned projects.

Is this where i should be using ILMerge? to create one assembly.... Company.dll for example?

How would i go about integrating this into my build process?

回答1:

The question ILMerge Best Practices has good info on why.

When I use ILMerge, I use it to build a single DLL, to simplify deployment.

As to How, I define a separate, custom VS project, "Converged.csproj" if you like. In that .csproj file I define a custom Compile target. It is boilerplate code, that performs an ILMerge on all the referenced assemblies for the project.

It looks like this:

<Target Name="Compile">
  <!-- Outputs="$(IntermediateOutputPath)$(TargetFileName)" -->
  <!-- Outputs="$(TargetPath)" -->
  <Message Text="Performing the Ilmerge." />
  <!-- in this CreateItem stanza, we collect all the DLLs for the referenced projects -->
  <CreateItem Include="@(_ResolvedProjectReferencePaths)">
    <Output TaskParameter="Include" ItemName="AssembliesToMerge" />
  </CreateItem>
  <!-- This weird bit of hieroglyphics is the assemblies to merge, quoted, and separated by spaces -->
  <!-- Example:  "c:\foo\project1\bin\Debug\ProjectOne.dll"   "c:\foo\project2\bin\Debug\ProjectTwo.dll"  -->
  <Message Text="AssembliesToMerge= @(AssembliesToMerge -> '&quot;%(Fullpath)&quot;', ' ')" />
  <!-- Message Text="TargetPath= $(TargetPath)" / -->
  <Message Text="TargetFileName= $(TargetFileName)" />
  <!-- produce the merged assembly - putting the output in the "IntermediateOutputPath" eg obj\Debug. -->
  <!-- it will be copied later by the CopyFilestoOutputDirectory task defined in Microsoft.Common.Targets -->

  <Error
     Text="ILMerge cannot be found. You need to download and install ILMerge in order to build DotNetZip."
     Condition="!Exists('$(ProgramFiles)\Microsoft\Ilmerge\Ilmerge.exe')" />

  <Exec Command="&quot;$(ProgramFiles)\Microsoft\Ilmerge\Ilmerge.exe&quot;  /t:library  /xmldocs /out:&quot;$(IntermediateOutputPath)$(TargetFileName)&quot;  @(AssembliesToMerge -> '&quot;%(Fullpath)&quot;', ' ') " />

  <!-- for some reason the XML doc file does not get copied automatically from obj\Debug to bin\Debug. -->
  <!-- we do it here explicitly. -->
  <Copy SourceFiles="$(IntermediateOutputPath)$(AssemblyName).XML" DestinationFolder="$(OutDir)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="$(OverwriteReadOnlyFiles)" />
</Target>