简体   繁体   中英

Is there a way to make NuGet Package Source settings per Project?

How do I set the Nuget location in a csproject in Net core?

We have Microsoft official nuget packages which should be referring to Nuget.org.

Other packages which should refer to C:\NugetFileShare, (Nugets for company class libraries in a private Nuget repository)

Currently, we are using Package Reference format.

Is there anyway in the Project xml file to state the Nuget location for each, and ones which require Private Nuget location?

The following designations from a Solution level, curious if it can be done from project level scope? Referencing NuGet packages from directory in solution

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <config>
      <add key="globalPackagesFolder" value=".\packages" />
    </config>
</configuration>

Other Resources:

Is there a way to make NuGet Package Source settings per solution?

Is there a way? yes. Will it work how you want? probably not.

It's also not entirely clear what you really want (this feels like a case of XY-problem . If you really do want per-project sources, you didn't explain why), as the nuget.config snippet you provided sets the location that NuGet will download and extract the packages. The locations NuGet searches for packages to download is in the packageSources section . Why do you want the sources to be different per project? If you have the mistaken belief that there can be only one source, that's not correct. It's very normal for a solution to have multiple package sources. Even the .NET base class libraries code (corefx) uses 3 package sources and NuGet itself uses 7 sources .

So, what you probably want is a nuget.config file that looks like this, in the same directory as your .sln file, or in your repo root:

<configuration>
  <packageSources>
    <clear /> <!-- clear to make sure that everyone uses the same sources -->
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    <add key="company packages" value="C:\NugetFileShare" />
  </packageSources>
</configuration>

Now, if there really is a good reason why you want per-project package sources, the first thing to note is that it may not work in Visual Studio.

One way is to simply have a nuget.config file in your project directory (this will definitely not work in VS). Then, when you directly restore just that project (restoring a solution file will not take this config into account), it will use that config file.

Since you tagged .NET Core, and .NET Core can only use SDK-style project, and SDK-style projects only use PackageReference, and PackageReference is restored with MSBuild, we can look at NuGet's MSBuild restore task to see if there is anything that can be used to achieve this goal. This line uses a property named RestoreAdditionalProjectSources . Unfortunately I can't guess the syntax it wants, but you could try using <RestoreAdditionalProjectSources>C:\NugetFileShare</RestoreAdditionalProjectSources> in any <PropertyGroup> in your project file. Since the property's name is "additional project sources", it sounds like it should certainly work when restoring from the command line, even when restoring the solution. I have no idea if it works in Visual Studio. This MSBuild property won't work for projects using packages.config .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM