本地编译时如何将 NuGet 安装在 .nuget 文件夹中

How to NuGet install in .nuget folder when compiling locally

我有两个 .Net 项目:

使用 TFS,我们能够编译核心库,发布生成的 NuGet 库,然后启动应用程序的构建,恢复对核心库的 NuGet 引用。一切都按预期工作。

问题是,在开发核心库+应用程序时,我们经常编译核心,不能等到整个核心库构建完成才能更新应用程序的本地引用。此外,我们必须推送核心库的完整提交(因此我们不能在应用程序测试之前一次推送一个 DTO)。

所以我想知道 Visual Studio 和 Nuget 中是否存在一种方法 publish/install 本地用户的 .nuget 文件夹中的核心 Nuget 库,以便让它自动可见该应用程序的下一个构建项目。

你能帮我解决这个问题吗?

So I was wondering if exists a way in Visual Studio and Nuget to publish/install the core's Nuget library in the local user's .nuget folder in order to let it be visible.

在本地发布nuget包(nuget push)到一个位置之前,我们应该添加路径作为包源。所以我们需要先将本地包的路径设置为package source。 nuget.config文件中的内容对应VS中的Package SourceUI,所以有两种方法可以实现。

1.In VS,go Tools=>nuget package manager=>package manager settings=>package source,点击绿色按钮定义新的包源。

2.Or 我们可以找到当前用户的 nuget.config 文件,参见 this document。 #1 中的 UI 操作实际上有助于在 C:\Users\xxx\AppData\Roaming\NuGet 中的 nuget.config 文件中定义源,参见:

所以我们可以直接编辑这个文件来设置我们新的包源,保存文件并重启VS后,我们可以在UI.

中看到新定义的源

My problem is to automatize the nuget push command to the local feed, that in my case should be %userprofile%.nuget\packages. I cannot ask any person in the company to manually copy the core package to the local feed in order to see the changes in the linked app

将本地提要添加到包源后,您可以使用nuget push命令将包发布到提要。

你可以在你的核心库项目文件(xx.csproj)中定义一个类似于此的自定义目标来自动打包并自动推送包:

  <Target Name="CustomTarget" AfterTargets="build">
    <Exec Command="nuget.exe pack xx.csproj"/>
    <Exec Command="nuget.exe push -source xxx path\xxx.nupkg"/>
  </Target>

您还可以将 Conditions 添加到此目标,<Target Name="CustomTarget" AfterTargets="build" Condition="$(Configuration)=='Debug'">。您可以控制 VS 应在哪个配置中 运行 此目标为您打包和推送。

另外:

对于.net core 库项目,VS 有创建的选项。 nuget 包。可以在VS中右击项目selectPackbutton.So也可以为这个项目定义一个.bat文件来做nuget push。该过程是构建项目=>使用 pack 选项轻松获取 nuget 包=>运行 .bat 自动推送。

希望对您有所帮助。

I made some changes for my needs. In fact, locally I do not have nuget.exe installed but only the .Net Core runtime, so I'm using the dotnet pack command instead of nuget.exe pack and dotnet nuget push instead of nuget.exe push. Moreover before pushing locally the updated NuGet package I delete the old package with the same version, else dotnet nuget push 包的回答开始。

我接受 Lance Li-MSFT 的回答,因为它是我解决方案的起点,适用于本地 nuget.exe 的人。

我的解决方案如下

corelib.csproj

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <Target Name="PublishNuGetLocally" AfterTargets="Build">
    <Exec Command="dotnet pack $(ProjectPath)"/>
    <Exec Command="dotnet nuget delete --source $(UserProfile)\.nuget\packages $(PackageId) $(PackageVersion) --non-interactive" ContinueOnError="WarnAndContinue"/>
    <Exec Command="dotnet nuget push --source $(UserProfile)\.nuget\packages $(ProjectDir)bin$(ConfigurationName)$(PackageId).$(PackageVersion).nupkg" />
  </Target>
  [...]

</Project>

附带说明一下,我不必在 Visual Studio 中配置包源,因为全局 Nuget 缓存(在路径 %UserProfile%\.nuget\packagesis automatically used 作为第一个位置搜索 NuGet 包。