【问题标题】:Can I include multiple publishing methods in my .NET Core csproj?我可以在我的 .NET Core csproj 中包含多种发布方法吗?
【发布时间】:2021-03-23 23:15:38
【问题描述】:

我有一个简单的 .NET Core .csproj 用于我想以两种方式部署的项目:

  1. 作为一个独立的、修剪过的单文件二进制文件(可移植的mytool.exe,没有其他文件)
  2. 作为未修剪的多文件 DLL(dotnet mytool.dll,文件夹中还有一些其他支持的 DLL)

我想这样做是因为单文件 .NET Core 二进制文件冷启动非常慢。我希望这个工具尽可能地便携,所以我需要一个单文件 .NET Core 二进制文件,但我也想让用户调用更快的dotnet mytool.dll,如果他们不需要便携性。

我已将我的工具配置为构建一个独立的、经过修剪的单文件二进制文件:

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>

是否有一种简单的方法可以提供多个“配置”或“目标”,可以从命令行轻松构建(或者在我的情况下是 ADO 管道),以便支持我的其他配置?例如:

  <!-- Other configuration (multi-file), somehow -->
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <PublishReadyToRun>true</PublishReadyToRun>
  </PropertyGroup>

【问题讨论】:

  • 您可以将所有发布参数直接传递到命令行或基于PropertyGroup条件

标签: .net-core azure-pipelines csproj


【解决方案1】:

根据 Pavel 的评论,我找到了一些相关信息。

然后,按照.NET application publishing overview,我设置了一个自定义属性(flavor)来控制构建:

  <!--
    Default flavor is SINGLEBINARY
    https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/msbuild/how-to-build-the-same-source-files-with-different-options?view=vs-2015
  -->
  <PropertyGroup>
      <Flavor Condition="'$(Flavor)'==''">SINGLEBINARY</Flavor>
  </PropertyGroup>

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

  <!-- If flavor is SINGLEBINARY, output a standalone EXE -->
  <PropertyGroup Condition="'$(Flavor)'=='SINGLEBINARY'">
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishTrimmed>true</PublishTrimmed>
  </PropertyGroup>

  <!-- If flavor is MULTIBINARY, output a more-typical dotnet DLL -->
  <PropertyGroup Condition="'$(Flavor)'=='MULTIBINARY'">
    <!-- No specific options; the default publish works. -->
  </PropertyGroup>

我现在可以基于此属性进行单独的构建和发布:

dotnet build /p:flavor=singlebinary # single-binary; self-contained EXE for easy sharing
dotnet build /p:flavor=multibinary # multi-binary

dotnet publish --configuration Release /p:flavor=singlebinary
dotnet publish --configuration Release /p:flavor=multibinary

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 2023-02-02
    • 1970-01-01
    • 2019-07-12
    • 2017-01-01
    • 2013-01-09
    • 1970-01-01
    • 2020-08-07
    • 2013-09-30
    相关资源
    最近更新 更多