在同一Nuget软件包中以.NET 4.7.2和.NET 6.0构建?

发布于 2025-01-24 03:13:23 字数 1572 浏览 3 评论 0原文

我可以为.NET Framework 4.7.2项目创建一个Nuget软件包。

# .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构建一个版本。我感到迷路了。很抱歉这样问这个,但是我该怎么办。多个构建定义?多个任务?

I'm able to create a NuGet package for .NET Framework 4.7.2 project.

# .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'

But I would like to build a version for .NET 6.0. I feel lost. I'm sorry to ask this like this but what should I do. Multiple build definition? Multiple tasks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-01-31 03:13:23

要使用多目标支持,您需要始终如一地使用新的构建工具链。也就是说,您需要使用.NET SDK而不是旧的msbuild/nuget.exe工具构建和包装。

因此,首先,您需要使用构建项目文件。要同时定位net472net6.0,请使用< targetFrameWorks> element(与< targetframework> 元素)。您还应将< iSpackable>/iSpackable>用于您不想包装到.nupkg文件中的任何项目,因为多目标是通过在 solution包装中完成的等级

<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.ReferenceSemblies作为私人资产可确保您的.NET Framework 4.7.2构建即使在Linux和MacOS上也可以正常工作。您需要的只是。net 6+ sdk 在构建计算机上。

您需要使用 dotnet build build < /a>进行建筑物和 dotnet 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

技术上,dotnet还原dotnet builddotnet pack是3个单独的命令。但是,在这种情况下,您实际需要执行的唯一一个是dotnet pack。默认情况下,如果未提供 参数,它将运行其他两个。但是,为了使构建更易于管理,有时将它们用作单独命令以使其更清楚MSBuild参数是有利的。

To use the multi-targeting support, you need to consistently use the new build toolchain. That is, you need to build and pack with the .NET SDK rather than the old MSBuild/NuGet.exe tools.

So, firstly you need to be using the new .csproj format to build your project files. To target both net472 and net6.0, use the <TargetFrameworks> element (as opposed to <TargetFramework> element). You also should set <IsPackable>false</IsPackable> for any projects that you don't want to pack into .nupkg files, since multi-targeting is done by packing at the solution level.

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

Adding Microsoft.NETFramework.ReferenceAssemblies as a private asset ensures your .NET Framework 4.7.2 build will work even on Linux and macOS. All you need is the .NET 6+ SDK on the build machine.

You need to use the dotnet build to do the building and dotnet pack to do the packing. Both of these should be executed on the solution file.

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

Technically, dotnet restore, dotnet build, and dotnet pack are 3 separate commands. However, for this scenario, the only one you actually need to execute is dotnet pack. By default it will run the other two if the --no-build parameter is not supplied. But, to make the build more manageable, it is sometimes advantageous to use them as separate commands to keep it more clear what the MSBuild parameters are for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文