是否可配置Cake让DotNetCorePublishSettings启用self-contained开关?
解决方案
可以实现该需求,Cake 内置的 DotNetCorePublish 函数提供了原生配置项支持开启自包含发布,对应 MSBuild 的 --self-contained 开关,以下是两种常用配置方式:
方式1:通过配置属性直接设置
直接在 DotNetCorePublishSettings 中显式开启 SelfContained 开关,同时指定目标运行时标识符(RID)即可:
DotNetCorePublish("./你的项目路径/项目名.csproj", new DotNetCorePublishSettings { // 开启自包含发布,打包对应版本的.NET框架 SelfContained = true, // 指定目标运行时,可根据发布目标替换为 linux-x64/osx-arm64/win-arm64 等 RuntimeIdentifier = "win-x64", // 其他原有配置,比如输出路径 OutputDirectory = "./publish" });
方式2:通过自定义参数传递MSBuild开关
如果需要更灵活的参数传递,也可以直接通过参数自定义配置传入对应的MSBuild开关:
DotNetCorePublish("./你的项目路径/项目名.csproj", new DotNetCorePublishSettings { OutputDirectory = "./publish", ArgumentCustomization = args => args .Append("--self-contained true") .Append("-r win-x64") });
注意事项
- 开启自包含发布时必须指定目标运行时标识符(RID),否则发布过程会报错
- 如果需要减小最终发布包体积,可以额外添加
TrimMode = "full"配置开启框架裁剪,仅保留项目实际用到的框架组件
内容的提问来源于stack exchange,提问作者Sascha Manns
相关产品推荐
相关产品推荐

