Is it possible to change the location of packages for NuGet?

前端 未结 15 1809
误落风尘
误落风尘 2020-11-22 17:11

I have the following convention for most of my projects:

/src
    /Solution.sln
    /SolutionFolder
        /Project1
        /Project2
        /etc..
/lib
          


        
相关标签:
15条回答
  • 2020-11-22 17:32

    It's now possible to control which folder the packages are installed into.

    http://nuget.codeplex.com/workitem/215

    Edit: See Phil Haack's comment on Dec 10 2010 at 11:45 PM (in the work item/the link above). The support is partially implemented in 1.0, but is not documented.

    According to @dfowler: Add a nuget.config file next to the solution with this:

    <settings>
    <repositoryPath>{some path here}</repositoryPath>
    </settings>
    

    There is a nuget package for creating the package folder override.

    Update for version 2.1

    As Azat commented, there is now official documentation on how to control the package locations. The release notes for 2.1 specifies the following configuration in a nuget.config file (see the release notes for a description of valid places to put the config files and how the hierarchical configuration model works):

    <configuration>
      <config>
        <add key="repositoryPath" value="C:\thePathToMyPackagesFolder" />
      </config>
      ... 
    </configuration>
    

    This would change the packages folder for the configuration level you put the file in (solution if you put it in the solution directory, project in project directory and so on). Note that the release notes state:

    [...] if you have an existing packages folder underneath your solution root, you will need to delete it before NuGet will place packages in the new location.

    0 讨论(0)
  • 2020-11-22 17:35

    The most consistent way is by using nuget config to explicitly set the config:

    nuget config -set repositoryPath=c:\packages -configfile c:\my.config
    

    https://docs.microsoft.com/en-us/nuget/consume-packages/configuring-nuget-behavior#changing-config-settings

    0 讨论(0)
  • 2020-11-22 17:36

    None of this answers was working for me (Nuget 2.8.6) because of missing some tips, will try to add them here as it might be useful for others.

    After reading the following sources:
    https://docs.nuget.org/consume/NuGet-Config-Settings
    https://github.com/NuGet/Home/issues/1346
    It appears that

    1. To make working Install-Package properly with different repositoryPath you need to use forward slashes, it's because they are using Uri object to parse location.
    2. Without $ on the begining it was still ignoring my settings.
    3. NuGet caches config file, so after modifications you need to reload solution/VS.
    4. I had also strange issue while using command of NuGet.exe to set this option, as it modified my global NuGet.exe under AppData\Roaming\NuGet and started to restore packages there (Since that file has higher priority, just guessing).

    E.g.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <solution>
        <add key="disableSourceControlIntegration" value="true" />
      </solution>
      <config>
        <add key="repositorypath" value="$/../../../Common/packages" />
      </config>
    </configuration>
    

    You can also use NuGet command to ensure that syntax will be correct like this:

    NuGet.exe config -Set repositoryPath=$/../../../Common/packages -ConfigFile NuGet.Config
    
    0 讨论(0)
  • 2020-11-22 17:38

    UPDATE for VS 2017:

    Looks people in Nuget team finally started to use Nuget themselves which helped them to find and fix several important things. So now (if I'm not mistaken, as still didn't migrated to VS 2017) the below is not necessary any more. You should be able to set the "repositoryPath" to a local folder and it will work. Even you can leave it at all as by default restore location moved out of solution folders to machine level. Again - I still didn't test it by myself

    VS 2015 and earlier

    Just a tip to other answers (specifically this):

    Location of the NuGet Package folder can be changed via configuration, but VisualStudio still reference assemblies in this folder relatively:

    <HintPath>..\..\..\..\..\..\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>
    

    To workaround this (until a better solution) I used subst command to create a virtual drive which points to a new location of the Packages folder:

    subst N: C:\Development\NuGet\Packages
    

    Now when adding a new NuGet package, the project reference use its absolute location:

    <HintPath>N:\SomeAssembly\lib\net45\SomeAssembly.dll</HintPath>
    

    Note:

    1. Such a virtual drive will be deleted after restart, so make sure you handle it
    2. Don't forget to replace existing references in project files.
    0 讨论(0)
  • 2020-11-22 17:40

    The config file in the accepted answer works for me in VS2012. However, for me it only works when I do the following:

    1. Create a new project in VS.
    2. Exit VS - this seems to be important.
    3. Copy the config files to the project folder.
    4. Restart VS and add packages.

    If I follow those steps I can use a shared package folder.

    0 讨论(0)
  • 2020-11-22 17:41

    In addition to Shane Kms answer, if you've activated Nuget Package Restore, you edit the NuGet.config located in the .nuget-folder as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <repositoryPath>..\..\ExtLibs\Packages</repositoryPath>
    </configuration>
    

    Notice the extra "..\", as it backtracks from the .nuget-folder and not the solution folder.

    0 讨论(0)
提交回复
热议问题