Skip to content

Commit 9576cbd

Browse files
committed
Add multitargeting example
1 parent 6d3ed8f commit 9576cbd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/consume-packages/Central-Package-Management.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,37 @@ For each project, you then define a `<PackageReference />` but omit the `Version
7373

7474
Now you're using central package management and managing your versions in a central location!
7575

76+
### Using Different Versions for Different Target Frameworks
77+
78+
As NuGet packages evolve, sometimes package owners drop support for older target frameworks.
79+
This can cause issues for developers of libraries that still target older frameworks where they want to reference newer versions of packages with newer target frameworks.
80+
For example, this project targets .NET Standard 2.0, .NET 8.0, and .NET Framework 4.7.2 but references `PackageA` which no longer supports .NET Standard 2.0 in its latest version.
81+
82+
```xml
83+
<Project Sdk="Microsoft.NET.Sdk">
84+
<PropertyGroup>
85+
<TargetFrameworks>netstandard2.0;net8.0;net472</TargetFrameworks>
86+
</PropertyGroup>
87+
<ItemGroup>
88+
<PackageReference Include="PackageA" />
89+
</ItemGroup>
90+
</Project>
91+
```
92+
93+
In this case, you can define different versions for each target framework in your `Directory.Packages.props` using [MSBuild conditions](/visualstudio/msbuild/msbuild-conditions):
94+
95+
```xml
96+
<Project>
97+
<PropertyGroup>
98+
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
99+
</PropertyGroup>
100+
<ItemGroup>
101+
<PackageVersion Include="PackageA" Version="1.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
102+
<PackageVersion Include="PackageA" Version="2.0.0" Condition="'$(TargetFramework)' == 'net8.0' Or '$(TargetFramework)' == 'net472'" />
103+
</ItemGroup>
104+
</Project>
105+
```
106+
76107
## Central Package Management rules
77108

78109
The `Directory.Packages.props` file has a number of rules with regards to where it's located in a repository's directory and its context. For the sake of

0 commit comments

Comments
 (0)