dotnet core瘦身发布

时间:2024-07-13 23:34:38

需要安装nuget包Microsoft.Packaging.Tools.Trimming
然后利用如下命令发布
dotnet publish -r win10-x64 -c release --self-contained true /p:TrimUnusedDependencies=true
发布完成之后,实际上还是有不少dll文件,只不过由原来的200多个减少了70多个。
 
然后还有个瘦身的方法
项目地址https://github.com/dgiagio/warp
国内这毛线网可能有点慢

关于windows摘录如下

Windows

Create a simple console application

PS C:\Users\Diego\Devel> mkdir myappPS C:\Users\Diego\Devel> cd myappPS C:\Users\Diego\Devel\myapp> dotnet new consolePS C:\Users\Diego\Devel\myapp> dotnet runHello World!PS C:\Users\Diego\Devel\myapp>

Publish the application with native installer for win10-x64 runtime

PS C:\Users\Diego\Devel\myapp> dotnet publish -c Release -r win10-x64

The application should be published to bin/Release/netcoreapp2.1/win10-x64/publish/

Download warp-packer

If you save warp-packer in a directory in your PATH, you only need to download it once.

PS C:\Users\Diego\Devel\myapp> [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" ; Invoke-WebRequest https://github.com/dgiagio/warp/releases/download/v0.3.0/windows-x64.warp-packer.exe -OutFile warp-packer.exe

Create your self-contained application

PS C:\Users\Diego\Devel\myapp> .\warp-packer --arch windows-x64 --input_dir bin/Release/netcoreapp2.1/win10-x64/publish --exec myapp.exe --output myapp.exe

Run your self-contained application

PS C:\Users\Diego\Devel\myapp> .\myapp.exeHello World!PS C:\Users\Diego\Devel\myapp>

More information about your self-contained application

PS C:\Users\Diego\Devel\myapp> "{0:N2} MB" -f ((Get-Item myapp.exe).Length / 1MB)28.51 MB

坑爹的情况是,这个warp程序会一直下载不下来,然后尝试文档中提到的用power shell下载
在powsershell 中执行如下语句
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls" ; Invoke-WebRequest https://github.com/dgiagio/warp/releases/download/v0.3.0/windows-x64.warp-packer.exe -OutFile warp-packer.exe
孬好速度比浏览器要一点,下载完后,准备打包

按照指令格式,拼装指令,示例如下,然后就可以将exe和DLL打包成1个exe了

PS E:\DijingTools\Dijing.Update> .\warp-packer.exe --arch windows-x64 --exec Dijing.Update.exe --input_dir
.\bin\release\netcoreapp2.1\win10-x64\publish\ --output Dijing.Update.exe

然后测试一下exe,可以正常,这样部署的时候,就不用带很多程序了

PS E:\DijingTools\Dijing.Update\bin\Debug\netcoreapp2.1\publish> .\Dijing.Update.exe http://zhcto.com/packa
ges/106SP7fc10b15-8dd8-4ecc-ba02-f52a833b3783.zip
文件夹E:\DijingTools\Dijing.Update\bin\Debug\netcoreapp2.1\publish\temp创建成功
升级包开始下载
升级包下载中...3833,157757,2%
升级包下载中...13587,157757,8%
升级包下载中...46063,157757,29%
升级包下载中...71479,157757,45%
升级包下载中...137015,157757,86%
升级包下载中...140667,157757,89%
升级包下载中...157757,157757,100%
升级包下载完成
检测到升级包已存在
升级包解压完成
升级文件遍历完成,文件个数为:5
升级包文件覆盖完成
临时文件夹删除完成

程序启动成功

PS E:\DijingTools\Dijing.Update\bin\Debug\netcoreapp2.1\publish>

还有一种CoreLR的东西,也可以瘦身,不过注入关系得自己写个表,就懒得弄了