How can I temporarily inactivate MSBuild.ILMerge.Task?
Asked Answered
K

1

5

I am using the NuGet package MSBuild.ILMerge.Taskin order to combine .NET external dlls to a single executable file. It works like a charm.

Now, when I try to edit while Debugging, Visual Studio says:

Changes are not allowed if the assembly has not been loaded

I guess it's the downside of having all the dependencies into a single .exe (I don't know about that, it's just a guess).

But anyway, it would make sense to have MSBuild.ILMerge.Task on Build, but inactive on Debug. Does anybody know how to achieve this?

Notice that when MSBuild.ILMerge.Task is installed through NuGet, when you hit "rebuild" it automatically creates the single .exe (letting the .config file outside, of course, plus a .pdb). You don't need to configure anything, and this why I am not able to guess the inner workings of the process and how to inactivate it.


For more information, I am writing a blog post about ILMerge. I am trying to document all how-to's, problems and caveats:

http://localcode.wikidot.com/merge-all-binary-files-dll-s-etc-into-a-single-exe


Edit1: I've noticed that the NuGet package automatically sets a .props file with the following info:

<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

I've started looking for DefaultTargets="Build" and a way to disable on debug, and I've found this post that seems to explain how.


Edit2:

If remove this lines from my .csproj, then it skips the ILMerge compilation correctly:

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.props'))" />
<Error Condition="!Exists('..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets'))" />
</Target>
<Import Project="..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets" Condition="Exists('..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets')" />  

It's something, but it looks a bit handcrafted and impractical for the moment...

Kunz answered 19/10, 2016 at 14:4 Comment(0)
I
16

You can choose to run ILMerge only for "Release" builds, and leave it out of "Debug" builds, in which case you can use the debugger as you normally would in Debug builds, but change to Release only when you're ready to merge your assemblies into a single executable.

You can do this by editing your .csproj file and changing this line:

<Import
  Project="..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets"
  Condition="Exists('..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets')" />

to:

<Import
  Project="..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets" 
  Condition="'$(Configuration)'=='Release' AND Exists('..\packages\MSBuild.ILMerge.Task.1.0.5\build\MSBuild.ILMerge.Task.targets')" />

the difference being inserting the '$(Configuration)'=='Release' AND to the Condition attribute.

Use the Debug / Release dropdown menu in your Visual Studio toolbar to toggle between build configurations. Or, alternatively, open Build > Configuration Manager and change the "Active solution configuration".

Islamize answered 22/10, 2016 at 19:16 Comment(2)
Incredibly useful for Nuget packages! You can use the debug configuration for all the development and release only when nuget pack-ing. I had a nasty build error when running unit tests and this Condition feature saved my day.Oidea
This was the only way to merge ClassLib Project A and its dependencies (for other solutions) and run project B in the same solution which had A as a project referenceAlvord

© 2022 - 2024 — McMap. All rights reserved.