繁体   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