When & How to use ILMerge with Visual Studio Project / Solution

后端 未结 1 491
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-06 09:52

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

  • Company.Data
    • Company.Data.LinqToSql
相关标签:
1条回答
  • 2021-02-06 10:53

    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>
    
    0 讨论(0)
提交回复
热议问题