问题
I have a solution with several ASP.NET Core projects in it, managed in Visual Studio 2015.
My NuGet packages are currently being stored in C:\Users\My User\.nuget\packages\
. I need to instead have a Packages directory in my solution and reference packages from there. Like this:
- SolutionFolder
- src
- Project1
- Project2
- [...]
- Packages
- src
I copied the packages from the default path above into a solution directory, then removed all references for a project. I then added my Packages directory as a NuGet Package Source. Finally, I clicked Manage NuGet Packages and added some to a specific project, making sure that my custom package source was selected.
This seemed to work, but when I checked the path of the referenced packages, it was still the default C:\Users\[...]\packages\
path.
What am I missing? How do I reference the packages from the solution directory?
I tried opening up the .xproj file and manually setting the path like I used to sometimes do in .csproj files, but it appears that isn't possible in .xproj files. Or I just don't know how to do it.
回答1:
I copied the packages from the default path above into a solution directory...it was still the default C:\Users[...]\packages\ path
You may mix up the concept "repositoryPath
" and "Packages source
".
Although you removed all references, added my Packages directory as a NuGet Package Source, install reference from custom package source, you just only change the Packages source. Once you install the packages from the custom package source, NuGet will download the packages from the custom package source and set those download packages to the default repositoryPath
, then install those packages from default repositoryPath
to your project. So it was still in the default path.
If you want to change packages default location for .net core project, you can set "NUGET_PACKAGES" environment variable. Just Set "NUGET_PACKAGES" = "c:\teampackages". Or you can place a NuGet.Config file next to the solution with the following content:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
</configuration>
For the detail info, you can refer to the same issue on GitHub.
However, what I want to say is that you might not be happy with the results. Because .Net Core projects rely on so many NuGet packages. Almost hovering around 1GB. I think you will not want to set all those packages for each solution. Besides, with this change, you need to set up additional sources in VS for every solution. It`s quite inconvenient for .net core project. This is the reason why NuGet team set the special settings for .net core projects.
回答2:
You can create a Nuget.config in your src directory and set the repositoryPath
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="..\packages" />
</config>
</configuration>
回答3:
You can create a local nuget repository for offline development and instead of having a packages directory use local repo. here is guide:
CREATING A LOCAL NUGET REPOSITORY FOR OFFLINE DEVELOPMENT
来源:https://stackoverflow.com/questions/45428808/referencing-nuget-packages-from-directory-in-solution