Reputation: 18485
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?
Upvotes: 2
Views: 4058
Reputation: 56849
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
, anddotnet pack
are 3 separate commands. However, for this scenario, the only one you actually need to execute isdotnet 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.
Upvotes: 5