如何解决项目预构建事件中的 Nuget 包版本和路径? [英] How to resolve Nuget Package Version and Path in Pre-Build Event of a Project?

查看:10
本文介绍了如何解决项目预构建事件中的 Nuget 包版本和路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NuGet 包,其中包含我的项目中引用的内容文件.在新机器上获取最新版本时,构建失败,因为 NuGet 还原不复制内容文件.所以我想做的是:

在 PreBuild 中如果我的内容文件不存在,运行这个 nuget 命令:Update-Package My.Nuget.Sources -reinstall"

由于 Nuget 文件的版本控制和获取 nuget 的访问权限,我正在努力确定路径差异.如何实际生成下面的 SourceOnlyNugetVersionNugetPath 变量?

如果不存在 "$(ProjectFolder)App_PackagesMy.NuGet.Sources.$(SourceOnlyNugetVersion?)somefile.cs" ("$(NugetPath)nuget.exe" 更新包 My.NuGet.Sources -reinstall)

解决方案

如何在项目的 Pre-Build 事件中解析 Nuget 包版本和路径?

如果你想在 Pre-build 事件中自动重新安装包,恐怕你目前无法实现.

我们可以使用命令 Update-Package -Id <package_name>–reinstall 在包管理器控制台中将包重新安装到您的项目中,但不可能自动执行此操作.

如果您想在构建事件中自动化它,您必须调用 NuGet CLI 而不是包管理器控制台.因为 NuGet CLI 不会修改项目文件或 packages.config;在这种方式下,它类似于还原,因为它只是将包添加到磁盘,但不会更改项目的依赖项.请参阅

反之,在包管理器上操作安装包:

<块引用>

将包及其依赖项安装到项目中.

因此我们无法使用 NuGet CLI 为项目重新安装 NuGet 包.

此外,我们无法在 Visual Studio 之外使用包管理器控制台 powershell,因为包管理器控制台提供的是对 Visual Studio 对象的访问.

https://github.com/NuGet/Home/issues/1512

同样,我们无法在构建事件中使用包管理器控制台,构建事件由 MSBuild 运行,因此它需要在从命令行运行构建时工作.

因此,自动重新安装 NuGet 包似乎是不可能的,或者替代方法是编写一个控制台应用程序,使用 NuGet.Core.dll 来执行与 PowerShell 脚本相同的操作.

I have a NuGet Package containing content files that is referenced in my project. When getting latest on a new machine, the build fails because NuGet Restore doesn't copy content files. So what I want to do is this:

In PreBuild
If my Content file doesn't exist,
    run this nuget Command: "Update-Package My.Nuget.Sources -reinstall"

I'm struggling with determining path differences due to versioning of the Nuget files and getting access to nuget. How do I actually generate the SourceOnlyNugetVersion and NugetPath variables below?

if not exist "$(ProjectFolder)App_PackagesMy.NuGet.Sources.$(SourceOnlyNugetVersion?)somefile.cs" (
    "$(NugetPath)nuget.exe" Update-Package My.NuGet.Sources -reinstall        
) 

解决方案

How to resolve Nuget Package Version and Path in Pre-Build Event of a Project?

If you want to reinstall package automatically in Pre-build event, I am afraid you can`t achieve it currently.

We could use the command Update-Package -Id <package_name> –reinstall to reinstall the packages to your project in the Package Manager Console, but it is impossible to automate that.

If you want to automate it in the build event, you have to call the NuGet CLI rather than Package Manager Console. Because NuGet CLI does not modify a project file or packages.config; in this way it's similar to restore in that it only adds packages to disk but does not change a project's dependencies. See NuGet CLI reference.

The operation Install packages on NuGet CLI:

Conversely, operation Install packages on Package Manager:

Installs a package and its dependencies into a project.

So we could not use NuGet CLI to reinstall NuGet packages for project.

Besides, we could not use the Package manager console powershell outside visual studio, because package manager console is providing is access to visual studio objects.

https://github.com/NuGet/Home/issues/1512

Similarly, we could not use Package manager console in the build event, build events are run by MSBuild so it needs to work when the build is run from the command line.

So it seems impossible to automate reinstall NuGet packages, or the alternative approach would be to write a console app that uses NuGet.Core.dll to do the same thing that the PowerShell script is doing.

这篇关于如何解决项目预构建事件中的 Nuget 包版本和路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆