NetBeauty2:让你的.NET项目输出目录更清爽

在.NET项目开发中,随着项目复杂性的增加,依赖的dll文件也会逐渐增多。这往往导致输出目录混乱,不便于管理和部署。而NetBeauty2开源项目正是为了解决这一问题而生,它能够帮助开发者在独立发布.NET项目时,将.NET运行时和依赖的dll文件移动到指定的目录,从而让输出目录更加干净、清爽。

unsetunset1. NetBeauty2简介unsetunset

NetBeauty2是一个开源的.NET依赖库整理工具,它的主要作用是在.NET项目独立发布时,对输出目录进行整理和优化。通过NetBeauty2,开发者可以轻松地将.NET运行时和依赖的dll文件移动到指定的目录,使得项目的输出目录更加清晰、易于管理。

项目仓库地址:https://github.com/nulastudio/NetBeauty2[1]

下图为优化后输出目录(.NET运行时及引用依赖库移到libraries目录,目录名可配置):

321f623c02b4bba855f63be9e53483f3.png

下图为极限优化后输出目录(查看 `--hiddens`[2] 选项使用)

bf99da54912cfbb825e4d622c265ec76.png

再来对比下未使用前输出目录(震撼吧!.NET运行时及相关依赖库全放在了根输出目录,.NET Framework可以配置privatePath,.NET Core可没那么方便):

6822348af87c54cf341059e55873c1b6.png

unsetunset2. 支持情况unsetunset


NetBeauty 2[3]NetCoreBeauty[4]
支持框架.Net Framework .Net Core 3.0+.Net Core 2.0+
支持部署模式Framework-dependent deployment (FDD) Self-contained deployment (SCD) Framework-dependent executables (FDE)Self-contained deployment (SCD)
支持操作系统Allwin-x64 win-x86 win-arm64(.NET 6+) linux-x64 linux-arm linux-arm64 osx-x64 osx-arm64(.NET 6+)
Need Patched HostFXRNo Yes(if use patch)Yes
Minimum Structure~20 Files ~8 Files(if use patch)~8 Files
How It Works`STARTUP_HOOKS`[5] `AssemblyLoadContext.Resolving`[6] `AssemblyLoadContext.ResolvingUnmanagedDll`[7] + `patched libhostfxr`[8](if use patch) `additionalProbingPaths`[9](if use patch)`patched libhostfxr`[10] `additionalProbingPaths`[11]
Shared RuntimeYesPossible If Using patched libhostfxr Alone

unsetunset3. 如何使用?unsetunset

3.1. 准备工作

在你的.NET Core工程(需要发布的主工程)添加Nuget包:

dotnet add package nulastudio.NetBeauty

打开工程文件编辑(.csproj):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <BeautySharedRuntimeMode>False</BeautySharedRuntimeMode>
    <!-- beauty into sub-directory, default is libs, quote with "" if contains space  -->
    <BeautyLibsDir Condition="$(BeautySharedRuntimeMode) == 'True'">../libraries</BeautyLibsDir>
    <BeautyLibsDir Condition="$(BeautySharedRuntimeMode) != 'True'">./libraries</BeautyLibsDir>
    <!-- dlls that you don't want to be moved or can not be moved -->
    <!-- <BeautyExcludes>dll1.dll;lib*;...</BeautyExcludes> -->
    <!-- dlls that end users never needed, so hide them -->
    <!-- <BeautyHiddens>hostfxr;hostpolicy;*.deps.json;*.runtimeconfig*.json</BeautyHiddens> -->
    <!-- set to True if you want to disable -->
    <DisableBeauty>False</DisableBeauty>
    <!-- set to False if you want to beauty on build -->
    <BeautyOnPublishOnly>False</BeautyOnPublishOnly>
    <!-- DO NOT TOUCH THIS OPTION -->
    <BeautyNoRuntimeInfo>False</BeautyNoRuntimeInfo>
    <!-- set to True if you want to allow 3rd debuggers(like dnSpy) debugs the app -->
    <BeautyEnableDebugging>False</BeautyEnableDebugging>
    <!-- the patch can reduce the file count -->
    <!-- set to False if you want to disable -->
    <!-- SCD Mode Feature Only -->
    <BeautyUsePatch>True</BeautyUsePatch>
    <!-- App Entry Dll = BeautyDir + BeautyAppHostDir + BeautyAppHostEntry -->
    <!-- see https://github.com/nulastudio/NetBeauty2#customize-apphost for more details -->
    <!-- relative path based on AppHostDir -->
    <!-- .NET Core Non Single-File Only -->
    <!-- <BeautyAppHostEntry>bin/MyApp.dll</BeautyAppHostEntry> -->
    <!-- relative path based on BeautyDir -->
    <!-- .NET Core Non Single-File Only -->
    <!-- <BeautyAppHostDir>..</BeautyAppHostDir> -->
    <!-- <BeautyAfterTasks></BeautyAfterTasks> -->
    <!-- valid values: Error|Detail|Info -->
    <BeautyLogLevel>Info</BeautyLogLevel>
    <!-- set to a repo mirror if you have troble in connecting github -->
    <!-- <BeautyGitCDN>https://gitee.com/liesauer/HostFXRPatcher</BeautyGitCDN> -->
    <!-- <BeautyGitTree>master</BeautyGitTree> -->
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="nulastudio.NetBeauty" Version="2.1.4.4" />
  </ItemGroup>

</Project>

运行 dotnet build 或者 dotnet publish, 优化输出操作会自动完成。

3.2. 如果应用已经发布?

如果你的应用程序已经发布,可以这样使用(站长没试过,这可以做为发布后补偿措施):

Usage:
nbeauty2 [--loglevel=(Error|Detail|Info)] [--srmode] [--enabledebug] [--usepatch] [--hiddens=hiddenFiles] [--noruntimeinfo] [--roll-forward=<rollForward>] [--apphostentry=<appHostEntry>] [--apphostdir=<appHostDir>] <beautyDir> [<libsDir> [<excludes>]]

例如:

ncbeauty2 --usepatch --loglevel Detail --hiddens "hostfxr;hostpolicy;*.deps.json;*.runtimeconfig*.json" /path/to/publishDir libraries "dll1.dll;lib*;..."

3.3. 配置为.NET Core全局工具

dotnet tool install --global nulastudio.nbeauty

安装后在程序发布时会自动应用。

unsetunset4. 各种项目使用示例unsetunset

克隆仓库(https://github.com/nulastudio/NetBeauty2[12]),里面有各种模版项目使用示例:

127d18066a9589cc43d69126247e4aa1.jpeg
测试工程名说明
WPFTestWPF项目(Winform类似),默认.NET 5
WebAppTestRazorPages项目,默认.NET 6
NetFxTest.NET Framework WPF项目(.NET 4.x,Winform类似)
ChromelyTest引用了Chromely的.NET项目
AvaloniaTestAvalonia UI项目,默认.NET 5

小知识1

Chromely NuGet包是一个用于创建跨平台桌面应用的库,它提供了一个基于Chromium的浏览器控件。通过Chromely,开发者可以使用Web技术(如HTML、CSS和JavaScript)来构建桌面应用的用户界面,同时保留对本地系统资源的访问。

Chromely NuGet包提供了一套完整的API和工具,使得开发者可以轻松地将Web应用程序转换为桌面应用程序,而无需进行大量的代码重写或修改。它还支持各种插件和扩展,以便开发者可以根据需要添加额外的功能或定制现有的功能。

此外,Chromely还支持多种编程语言和框架,如C#、.NET Core、ASP.NET Core等,这使得开发者可以选择他们最熟悉的技术栈来构建应用程序。

小知识2

Avalonia UI是一个跨平台的.NET UI框架,它允许开发者使用XAML和C#语言创建可在多个平台上运行的应用程序,包括Windows、Linux、macOS、iOS、Android以及WebAssembly。Avalonia UI旨在帮助开发者构建漂亮、现代的图形用户界面(GUI)。它兼容所有支持.NET Standard 2.0的平台,使开发者能够从单个代码库创建适用于多个操作系统的原生应用程序。通过使用Avalonia UI,开发者可以充分利用.NET生态系统的强大功能,同时实现跨平台兼容性,降低开发成本并提高开发效率。

推荐开源控件库:irihitech/Semi.Avalonia[13],irihitech/Ursa.Avalonia[14]

unsetunset5. 总结unsetunset

林德熙大佬分享过类似的包NuGet Gallery | dotnetCampus.PublishFolderCleaner 3.11.1[15],但该库说明只在Windows发布支持,大家可以对比使用,原文链接:PublishFolderCleaner 让你的 dotnet 应用发布文件夹更加整洁 - lindexi - 博客园 (cnblogs.com)[16],再次给出本文介绍库NetBeauty2开源地址:

项目仓库地址:https://github.com/nulastudio/NetBeauty2[17]

参考:

  • nulastudio/NetBeauty2: Move a .NET Framework/.NET Core app runtime components and dependencies into a sub-directory and make it beauty. (github.com)[18]

  • 路遥工具箱 .NET 6.0 独立部署时优化目录结构-码农很忙 (coderbusy.com)[19]

  • PublishFolderCleaner 让你的 dotnet 应用发布文件夹更加整洁 - lindexi - 博客园 (cnblogs.com)[20]

参考资料

[1]

https://github.com/nulastudio/NetBeauty2: https://github.com/nulastudio/NetBeauty2

[2]

--hiddens: https://github.com/nulastudio/NetBeauty2#use-the-binary-application-if-your-project-has-already-been-published

[3]

NetBeauty 2: https://github.com/nulastudio/NetBeauty2

[4]

NetCoreBeauty: https://github.com/nulastudio/NetBeauty2/tree/v1

[5]

STARTUP_HOOKS: https://github.com/dotnet/runtime/blob/main/docs/design/features/host-startup-hook.md

[6]

AssemblyLoadContext.Resolving: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext.resolving?view=netcore-3.0

[7]

AssemblyLoadContext.ResolvingUnmanagedDll: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext.resolvingunmanageddll?view=netcore-3.0

[8]

patched libhostfxr: https://github.com/nulastudio/HostFXRPatcher

[9]

additionalProbingPaths: https://github.com/dotnet/toolset/blob/master/Documentation/specs/runtime-configuration-file.md#runtimeoptions-section-runtimeconfigjson

[10]

patched libhostfxr: https://github.com/nulastudio/HostFXRPatcher

[11]

additionalProbingPaths: https://github.com/dotnet/toolset/blob/master/Documentation/specs/runtime-configuration-file.md#runtimeoptions-section-runtimeconfigjson

[12]

https://github.com/nulastudio/NetBeauty2: https://github.com/nulastudio/NetBeauty2

[13]

irihitech/Semi.Avalonia: https://github.com/irihitech/Semi.Avalonia

[14]

irihitech/Ursa.Avalonia: https://github.com/irihitech/Ursa.Avalonia

[15]

NuGet Gallery | dotnetCampus.PublishFolderCleaner 3.11.1: https://www.nuget.org/packages/dotnetCampus.PublishFolderCleaner

[16]

PublishFolderCleaner 让你的 dotnet 应用发布文件夹更加整洁 - lindexi - 博客园 (cnblogs.com): https://www.cnblogs.com/lindexi/p/15423277.html

[17]

https://github.com/nulastudio/NetBeauty2: https://github.com/nulastudio/NetBeauty2

[18]

nulastudio/NetBeauty2: Move a .NET Framework/.NET Core app runtime components and dependencies into a sub-directory and make it beauty. (github.com): https://github.com/nulastudio/NetBeauty2

[19]

路遥工具箱 .NET 6.0 独立部署时优化目录结构-码农很忙 (coderbusy.com): https://www.coderbusy.com/archives/2301.html

[20]

PublishFolderCleaner 让你的 dotnet 应用发布文件夹更加整洁 - lindexi - 博客园 (cnblogs.com): https://www.cnblogs.com/lindexi/p/15423277.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值