簡體   English   中英

在同一個 NuGet package 中構建 .Net 4.7.2 和 .Net 6.0?

[英]Build for .Net 4.7.2 and .Net 6.0 in same NuGet package?

我能夠為 .NET Framework 4.7.2 項目創建一個 NuGet package。

# .NET Desktop
# Build and run tests for .NET Desktop or Windows classic desktop solutions.
# Add steps that publish symbols, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/apps/windows/dot-net

name: $(Date:yy).$(Date:MM).$(Rev:r)

trigger: 
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  displayName: NuGet restore
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: NuGetCommand@2
  displayName: 'NuGet pack'
  inputs:
    command: 'pack'
    packagesToPack: '**/*.csproj'
    versioningScheme: 'byBuildNumber'

- task: CopyFiles@2
  inputs:
    sourceFolder: '$(Build.SourcesDirectory)'
    contents: '**/$(BuildConfiguration)/**'
    targetFolder: '$(Build.ArtifactStagingDirectory)'

- task: NuGetCommand@2
  displayName: 'NuGet push'
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    allowPackageConflicts: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

但我想為 .NET 6.0 構建一個版本。 我感到失落。 我很抱歉這樣問,但我該怎么辦。 多重構建定義? 多個任務?

要使用多目標支持,您需要始終使用新的構建工具鏈。 也就是說,您需要使用 .NET SDK 而不是舊的 MSBuild/NuGet.exe 工具來構建和打包。

因此,首先您需要使用new.csproj 格式來構建您的項目文件。 要同時定位net472net6.0 ,請使用<TargetFrameworks>元素(與<TargetFramework>元素相反)。 您還應該為不想打包到.nupkg 文件中的任何項目設置<IsPackable>false</IsPackable> ,因為多目標是通過在解決方案級別打包完成的。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net6.0;net472</TargetFrameworks>
    <IsPackable>true</IsPackable>
  </PropertyGroup>

  <ItemGroup>
  <!-- This is to allow the .NET Framework references to be machine-indepenedent so builds can happen without installing prerequisites -->
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="All" />
  </ItemGroup>
</Project>

Microsoft.NETFramework.ReferenceAssemblies添加為私有資產可確保您的 .NET Framework 4.7.2 構建甚至可以在 Linux 和 macOS 上運行。 您只需要構建機器上的.NET 6+ SDK

您需要使用do.net build進行構建,並使用do.net pack進行打包。 這兩個都應該在解決方案文件上執行。

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'
  sdkVersion: '6.0.202'
  nugetArtifactName: 'nuget'

steps:
# Install .NET SDK
- task: UseDotNet@2
  displayName: 'Use .NET SDK $(sdkVersion)'
  inputs:
    packageType: 'sdk'
    version: '$(sdkVersion)'

# Restore and build the solution (if not supplying --no-restore, it is automatic)
- task: DotNetCoreCLI@2
  displayName: 'dotnet build $(solution)'
  inputs:
    command: custom
    projects: '$(solution)'
    custom: build
    arguments: '--configuration $(buildConfiguration) --verbosity normal /p:Platform="$(buildPlatform)"

# Pack the solution with the --no-build argument.
# You may need to pass additional parameters here, such as /p:PackageVersion="$(PackageVersion)".
- task: DotNetCoreCLI@2
  displayName: 'dotnet pack'
  inputs:
    command: custom
    projects: '$(solution)'
    custom: pack
    arguments: '--configuration $(buildConfiguration) --output "$(Build.ArtifactStagingDirectory)/$(nugetArtifactName)" --no-build --verbosity normal'

# Publish the NuGet artifacts to ensure they can be inspected if any step after this fails
- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: $(nugetArtifactName)'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)/$(nugetArtifactName)'
    ArtifactName: '$(nugetArtifactName)'
  condition: succeededOrFailed()

# Push the NuGet files to a temporary feed (optional)
- task: NuGetCommand@2
  displayName: 'NuGet push'
  inputs:
    command: 'push'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/*.nupkg;!$(Build.ArtifactStagingDirectory)/**/*.symbols.nupkg'
    nuGetFeedType: 'internal'
    allowPackageConflicts: true

從技術上講, do.net restoredo.net builddo.net pack是 3 個獨立的命令。 但是,對於這種情況,您實際需要執行的唯一一個是do.net pack 默認情況下,如果未提供--no-build參數,它將運行其他兩個。 但是,為了使構建更易於管理,有時將它們用作單獨的命令是有利的,這樣可以更清楚地了解 MSBuild 參數的用途。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM