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

前端 未结 15 1808
误落风尘
误落风尘 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:42

    The solution proposed in release notes for 2.1 doesn't work out-of-the-box. They forgot to mention that there is code:

    internal string ResolveInstallPath()
    {
        if (!string.IsNullOrEmpty(this.OutputDirectory))
        {
            return this.OutputDirectory;
        }
        ISettings settings = this._configSettings;
    
        ...
    }
    

    which prevents it from working. To fix this you need to modify your NuGet.targets file and remove 'OutputDirectory' parameter:

        <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(RequireConsentSwitch)</RestoreCommand>
    

    So now, if you add 'repositoryPath' config somewhere in NuGet.config (see the release notes for a description of valid places to put the config files), it will restore all packages into single location, but... Your .csproj still contains hints to assemblies written as relative paths...

    I still don't understand why they went hard way instead of changing PackageManager so it would add hint paths relative to PackagesDir. That's the way I do manually to have different package locations locally (on my desktop) and on build agent.

    <Reference Include="Autofac.Configuration, Version=2.6.3.862, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
      <Private>True</Private>
      <HintPath>$(PackagesDir)\Autofac.2.6.3.862\lib\NET40\Autofac.Configuration.dll</HintPath>
    </Reference>
    
    0 讨论(0)
  • 2020-11-22 17:43

    For .NET Core projects and Visual Studio 2017 I was able to restore all packages to relative path by providing this configuration:

    <configuration>
      <config>
        <add key="globalPackagesFolder" value="lib" />
      </config>
      ... 
    </configuration>
    

    Based on my experience the lib folder was created on the same level where Nuget.config was found, no matter where sln file was. I tested and the behavior is same for command line dotnet restore, and Visual Studio 2017 rebuild

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

    Just updating with Nuget 2.8.3. To change the location of installed packages , I enabled package restore from right clicking solution. Edited NuGet.Config and added these lines :

      <config>
        <add key="repositorypath" value="..\Core\Packages" />
      </config>
    

    Then rebuilt the solution, it downloaded all packages to my desired folder and updated references automatically.

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