This document describes the steps to add, remove or restore NuGet packages in your application when the build environment does not include Visual Studio (e.g. you are developing a .Net Core application using a text editor and either the NuGet or dotnet CLIs, using Visual Studio Online, or configuring package restore in a build rig) .
See Installing NuGet client tools for approaches to installing the Nuget CLI on Windows, MacOS and Linux.
Authenticating to the NuGet feed
To enable automatic authentication when using the dotnet or nuget CLIs you will need to store your Software Potential credentials in a NuGet.Config file (see a sample here).
The config file can be manually created in a text editor and saved to one of three locations on the dev/build machine depending on whether the configuration is intended to operate in project, user or machine level scope (see Microsoft reference for configuring NuGet behavior).
For example, on a Linux machine the following config file could be saved as NuGet.Config at ~/.nuget/NuGet for user level scope:
<?xml version="1.0" encoding="utf-8"?> <configuration> <packageSources> <add key="https://www.nuget.org/api/v2/" value="https://www.nuget.org/api/v2/" /> <add key="SoftwarePotential" value="https://srv.softwarepotential.com/NuGet/nuget" /> </packageSources> <packageSourceCredentials> <SoftwarePotential> <add key="Username" value="user" /> <add key="ClearTextPassword" value="my_password" /> </SoftwarePotential> </packageSourceCredentials> </configuration>
Alternatively, if the nuget CLI is installed, you can create a minimal config file manually, and then use the CLI to set the required values on the command line. On Windows, this has the advantage of encrypting the stored password. To do this:
- First create a new empty config file by saving the following xml template at the appropriate location e.g. in ~/.nuget/NuGet:
<?xml version="1.0" encoding="utf-8"?> <configuration> </configuration>
- Run the following command to set the Software Potential feed source and your user credentials:
nuget sources add -Name "SoftwarePotential" -source "https://srv.softwarepotential.com/NuGet/nuget" -User "user" -pass my_password -configFile "~/.nuget/NuGet/NuGet.Config"
Note that:
- This will also add the default NuGet source.
- If you are adding credentials to the SoftwarePotential source already configured in the Nuget.config file please use the “Update” option rather than "Add" (as duplicate entries for a source are not permitted).
- Encrypted passwords are not supported on platforms other than Windows at the time of writing. In Linux or MacOs environments the --StorePasswordInClearText option should be added to this command.
Managing NuGet Packages
Once authenticated to the Software Potential source you can access your NuGet packages for installation in your projects. The NuGet feed can serve multiple versions of a given NuGet Permutation package and multiple revisions of each version.
NuGet packages can be managed (added, removed, updated or restored) using either the dontnet CLI or the Nuget CLI.
Using NuGet CLI
The nuget.exe CLI is for .NET Framework projects and requires a packages.config file for package references. See here for more details on using NuGet CLI for installing/restoring packages.
The CLI tool looks for a packages.config file in the current directory, unless a file path is specified in the command options.
Adding and removing packages
Packages can be added to the Packages Directory in your solution using the install command:
nuget install SoftwarePotential.Protection-<YourPermutationShortcode> -OutputDirectory packages
Restoring Packages
Any packages included in the packages.config file but missing from the packages folder can be downloaded and installed using the restore command.
nuget restore <projdir>\packages.config -PackagesDirectory packages
See the following articles for more details on these commands - nuget install and nuget restore.
Using dotnet CLI
Adding or Removing Packages
Packages can be added to and removed from your project from the command line using the dotnet CLI
dotnet add package SoftwarePotential.Protection-<YourPermutationShortcode>
or
dotnet remove package SoftwarePotential.Protection-<YourPermutationShortcode>
See the following articles for more details on the above - dotnet add package reference and dotnet remove package reference.
Restoring Packages
Packages are restored by the "dotnet restore" command. In addition, packages are restored by default by all commands that require a restore to occur, such as "dotnet new", "dotnet build" and "dotnet run" (see dotnet restore reference).
Comments
0 comments
Article is closed for comments.