Azure Artifact 中包的多个版本

Multiple versions of a package in Azure Artifact

我在 Azure Devops 中设置了一个工件源,并使用 "nuget.exe push" 将我们的一些私有包推送到它。

我遇到的问题是一些包有多个版本(例如 1.1、1.2、1.3 等),我们有许多项目,其中一些将使用 1.1,一些将使用 1.2 等。

将版本 1.1、1.2 和 1.3 上传到工件源后,只有 1.3 可用于我的项目,因为它是最新版本。如果我在 Devops 界面中单击上传的包,我可以看到以前的版本,但是 none 使用 1.1 或 1.2 的 devops 项目将构建,因为它们找不到旧版本。

我在一些地方读到过,一种解决方法是为每个项目提供一个工件提要,并将所需的版本推送到该提要。我遇到的问题是我简化了问题的规模,因为实际上我们有大约 20 个包,每个包最多可以有 30 个不同的版本,而我有大约 50 个项目。为每个人创建一个提要会非常耗时,并且在我推送它们时会涉及复制很多包。

如果我从 nuget 向项目添加任何包,我可以选择要添加的版本,但在使用工件提要时似乎无法复制它。我这样做不正确还是有更好的方法来实现我的需要?

编辑:

我没有使用项目范围的提要,它在 Devops 中列为组织范围的提要。

packages.config 文件专门针对特定版本,例如

<package id="CommonResourceAssembly" version="2.17.60.0" targetFramework="net451" />

并且错误日志显示找不到此版本:

##[error].nuget\NuGet.targets(103,9): Error : Unable to find version '2.17.60.0' of package 'CommonResourceAssembly'.

提要本身显示版本 2.17.61 是当前版本,但 2.17.60 是此特定项目所需的版本,并且确实出现在版本历史记录中:

Am I doing this incorrectly or is there a better way to achieve what I need?

恐怕您的设置有误。那是因为 Azure Artifact 支持包的多个版本。这也是为什么您可以在 Devops 界面中看到以前版本的原因。

当我们在 Azure devops pipeline 中使用 nuget restore 时,nuget restore 任务会根据 packages.config/ 中指定的包版本来恢复包PackageReference,喜欢:

Packages.config:

<packages>
  <package id="Newtonsoft.Json" version="8.0.3" targetFramework="net46" />
</packages>

PackageReference:

<ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="8.0.3" />
</ItemGroup>

因此,请检查以下可能导致此问题的可能性:

  • 检查您的 Azure Artifact 是否为 project scoped feeds(如果您的 Azure Artifact 提要是在 11/4/2019 之前创建的,请转到第二点):

  • 检查是否在 packages.config/PackageReference:

    中使用了通配符
    <PackageReference Include="Packagename" Version="1.*" />
    
  • 检查您的解决方案中是否有 nuget.config,设置如下:

    <configuration>
       <config> 
         <add key="dependencyversion" value="Highest" /> 
       </config>
    </configuration>
    

如果以上对您没有帮助,请与我们分享有关此问题的更多信息,info/image 关于构建失败项目的 packages.config/PackageReference,info/image关于 Azure Artifact,包括包版本 1.1、1.2,最新但不重要,错误日志

希望这对您有所帮助。

感谢@Leo Liu-MSFT 的帮助,我找出了需要修改的地方:

1 从 .nuget 文件夹中删除 nuget.exe 和 nuget.targets 文件

2 删除所有 .csproj 文件中指向 nuget.targets 文件的引用:

  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />   <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />   </Target>

3 在构建之前在管道 YAML 中添加一个步骤,查看 nuget.config 文件以恢复包:

task: NuGetCommand@2   
displayName: 'NuGet restore'   
inputs:   
 restoreSolution: '**\*.sln'   
 feedsToUse: config   
 nugetConfigPath: '.nuget/NuGet.config'

现在可以正确构建解决方案并且可以引用包的任何先前版本