在 nuget 包中使用包引用的道具和目标



我喜欢创建一个包含.props文件的NuGet包。

.props文件包含以下内容:

<Project>
<ItemGroup>
<PackageReference Include="GitVersion.Tool" Version="[5.1.1]" PrivateAssets="All" ExcludeAssets="All" />
<PackageReference Include="dotnet-sonarscanner" Version="[4.7.1]" PrivateAssets="All" ExcludeAssets="All" />
<PackageReference Include="ReportGenerator" Version="[4.4.6]" PrivateAssets="All" ExcludeAssets="All" />
<PackageReference Include="NuGet.CommandLine" Version="[5.4.0]" PrivateAssets="All" ExcludeAssets="All" />
<PackageReference Include="CycloneDX" Version="[0.9.0]" PrivateAssets="All" ExcludeAssets="All" />
<PackageReference Include="trx2junit" Version="[1.3.0]" PrivateAssets="All" ExcludeAssets="All" />
</ItemGroup>
<ItemGroup>
<NukeSpecificationFiles Include="***.json" Exclude="bin**;obj**" />
<NukeExternalFiles Include="***.*.ext" Exclude="bin**;obj**" />
<None Remove="*.csproj.DotSettings;*.ref.*.txt" />
<!-- Common build related files -->
<None Include="..build.ps1" />
<None Include="..build.sh" />
<None Include="...nuke" LinkBase="config" />
<None Include="..global.json" LinkBase="config" Condition="Exists('..global.json')" />
<None Include="..nuget.config" LinkBase="config" Condition="Exists('..nuget.config')" />
<None Include="..GitVersion.yml" LinkBase="config" Condition="Exists('..GitVersion.yml')" />
<None Include="..CODEOWNERS" LinkBase="config" Condition="Exists('..CODEOWNERS')" />
<None Include="...teamcitysettings.kts" LinkBase="ci" Condition="Exists('...teamcitysettings.kts')" />
<None Include="...githubworkflows*.yml" LinkBase="ci" />
<None Include="..azure-pipelines.yml" LinkBase="ci" Condition="Exists('..azure-pipelines.yml')" />
<None Include="..Jenkinsfile" LinkBase="ci" Condition="Exists('..Jenkinsfile')" />
<None Include="..appveyor.yml" LinkBase="ci" Condition="Exists('..appveyor.yml')" />
<None Include="...gitlab-ci.yml" LinkBase="ci" Condition="Exists('...gitlab-ci.yml')" />
<None Include="...travis.yml" LinkBase="ci" Condition="Exists('...travis.yml')" />
</ItemGroup>
</Project>

在打包并将 nuget 推送到源后,nuget 会被其他项目使用,这很有效。

使用项目解释.props以及所有条件文件,并添加PackageReference

当我使用 VS2019 构建项目时,一切正常。但是当我使用dotnet build构建时.props不会添加文件中的PackageReference

我检查了project.assets.json,它们是不同的。VS2019将工具添加到资产文件中:

...
"CycloneDX/0.9.0": {
"type": "package"
},
"dotnet-sonarscanner/4.7.1": {
"type": "package"
},
"GitVersion.Tool/5.1.1": {
"type": "package"
},
...

但是 dotnet build 不会添加它。

是否需要将属性或标志添加到 dotnet 还原命令?或者你知道为什么行为不同。

我的目标是使用项目将 dotnet 工具作为其项目中的私有资产。

我找到了解决您问题的方法,因为我也在为同样的问题而苦苦挣扎。

溶液

提供 NuGet 包作为包引用,并以可传递方式使用它们。 为此,您可以将 PrivatAssets 设置为 None 以包含所有资产(或取决于您确切想要传递提供的内容,但编译和运行时应该是其中的一部分(。另请查看控制 DECTENCY 资产以了解详细信息。

您的 .csproj 将包含以下声明:

<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="GitVersion.Tool" Version="[5.1.1]" PrivateAssets="None" />
<PackageReference Include="dotnet-sonarscanner" Version="[4.7.1]" PrivateAssets="None" />
<PackageReference Include="ReportGenerator" Version="[4.4.6]" PrivateAssets="None" />
<PackageReference Include="NuGet.CommandLine" Version="[5.4.0]" PrivateAssets="None" />
<PackageReference Include="CycloneDX" Version="[0.9.0]" PrivateAssets="None" />
<PackageReference Include="trx2junit" Version="[1.3.0]" PrivateAssets="None" />
</ItemGroup>
<ItemGroup>
<None Include="buildYourPackage.props" Pack="true" PackagePath="build" />
</ItemGroup>
</Project>

你相应的 YourPackage.props 看起来像以前一样,但没有引用。

然后,您的消费项目可以简单地引用 YourPackage(并选择性地控制应该使用的内容(:

<PackageReference Include="YourPackage" Version="x.y" /> 

对原始问题的阐述

我还想通过NuGet包导入PackageReferences,并且在Visual Studio中本地构建时工作正常。但是,包引用未导入到运行dotnet build的构建服务器上(dotnet build在本地也不起作用(。

经过一番研究,我偶然发现了这份关于MSBuild道具和目标内容的指南,特别是:

软件包的 .props 和 .targets 中,有一些事情不能执行,例如不指定影响还原的属性和项,因为它们将被自动排除。

  • 一些不得添加或更新的项目示例:PackageReference、PackageVersion、PackageDownload 等

这在某种程度上是有道理的,因为提供 .props/.targets 的 NuGet 是随restore一起导入的,并为自己提供要在完全相同的步骤中处理的引用。我怀疑dotnet还原根本无法处理这个问题。(它也是epxlains,我需要在Visual Studio中构建两次才能使其工作。

不,没有标志。您需要改用 dotnet pack。

最新更新