我想将 VB.NET 控制台应用程序项目引用的一个 .NET DLL 程序集和一个 C# 类库项目合并到一个命令行控制台可执行文件中。
我可以从命令行使用 ILMerge 来完成此操作,但我想将参考程序集和项目的这种合并集成到 Visual Studio 项目中。从我的阅读中,我了解到我可以通过 MSBuild 任务或目标来完成此操作,并将其添加到 C#/VB.NET 项目文件中,但我找不到具体的示例,因为 MSBuild 是一个很大的主题。此外,我找到了一些将 ILMerge 命令添加到 Post-build 事件的参考资料。
- 将 ILMerge 集成到 Visual Studio (C#/VB.NET) 项目(这些项目只是 MSBuild 项目)中,以将所有引用的程序集 (copy-local=true) 合并到一个程序集中?
-
这如何与可能的 ILMerge.Targets 文件相关联?
-
使用构建后事件更好吗?
I want to merge one .NET DLL assembly and one C# Class Library project referenced by a VB.NET Console Application project into one command-line console executable.
I can do this with ILMerge from the command-line, but I want to integrate this merging of reference assemblies and projects into the Visual Studio project. From my reading, I understand that I can do this through a MSBuild Task or a Target and just add it to a C#/VB.NET Project file, but I can find no specific example since MSBuild is large topic. Moreover, I find some references that add the ILMerge command to the Post-build event.
-
How do I integrate ILMerge into a Visual Studio (C#/VB.NET) project, which are just MSBuild projects, to merge all referenced assemblies (copy-local=true) into one assembly?
-
How does this tie into a possible ILMerge.Targets file?
-
Is it better to use the Post-build event?
发布评论
评论(9)
“MSBuild ILMerge 任务”(或
MSBuild.ILMerge.Task
)NuGet 包使这个过程变得非常简单。它默认将任何“复制本地”引用合并到主程序集中。注意:虽然这些包具有相似的名称,但这个包与 Davide Icardi 在他的 ILMerge.MSBuild.Tasks 不同。 com/a/13446006/137646">答案。我在这里建议的版本于 2014 年 8 月首次发布。
The "MSBuild ILMerge task" (or
MSBuild.ILMerge.Task
) NuGet package makes this process quite simple. It defaults to merging any "copy local" references into your main assembly.Note: Although the packages have similar names, this one is different from
ILMerge.MSBuild.Tasks
that Davide Icardi mentioned in his answer. The one I'm suggesting here was first published in August 2014.这里有一个替代解决方案:
1)从 nuget 安装 ILMerge.MSBuild.Tasks 包
2) 通过添加以下代码编辑要合并的项目的 *.csproj 文件:
3) 像往常一样构建项目。
Here an alternative solution:
1) Install ILMerge.MSBuild.Tasks package from nuget
2) Edit the *.csproj file of the project that you want to merge by adding the code below:
3) Build your project as usual.
更多信息可能对实施 Scott Hanselman 的解决方案的人有用。
当我第一次设置它时,它会抱怨无法解析对 System.Core 等的引用。
这与 .NET 4 支持有关。包含指向 .NET 4 Framework 目录的 /lib 参数可以修复此问题(实际上只需包含 $(MSBuildBinPath))。
/lib:$(MSBuildBinPath)
然后我发现 IlMerge 在合并时会挂起。它使用了一些 CPU 和大量 RAM,但没有输出任何内容。我当然是在 stackoverflow 上找到了修复。
/targetplatform:v4
我还发现 Scott 博客文章中使用的一些 MSBuild 属性依赖于从项目目录执行 MsBuild,因此我对它们进行了一些调整。
然后我移动了目标并移动了目标。 ilmerge.exe 到我们源代码树的工具文件夹中,这需要对路径进行另一次小调整...
我最终得到以下 Exec 元素来替换 Scott 原始文章中的元素:
更新
我还找到了 Logic Labs 答案,如果您使用 Nuget 包,则有关保留 CopyLocal 行为并从 CopyLocal 中排除 ilMerged 程序集至关重要。否则,您需要为未合并的引用程序集的每个包目录指定 /lib 参数。
Some more information that might be useful to some people implementing Scott Hanselman's solution.
When I first set this up it would complain about not being able to resolve references to System.Core, etc.
It is something to do with .NET 4 support. Including a /lib argument pointing to the .NET 4 Framework directory fixes it (in fact just include the $(MSBuildBinPath)).
/lib:$(MSBuildBinPath)
I then found that IlMerge would hang while merging. It was using a bit of CPU and a lot of RAM but wasn't outputting anything. I found the fix on stackoverflow of course.
/targetplatform:v4
I also found that some of the MSBuild properties used in Scott's blog article relied on executing MsBuild from the project's directory, so I tweaked them a bit.
I then moved the targets & ilmerge.exe to the tools folder of our source tree which required another small tweak to the paths...
I finally ended up with the following Exec element to replace the one in Scott's original article:
UPDATE
I also found Logic Labs answer about keeping the CopyLocal behaviour and just excluding ilMerged assemblies from CopyLocal essential if you are using Nuget packages. Otherwise you need to specify a /lib argument for each package directory of referenced assemblies that aren't being merged.
文章在 Visual Studio 中与 ILMerge 和 MSBuild 无缝混合语言位于 http://www.hanselman.com/blog/MishingLanguagesInASingleAssemblyInVisualStudioSeamlessWithILMergeAndMSBuild.aspx 演示如何在 Visual Studio 项目中使用 ILMerge 和 MSBuild。
The article Mixing Languages in a Single Assembly in Visual Studio seamlessly with ILMerge and MSBuild at http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx demonstrates how to use ILMerge and MSBuild within a Visual Studio Project.
我在这篇文章中发现了一个问题:http://www.hanselman.com/blog/MishingLanguagesInASingleAssemblyInVisualStudioSeamlessWithILMergeAndMSBuild。 .aspx.
如果您有任何不希望 ILMerge 的引用,则本文中的代码会失败,因为它会覆盖默认的 CopyLocal 行为而不执行任何操作。
要解决此问题 - 而不是:
将此条目添加到目标文件中(仅限 .NET 3.5)(以过滤掉非 ilmerge copylocal 文件,并将它们视为正常文件)
One issue I found with the article at: http://www.hanselman.com/blog/MixingLanguagesInASingleAssemblyInVisualStudioSeamlesslyWithILMergeAndMSBuild.aspx.
If you have any references that you do not wish to ILMerge then the code in the article fails because it overrides the default CopyLocal behaviour to do nothing.
To fix this - Instead of:
Add this entry to the targets file instead (.NET 3.5 only) (to filter out the non-ilmerge copylocal files, and treat them as normal)
这是一个很棒的文章将向您展示如何将引用的程序集合并到输出程序集中。它准确地展示了如何使用 msbuild 合并程序集。
This is a great article that will show you how to merge your referenced assemblies into the output assembly. It shows exactly how to merge assemblies using msbuild.
我的 2 美分 - 我选择了 @Jason 的回复,并使其适用于我的解决方案,我想在 bin/Debug 文件夹中生成 *.exe,并将所有 *.dll 放在同一文件夹中。
注意:此解决方案显然已硬编码到 ILMerge nuget 包版本中。如果您有一些改进建议,请告诉我。
My 2 cents - I picked up @Jason's response and made it work for my solution where I wanted to generate the *.exe in the bin/Debug folder with all *.dlls inside the same folder.
Note: This solution is obviously hardcoded into the ILMerge nuget package version. Please let me know if you have some suggestions to improve.
通过添加以下代码来编辑要合并的项目的 *.csproj 文件:
注释:
$(SolutionDir)packages\ILMerge.3.0.29\tools\net452\ILMerge .exe
与您拥有ILMerge.exe
的任何路径。Condition
,以便在调试时也进行合并,但调试器可能不会/internalize:"$(MSBuildProjectPath)ilmerge.exclude"
Edit the *.csproj file of the project that you want to merge by adding the code below:
Notes:
$(SolutionDir)packages\ILMerge.3.0.29\tools\net452\ILMerge.exe
with whatever path you have theILMerge.exe
in.Condition
in the target to also merge on Debug but then the Debugger might not work/internalize:"$(MSBuildProjectPath)ilmerge.exclude"
看看乔莫的这篇文章。他有一个将 ILMerge 侵入 msbuild 系统的快速过程
Check out this article by Jomo. He has a quick process to hack ILMerge into the msbuild system