Search code examples
tfsbuildilmergebuild-definition

How do I use ILMerge with TFS Builds Scripts?


I want to use ILMerge to merge a number of .dlls as part of the build definition/script. How do I do that with TFS 2015?


Solution

  • There are a number of ways to do this. Here's one way that I found to be simple and clean. I'm only going to cover the part about adding the ILMerge process to an existing build definition that is already successfully building and publishing your build artifacts. I'm also going to gloss over how ILMerge works and where to get it.

    Open your build definition for editing;

    • From Visual Studio 2015, in Team Explorer pane, navigate to the Home button.
    • Click on 'Builds' under Project
    • Find your build definition, right click on it and choose 'Edit Build Definition'

    Add a 'Command Line' build step

    • Assuming you are in edit mode for an existing build definition.
    • Click 'Build' tab if not already on it.
    • Click 'Add build step...'.
    • Click 'Utility' from the navbar at left.
    • Click 'Add' button next to 'Command Line' and close the dialog.

    • Note: Only click it once. Gets added but dialog stays open.

    • In the 'Tool' input, specify: $(Build.ArtifactStagingDirectory)\ILMerge.exe

    • Note: You may need to customize this path depending on where you are getting ILMerge from. See 'Referencing ILMerge' section down below.

    • In the 'Arguments' input, specify something like this example which merges five files into one and names the merged file after the build definition name which is optional:
      
      /lib:"C:\Windows\Microsoft.NET\Framework\v4.0.30319" /out:"\\SomeServer\BuildDrops\Builds\$(Build.DefinitionName)\$(Build.BuildNumber)\$(Build.DefinitionName).dll" /log:"\\SomeServer\BuildDrops\Builds\$(Build.DefinitionName)\$(Build.BuildNumber)\MergeLog.txt" $(Build.ArtifactStagingDirectory)\ABC.Services.dll $(Build.ArtifactStagingDirectory)\ABC.Auth.dll $(Build.ArtifactStagingDirectory)\ABC.Lib.dll $(Build.ArtifactStagingDirectory)\ABC.Content.dll $(Build.ArtifactStagingDirectory)\ABC.Test.dll /targetplatform:v4`
      

    Referencing ILMerge - Note: There are a number of ways you can do this so I'll just note a few.

    1. Copy ILMerge.exe down from the network at build time. (Recommended)

    • Click 'Add build step...'.
    • Click 'Utility' from left side navbar.
    • Click the 'Add' button next to 'Copy Files'.
    • Click and drag the "Copy Files' build step so it comes before the 'Command Line' we added.
    • Click the 'Copy Files' build step so we can edit it.
    • In 'Source Folder', specify the network folder where ILMerge.exe can be found.
    • In 'Contents', specify ILMerge.exe as the Contents unless you have more files to copy down. It's a wildcard search and specifying ILMerge.exe is an exact match search.
    • In 'Target Folder', specify $(Build.ArtifactStagingDirectory)

    2. Copy ILMerge to the build client machine and create an environment variable on the build clients for it.

    • I haven't tested this because I don't have direct access to the build clients which is why I chose an alternative path.

    3. You might be able to figure out a way to reference ILMerge directly from a network share.

    • Your mileage may vary and so will your relative file paths in the arguments you need to pass in.

    4. Add a copy of ILMerge to one of your projects.

    • It really doesn't matter where you put it in the solution because ILMerge.exe runs post-build.

    • If you place ILMerge.exe anywhere besides the root of one of your projects, you may need to update the Tool path in the 'Command Line' build step AND you would need to update any path in the arguments to reflect the working directory of ILMerge.exe. You'll likely be looking at needing some '..\..\' in your paths. I didn't test that.*


    Argument Notes:

    • /lib:"C:\Windows\Microsoft.NET\Framework\v4.0.30319"

      • You may need to leave this out or change it. We had some legacy projects that we needed to support. If you need more info on this particular subject, check this out: using ILMerge with .NET 4 libraries
    • /out:"\SomeServer\BuildDrops\Builds\$(Build.DefinitionName)\$(Build.BuildNumber)\$(Build.DefinitionName).dll"

      • You should be able to find your path by going into the 'Publish Build Artifacts' step (above the Command Line build step), and reviewing the 'Path:' field. You should be able to copy over that entire path and then add your desired filename and extension to the end.
    • log:"\SomeServer\BuildDrops\Builds\$(Build.DefinitionName)\$(Build.BuildNumber)\MergeLog.txt"

      • Including the log is optional. I'd only use it for troubleshooting ILMerge issues and then remove it when everything is working.
    • $(Build.ArtifactStagingDirectory)\ABC.Auth.dll

      • These are the .dll's you plan to merge together.
      • They are just separated by a space.
      • The Build.ArtifactStagingDirectory is the location where your built files get copied to on the build client machine. This is a temporary staging location before they get published to the official drop path.
    • /targetplatform:v4

      • Again, you may need to adjust this.

    Additional Resources: https://www.visualstudio.com/en-us/docs/build/steps/utility/command-line