ILMerging assemblies with Psake / Powershell


On NBehave I am migrating the existing build framework from Nant over to Psake, and am thoroughly enjoying the ability to debug and step through my build scripts.

We needed to merge some of our output assemblies before packaging, and this was extremely easy in POSH:

function ilmerge($key, $directory, $name, $assemblies, $extension)
    # Create a new temp directory as we cannot overwrite the main assembly being merged.
    new-item -path $directory -name "temp_merge" -type directory -ErrorAction SilentlyContinue
    # Unfortuntately we need to tell ILMerge its merging CLR 4 assemblies.
    if($framework -eq "4.0")
        Exec { tools\ilmerge\ilmerge.exe /keyfile:$key /out:"$directory\temp_merge\$name.$extension" "$directory\$name.$extension" $assemblies /targetplatform:"v4,$env:ProgramFiles\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" }
        Exec { tools\ilmerge\ilmerge.exe /keyfile:$key /out:"$directory\temp_merge\$name.$extension" "$directory\$name.$extension" $assemblies }
    Get-ChildItem "$directory\temp_merge\**" -Include *.dll, *.pdb | Copy-Item -Destination $directory
    Remove-Item "$directory\temp_merge" -Recurse -ErrorAction SilentlyContinue

The parameters are straightforward and as you’d expect, here’s an example usage:

$key = "$solution_dir\NBehave.snk"
$directory = "$build_dir\dist\v$framework"
$name = "NBehave.Narrator.Framework"

$assemblies = @("$directory\gherkin.dll", 

ilmerge $key $directory $name $assemblies "dll"


The extension should be “.dll” or “.exe” depending on what the main artifact your trying to merge is.

Older Posts