vscode 将 netcore 项目以单文件发布并裁剪

前言

net6 支持单文件发布,没事捣鼓了下,由于我开发用的 vscode 所以发布也就直接用 vscode 了

正文

知识点
  • vscode默认生成的任务中的发布以进程形式执行

  • net 发布需要用dotnet publish命令,以读取项目文件中指定的所有依赖项并将生成的文件集发布到目录中

  • dotnet publish 命令其实底层还是调用MSBuild,因此任何传递给 dotnet publish 的参数都将传递给 MSBuild

    -c-o 参数分别映射到 MSBuildConfigurationPublishDir 属性

  • 常用的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"
    }
  ]
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值