前言
net6 支持单文件发布,没事捣鼓了下,由于我开发用的 vscode 所以发布也就直接用 vscode 了
正文
知识点
-
vscode
默认生成的任务中的发布以进程形式执行 -
net 发布需要用
dotnet publish
命令,以读取项目文件中指定的所有依赖项并将生成的文件集发布到目录中 -
dotnet publish
命令其实底层还是调用MSBuild
,因此任何传递给dotnet publish
的参数都将传递给MSBuild
-c
和-o
参数分别映射到MSBuild
的Configuration
和PublishDir
属性 -
常用的
MSBuild
项目属性可参考官方文档(vs2022):https://docs.microsoft.com/zh-cn/visualstudio/msbuild/common-msbuild-project-properties?view=vs-2022- 使用
IncludeNativeLibrariesForSelfExtract
属性可以将原生的 dll 一起打包进去,win 下运行程序时会先把原生库自动解压到C:\Users\用户名\AppData\Local\Temp\.net\程序文件名
下的一个文件夹中 - 使用
PublishTrimmed
属性可以在打包的时候将一些没有用到的依赖过滤掉,使最终的文件瘦身,并且效果很明显
- 使用
示例
根据文档给出的属性整理了下参数,具体任务配置文件task.json
如下:
部分参数根据需求自行修改
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/src/GatewayBranch.Application/GatewayBranch.Application.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish-win-x64",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/GatewayBranch.Application/GatewayBranch.Application.csproj",
"/p:Configuration=Release",
"/p:PublishDir=${workspaceFolder}/.output/publish/win-x64",
"/p:RuntimeIdentifier=win-x64",
"/p:PublishSingleFile=true",
"/p:PublishTrimmed=true",
"/p:IncludeNativeLibrariesForSelfExtract=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish-win-x86",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/GatewayBranch.Application/GatewayBranch.Application.csproj",
"/p:Configuration=Release",
"/p:PublishDir=${workspaceFolder}/.output/publish/win-x86",
"/p:RuntimeIdentifier=win-x86",
"/p:PublishSingleFile=true",
"/p:PublishTrimmed=true",
"/p:IncludeNativeLibrariesForSelfExtract=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish-linux-64",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/GatewayBranch.Application/GatewayBranch.Application.csproj",
"/p:Configuration=Release",
"/p:PublishDir=${workspaceFolder}/.output/publish/linux-x64",
"/p:RuntimeIdentifier=linux-x64",
"/p:PublishSingleFile=true",
"/p:PublishTrimmed=true",
"/p:IncludeNativeLibrariesForSelfExtract=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/src/GatewayBranch.Application/GatewayBranch.Application.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/src/GatewayBranch.Application/GatewayBranch.Application.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}