预构建事件中的包管理器控制台命令 [英] Package Manager Console commands in pre-build event
问题描述
本周早些时候我发布了一个关于如何有效地的问题在一个解决方案中的多个 Web 应用程序之间共享文件.NuGet 是作为解决方案提供的,一段时间后我能够实施解决方案,但我有一个问题目前阻止我完成它.
Earlier this week I posted a question about how to effectively share files amongst multiple web applications within a solution. NuGet was offered as a solution and after some time I was able to implement a solution, but I have one issue that is currently blocking me from finishing it.
此时我有两个 Web 应用程序项目:
At this point I have two web application projects:
Project.Common.Presentations
Project.Website.Presentations
每次构建 Project.Common.Presentations
项目时,都会创建一个具有更高版本的新 NuGet 包.这个包用于我的主要 web 项目 Project.Website.Presentations
.最初,我通过 GUI 将 project.common.presentations NuGet 包添加到该项目.在那之后,我设置了一个预构建 eveny,我从命令行执行 NuGet 更新以获取最新版本的 Project.Common.Presentations
包.
Each time the Project.Common.Presentations
project is build, a new NuGet package with a higher version is created. This package is used in my main web project Project.Website.Presentations
. Initially I add the project.common.presentations NuGet package to this project through the GUI. Right after that I setup a pre-build eveny where I do a NuGet update from the command line to pull in the latest version of the Project.Common.Presentations
package.
但不知何故在命令行中使用 NuGet 更新添加了对我的项目的引用,使用了一个实际上不在磁盘上的位置,导致了这个问题:
But somehow using NuGet update in the command line adds a reference to my project using a location that’s not actually on disk, leading to this issue:
无法解析此引用.找不到程序集Project.Common.Presentations".检查以确保程序集存在于磁盘上.如果您的代码需要此引用,您可能会遇到编译错误.
Could not resolve this reference. Could not locate the assembly "Project.Common.Presentations". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
这是我设置结构的方式:
This is how I set up the structure:
- 我使用
.csproj
文件为我的Project.Common.Presentations
项目生成了一个 NuSpec 文件. - 作为我的
Project.Common.Presentations
中的构建后事件,我使用:$(ProjectDir)nuget.exe pack -sym $(ProjectPath)
.每次生成项目时都会生成一个新的 NuGet 包.在执行此构建事件之前,我使用脚本删除旧的 NuGet 包. - 作为我的
Project.Website.Presentations
中的预构建事件,我使用:$(ProjectDir)NuGet.exe update $(ProjectDir)packages.config
立>
- I generated a NuSpec file for my
Project.Common.Presentations
project using the.csproj
file. - As a post-build event in my
Project.Common.Presentations
, I use:$(ProjectDir)nuget.exe pack -sym $(ProjectPath)
. This builds a new NuGet package every time the project is build. Before this build-event is executed I use a script to delete the old NuGet packages. - As a pre-build event in my
Project.Website.Presentations
I use:$(ProjectDir)NuGet.exe update $(ProjectDir)packages.config
第一次重建全部运行,我的 .csproj
文件更新了对我的包的 dll 引用,一切都很好.我第二次运行rebuild all时,.csproj
文件再次更新,但它引用了一个不同的文件夹,其中包含更新版本的包,但该文件夹不知何故不在驱动器上.当我使用 GUI管理 NuGet 包"更新 NuGet 包时,不会出现此问题,一切正常.
The first rebuild all is run, my .csproj
file is updated with a dll reference to my package, all is fine and good. The second time I run rebuild all, the .csproj
file is updated again, but it references a different folder with a newer version of the package but the folder somehow is not on the drive. When I use the GUI "Manage NuGet packages" to update the NuGet package this problem does not occur, everything works fine.
删除预构建事件并手动更新包工作正常.但是每次构建时都必须手动执行某些操作并不是我喜欢的事情.使用包管理器控制台卸载和安装包也可以正常工作.有没有办法将包管理器控制台操作合并到预构建事件中?提前感谢您提供信息.
Removing the pre-build event, and updating the package manually works fine. But having to do something manually each time you build is not something I enjoy. Using the Package Manager Console to uninstall and install the package also works fine. Is there a way to incorporate Package Manager Console action into a pre-build event? Thanks in advance for the information.
推荐答案
将 NuGet 用于此类事情的基本思想是在解决方案中的所有项目之间共享相对静态(很少更改)的工件.我个人使用 NuGet 还包含一个通用母版页 - 我的所有文件都放在 NuGet Content 文件夹中.
The basic idea of using NuGet for something like this is to share the relatively static (seldom changing) artifacts among all the projects in your solution. I personally used NuGet to also include a common master page - all my files went in the NuGet Content folder.
我所有的常用功能都在库项目中,这消除了对任何类型的构建脚本的需求.
All my common functionality went in library projects, which obviated the need for build scripts of any kind.
Ref: 其中的多个 Web 应用程序项目Visual Studio 解决方案
这篇关于预构建事件中的包管理器控制台命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!