Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【文章推荐】.NET集中式包管理 #775

Closed
tonyqus opened this issue Dec 11, 2024 · 1 comment
Closed

【文章推荐】.NET集中式包管理 #775

tonyqus opened this issue Dec 11, 2024 · 1 comment

Comments

@tonyqus
Copy link
Member

tonyqus commented Dec 11, 2024

https://medium.com/@MilanJovanovicTech/central-package-management-in-net-simplify-nuget-dependencies-1f6c744f79d7

@gaufung gaufung changed the title 【文章推荐】.NET集中式package管理,可简化nuget依赖管理 【文章推荐】.NET集中式包管理 Dec 24, 2024
@gaufung
Copy link
Collaborator

gaufung commented Dec 24, 2024

如果你的项目解决方案中包含了很多 C# 工程项目,但是由于管理或者其他原因,同一个依赖包包含了不同的版本。这样会增加管理的复杂程度,比如当要升级版本的时候,需要更改所有的出现的地方。而且如果项目之间存在引用关系,会导致版本冲突的问题。所以 .NET 提供了一个集中包管理机制。首先在项目目录下创建一个 Directory.Packages.props 文件

<Project>
  <PropertyGroup>
    <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
  </PropertyGroup>
  <ItemGroup>
    <PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
    <PackageVersion Include="Serilog" Version="4.1.0" />
    <PackageVersion Include="Polly" Version="8.5.0" />
  </ItemGroup>
</Project>

这样在项目中的 csproj 文件,就不需要制定版本

<ItemGroup>
  <PackageReference Include="Newtonsoft.Json" />
  <PackageReference Include="AutoMapper" />
  <PackageReference Include="Polly" />
</ItemGroup>

如果在项目中覆盖掉版本,可以 VersionOverride 属性

<PackageReference Include="Serilog" VersionOverride="3.1.1" />

当然也可以制定某个包让每个工程项目使用

<ItemGroup>
  <GlobalPackageReference Include="SonarAnalyzer.CSharp" Version="10.3.0.106239" />
</ItemGroup>

@gaufung gaufung closed this as completed Dec 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants