nuget.exe command line keeps prompting for username/password - msbuild

I am trying to use NuGet packages with a pre-existing nightly build system, so I need to automatically check for missing / out of date NuGet package and update them if needed.
So far I've created a Visual Studio 2019 project which has a dependency for a NuGet package that exists inside my JFrog Artifactory repo online.
If I run the command nuget restore demopack3.sln in command prompt on my VS2019 solution file (.sln) it prompts me for my artifactory userName / password.
After I input my info, it correctly downloads the NuGet package. And I can run the command / delete my nuget package multiple times without being prompted for my username/password again. But occasionally, seemingly at random, it prompts me for my username/password manual entry
this is a problem because I need my nuget restore and msbuild commands to be ran automatically and without user input.
is there any way I can set my Artifactory username/password inside Windows Credentials Manager or some other automated way to avoid needing to manually enter my username/password when downloading NuGet packages from an Artifactory repo?
I am using NuGet Version: 6.1.0.106 in command prompt on windows 10

Related

nuproj.props file is missing

I have a library that I can successfully build locally and on another computer. However, when I try to do the build at VSTS online, I get
classes.csproj(152,5): Error : This project references NuGet
package(s) that are missing on this computer. Use NuGet Package
Restore to download them. For more information, see
http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is
....\classes\packages\NuProj.0.11.30\build\NuProj.props
I have tried multiple times to check the git repository but comparison shows that the VSTS version is the same.
What am I missing? Do I need to delete this VSTS project and simply reload? Hoping not.
From the command line, you need to do a manual nuget restore before building your project. Visual studio can take care of this automatically. But from the command line, msbuild is not running visual studio.
Thus do something like
nuget restore c:\my\path\to\my\project.sln

Can I execute npm install upon the installation of a NuGet package?

I'm creating a NuGet package which contains TypeScript dependencies...node_modules stuff. I don't want to include the node_modules as part of the package, so I'd prefer to execute npm install after the package installation completes. I've installed the package on a test website, and if I manually execute npm install it works.
I can put it in the readme to execute npm install after the package is completed, but I'd prefer to execute it automatically when the install is complete. Is there any way to make that happen?
Historically, we could write an Install.ps1 PowerShell script to invoke certain actions when installing a NuGet package. However, as of NuGet version 3, this feature is no longer supported:
Powershell script support was modified to no longer execute install and uninstall scripts, but init scripts are still executed.
The Init.ps1 script described above allows us to execute actions when a someone installs our package. Although the blog post above suggests that we can still use Init.ps1 scripts in our packages, the lack of recent documentation for this feature and a comment from a NuGet developer seem to indicate that Init.ps1 is also deprecated and that the behavior may not be available in the future. This makes sense if we consider that NuGet also aims to support Mac and Linux for Mono/.NET Core projects, and these systems do not yet include ubiquitous support for PowerShell like Windows (but they may eventually).
For these reasons, I cannot recommend at this time that we automate the package's post-install steps using NuGet's built-in features. The script that runs npm install must also handle challenges on systems where the npm program is missing or not available in the PATH, and some users may not like the idea of the package installing and running arbritrary code. I think it's fair for the project in question to simply instruct the end user in the README to run npm install after installation. Projects with more complex post-installation steps can instruct end users to run a script that executes each step. If you still wish to try using Init.ps1, continue reading:
The current NuGet docs omit information about Init.ps1, but older documentation from 2007 contains this description:
Init.ps1 runs the first time a package is installed in a solution. If the same package is installed into additional projects in the solution, the script is not run during those installations. The script also runs every time the solution is opened. For example, if you install a package, close Visual Studio, and then start Visual Studio and open the solution, the Init.ps1script runs again.
...
These files should be located in the tools directory of your package.
So, we can create an Init.ps1 file in the tools/ directory for our package which might look like the following:
param($installPath, $toolsPath, $package, $project)
Set-Location Path\To\Project\With\Node\Dependencies
npm install
...and then generate the NuGet package. The param() statement for the NuGet convention-based scripts provides the following values:
$installPath is the path to your package install
$toolsPath is the path to the tools directory under the package
$package is your package
$project is the project you are installing the application to. Note: This will be null in Init.ps1. It doesn't have a reference to a particular project because it runs at the solution level.
Because this script runs whenever we load a solution, it could slow down Visual Studio. We can add a condition to the script that checks if the packages are installed before running npm. Note that this functionality is restricted to NuGet operations run from Visual Studio or the Package Manager Console and likely will not work using the standalone NuGet CLI program because of the coupling of these scripts to projects and solutions.
Discussion to reintroduce Install.ps1 to NuGet

How to install Nuget package on Server without internet connection or local repo?

I have a build server that does not have internet access and I want to install this package on it.
I have downloaded nuget.exe and copied it to the server and added it to my PATH and also copied the nuget package above to the server and tried to install with nuget install microsoft.data.tools.msbuild.10.0.61026.nupkg
But the install fails with
Unable to load the service index for source https://api.nuget.org./v3/index.json.
Is there a way to simply install a nupkg file without a repo? The server is Windows Server 2012 R2.
I am assuming you are using NuGet 3 since this used to work in NuGet 2.
Disabling all your online package sources should allow you to install a NuGet package locally.
So you can either change your %APPDATA%\NuGet\NuGet.Config file so it has no package sources. Or you could use the -source parameter, specifying a package source that works offline, when calling install so the online package source is not used.
I was in the wrong folder when I ran the command. I needed to be one folder above and then specify the folder as the source. Worked.
nuget install -source C:\NugetDownloads microsoft.data.tools.msbuild -OutputDirectory E:\Nuget

TeamCity - no artifacts generated with Octopus Deploy / Octopack

I have a Visual Studio project, that's set up in TeamCity with a build-task to create artifacts via OctoPack (3.0.43). Next, a deploy-task, that receives the output NuGet-package from the build-task task (on successful build) as a dependency.
I've tried setting up the build-task using both the MsBuild build-runner and the Visual Studio (sln) one, but in both cases no artifacts are created.
I've added the OctoPack NuGet package to my Visual Studio project.
Found this one out myself:
When I added OctoPack NuGet package, all seemed well. Hinted by various posts, I looked to the OctoPack.targets file. Here I stumbled upon the Install.ps1 file, that seemed to be supposed to inject some build-actions in the VS project's .csproj file upon adding the NuGet package to project.
Next up, I removed the NuGet package and tried re-installing, but this time using the Package Management Console (View > Other Windows > Package Management Console) with command:
Install-Package OctoPack
This output the following:
Successfully added 'OctoPack 3.0.43' to MySolution.MyProject. & :
File
C:\somepath\mysolution\packages\OctoPack.3.0.43\tools\Install.ps1
cannot be loaded because running scripts is disabled on this system.
For more information, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170. At line:1 char:3
So, some security stuff was preventing the PowerShell script to do its stuff with the .csproj file.
Solution:
In the same window (Package Management Console) or in an elevated PowerShell, execute
Set-ExecutionPolicy RemoteSigned
Retry NuGet package-installation (still same window or using the NuGet GUI) - now the NuGet package installer should inject whatever your version of OctoPack wants it to. After this, my TeamCity build started generating OctoPack artifacts correctly.

During build/deploy, how do I get around Nuget packages that only copied files?

Background
I have an MVC3 project to which I added the Twitter.Bootstrap.Less Nuget package.
To my knowledge, all this did was copy the appropriate JavaScript / LESS files into their appropriate directories.
However, when I now run this through my build/deploy process onto my dev server, MSBuild doesn't copy the /Content/less folder to my production server as part of deployment package.
My build server doesn't have an internet connection, so unfortunately using NuGet without committing packages to source control isn't an option.
Question
How do I get MSBuild to deploy these files? Or do I need to copy the files, uninstall the nuget package, and manually copy them back in?
You could setup your own internal NuGet package source. That would be internal to your network and mean the build server could pickup the NuGet packages without an internet connection.
E.g. On the server, copy the packages you need to a folder and setup a package feed to that folder for the build server to use.
See:
http://docs.nuget.org/docs/creating-packages/hosting-your-own-nuget-feeds