which is good approach to create nuget packages? - nuget

As I developer I wonder which is the best approch to create a nuget packages ?
1.NuGet Package Project (link)
2.Use Nuget.exe use .Nuspec (add manually and update manually)
Anyone guide on this.
Currently I'm using nuget.exe and .Nuspec but problem is everytime I have to manually update .nuspec if any new project is added.
Is there any other good options to do so ?

You can also build a NuGet package by running the nuget pack command against the csproj file. More information can be found here: Creating And Publishing A Package

Create nuget package in the following manner
Download nuget.exe from here https://www.nuget.org/
Create empty spec file (execute below command under project root folder)
nuget spec
Update nuget spec file SomeLib.nuspec according to your library properties(use any text editor)
Create some folders nugetPack/lib/net46/ and then paste the dll here; which you want to make as nuget package
Create nuget package (execute below command under project root folder)
nuget pack -basepath nugetPack SomeLib.nuspec
Now use this nuget package in your project
Set the nuget package source which you have created
Goto Visual Studio > Tool > NuGet Package Manager > Package Sources > here add your nuget package source folder path
Right click and select managae nuget packages on the project where you want to consume nuget package.
Select the right package source from the top right drop down box.
search your nuget package and install.
Another cool way to create nuget package via NuGet Package Explorer
which you can download it from here https://npe.codeplex.com/ then just simply fill the form and save the nuspec file to your local nuget package source folder.
Sample nuspec file whihc has some dependencies
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>your.package.name</id>
<version>1.0.0</version>
<title>package title</title>
<authors>NG</authors>
<owners>NG</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>some description</description>
<summary>some summary</summary>
<copyright>open</copyright>
<language>en-US</language>
<tags>your package keyboards</tags>
<dependencies>
<dependency id="Microsoft.AspNet.WebApi.Core" version="5.2.3" />
<dependency id="Microsoft.AspNet.WebApi.Client" version="5.2.3" />
<dependency id="Newtonsoft.Json" version="9.0.1" />
</dependencies>
</metadata>
</package>
Github README

I'm new to developing nuget packages, (and posting on Stack Overflow). I started using the nuget.exe command-line approach, which I learned about: here. But from what I've gathered so far, there is a convention of using .NET Standard, instead of .NET Framework when developing nuget packages. Especially on nuget.org. So, when developing a .NET Standard class library, check out the project properties under the project tab in Visual Studio. There you will see multiple tabs on the left, starting with build. Click on the Package tab. Now you'll see a great way to enter all properties that you would normally have to do manually when trying to create the .nuspec file. You'll want to be sure to fill it out completely to avoid flags when uploading to nuget or wherever. Create your repository and file in github, and enter their URLs. Also, imgur.com is a great place to host your icon image. Be sure to click the Generate Nuget Package checkbox. Voila! Now build your library and you'll notice a .nupkg file. This is the best resource.

Related

Why isn't there any packages folder in my .NET Core solution's containing folder?

Are packages now cached in a more shared location somewhere or what?
My solution folder is devoid of any packages folder:
Per project: References->Nuget dictates what packages are referenced and restored. But, as Eastrall mentioned, the packages folder is now global and located in your user folder: C:\Users\[YourUsername]\.nuget\packages
To force ./packages folder for Solution
To force download of packages folder to be a child of the solution folder for .NET Core projects, follow these steps:
Create a NuGet.Config file in the same directory as the .sln file.
Copy the following contents into the NuGet.Config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="globalPackagesFolder" value=".\packages" />
</config>
</configuration>
Configure NuGet to download missing packages by:
3.1. In Visual Studio: Tools -> Options
3.2. Filter by nuget (top left in dialog). Select General
3.3. Ensure Allow NuGet to download missing packages is checked
Close and re-open the solution. NuGet will download the required packages.
Note: the NuGet.Config configuration can also be achieved by executing the following command from the NuGet Package Manager Console (Tools -> NuGet Package Manager -> Package Manager Console):
PM> nuget config -set globalPackagesFolder=.\packages -configfile "$(pwd)\NuGet.Config"
You still have packages folder in your .NET Core solution, but the global packages are located at: C:\Users\[YourUsername]\.nuget\packages
You can check out a question I asked to see if the answers do you any good.
How do I include NuGet packages in my solution for .Net Core projects?
You can get that packages folder back, but you might not be happy with the results, since .Net Core projects rely on so many NuGet packages. Mine is hovering around 1 GB.
https://learn.microsoft.com/en-us/nuget/schema/nuget-config-file#config-section

Nuget Dependencies not getting installed

I am running a private Nuget Server locally on IIS.
I am creating packages and uploading them all via commadline using nuget.exe (Later on I have to put this on build server, hence the command line). However there is one issue I am stuck at.
I am trying to declare dependencies. I generate the nuspec file in the folder where the .csproj file is there. Then I manually edit the nuspec file to add this under the metadata tag:
<metadata>
<dependencies>
<group targetFramework=".NETFramework4.5">
<dependency id="DemoProject" version="2.0.0.0" />
</group>
</dependencies>
</metadata>
DemoProject, version 2.0.0.0 is present on the Nuget Server.
The project I am creating package for, MyProj.csproj does not have the reference to the DemoProject added to it via Visual Studio. It is just at the packing time I want to create the dependency. It sounds strange but is needed for some initial validation.
Then I run pack command:
"C:\nuget\NuGet.exe" pack MyProj.csproj -IncludeReferencedProjects -Prop Configuration=Release
Then I push it to NuGet server using command line.
When I do an install via command line, then only MyProj package is present at the install location.
When I use Nuget Package Explorer and create a package, I can use the Package Dependency Editor to specify the dependency. It asks for the URL of my local Nuget Server and then adds the dependency. And when I install that package, it works !!
There seem to be no difference in the generated nuspec file in both the cases.
Obviously Nuget Package manager is doing something which I am missing out on.
Any hints?
More details: When I create a lib folder in the Package manager console and put my dll manually, lib->net45->MyProj.dll, then when I install the package thus created also "does not" install the dependency. Back to reading documentation again.
I know this doesn't directly answer your question but I had an issue with NuGet dependencies and my solution may possibly provide a hint.
DLL#1 was had no NuGet Dependencies. Pushed that to my private repo.
DLL#2 referenced DLL#1 NuGet package. Pushed that to my private repo.
All fine and dandy except from the Application project when I go to "Manage NuGet Packages...", the NuGet Package for DLL#2 is listed on the Browse tab but it shows no dependencies. I was forced to install both DLL#1 AND DLL#2 nuget packages. I desired to install DLL#2 nuget package and get DLL#1 automatically.
The way I fixed this was uninstalling all NuGet packages from DLL#2 solution.
Then going to Tools > Options > NuGet Package Manager > General. Then set the Default package management format to Packages.config and then unchecking the option "Allow format selection on first package install".
Then i installed all the needed NuGet packages. Now when i pack it up and push it up to the server, it shows the proper dependencies when going to "Manage NuGet Packages...".
nuget pack doesn't see the dependencies because its looking for them in the \packages folder. That folder didn't exist because I was using PackageReference for package management.
All worked after switching to Packages.config for package management.
I know the problem I had wasn't the same as yours but if you're not using Packages.config for package management, perhaps its related.
Late answer but the issue with this nuspec is <group targetFramework=".NETFramework4.5">. I feel confident that ".NETFramework4.5" is an invalid targetFramework. A full list of the available Target Frameworks is here.
When working with the Dependency section, you may not need to use the group tag. Any users who encounter similar errors would do well to read through that section. They have samples of proper usage.

how to tell NuGet to install package per project and not solution

I have a class library project which is reused across different solutions.
I would like NuGet to add a toolkit reference to the class library project and store it in the project folder not in the solution folder.
Example:
I have
D:\Projects\MyClassLibrary
D:\Projects\Solution1
D:\Projects\Solution2
Using NuGet, I want to add a toolkit to the MyClassLibrary project.
Right now, if I have Solution1 open, NuGet is adding the toolkit to
D:\Projects\Solution1\packages
I would like to tell NuGet to install the toolkit in the MyClassLibrary\packages folder instead.
How to do this?
You can change the default packages folder location since nuget 2.1:
<configuration>
<config>
<add key=" repositoryPath" value=" C:\myteam\teampackages" />
</config>
...
</configuration>
reference: Specify ‘packages’ Folder Location
-----------------UPDATE-------------------------
you have to enable nuget automatic package restore for the solution.
When you do it nuget create a .nuget folder in the solution root.
Inside that folder there are the file NuGet.Config where you can put this configuration.
-----------------(last) UPDATE 2020-------------------------
nuget has changed since and this answer does not apply anymore

NuGet package restore not working from .nuget\packages.config

I have a project, which uses Chutzpah to run unit tests, however the project does not use Chutzpah any other way, it only uses its command line interface. When I installed the Chutzpah NuGet package, the reference was added to .nuget\packages.config:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Chutzpah" version="2.4.3" />
</packages>
I have package restore enabled for this project and the build successfully restores all other used packages but not Chutzpah. I assume it has to do with the fact, that the package reference is located in .nuget\packages.config and not in a project packages.config file, but I don't know why this happened and what is the correct way to work with this?
The Chutzpah NuGet package was added to the project via Visual Studio to a specific project.
To work around the issue I moved the package reference to a project packages.config file and the restore works fine.
Package Restore will not automatically restore solution-level packages. You will need to add a build step to get NuGet to restore the solution packages. For example:
.nuget/nuget.exe install .nuget/packages.config -o packages
For more information see this post on solution level packages.

Create nuget package with multiple DLLs

Let's say I have a project with this structure:
MyLibrary\
MyLibrary.sln
MyLibrary.Core\
MyLibrary.Core.csproj
MyLibrary.Extensions\
MyLibrary.Extensions.csproj
MyLibrary.Tests\
MyLibrary.Tests.csproj
I want to create a single NuGet package which packages MyLibrary.Core.dll and MyLibrary.Extensions.dll. I can't seem to figure out how to get NuGet to do this. I've tried building a spec file manually and I've tried building one using "nuget spec MyLibrary.Core.csproj". I've tried adding all of the DLLs to a lib/ folder which I understand to be the convention-based mechanism for adding DLLs to the package. In every case I can get the MyLibary.Core.dll to get into the package but the MyLibrary.Extensions.dll does not end up packaged along with it.
TLDR: What is the best practice for creating a NuGet package with multiple projects / assemblies? Is there a tutorial out there that focuses on this? The tutorials I've found all focus on simple single-project demos.
You'll run NuGet on a single project (or nuspec file), but it supports pointers to other projects via the file element. This element uses the names of your project's References, so you avoid having to a) find the location of other project files, and b) copy files to a particular place as a post-build step.
Supposing you have a nuspec file for MyLibrary.Core.csproj, and it references MyLibrary.Extensions and MyLibrary.Tests such that they end up in the bin directory after a build:
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
...
</metadata>
<files>
<file src="bin\Release\MyLibrary.Extensions.dll" target="lib\net40" />
<file src="bin\Release\MyLibrary.Tests.dll" target="lib\net40" />
</files>
</package>
With this setup, all of your references should end up in the appropriate place in the NuGet package. You still have the hard-coded 'Release' in there, but I'd wager most probably don't distribute NuGet packages of their debug builds anyway.
Did you generate a blank nuspec file with:
nuget spec
If you use that file and then put your dlls in a folder under it named lib, it will package them up.
I had a little trouble with trying to generate a nuspec file from a project or dll. Also, if you manually reference any files in the nuspec file, the conventions are not used. This is probably the problem with nuspecs generated from dlls or projects.
Also, if you are trying to run this from a build script that executes in a different folder, you can tell nuget the location of your .\lib folder via the -BasePath command line:
build\nuget.exe pack nuget\Company.Project.nuspec -BasePath nuget\
Have you tried NuGet Package Explorer? Might be the easiest way:
http://nuget.codeplex.com/releases/view/59864
It seems your problem is the same as this question: Why doesn't nuget include the referenced project when packing?. If so, you can use the -includereferencedprojects option (See http://docs.nuget.org/docs/reference/command-line-reference#Pack_Command).
I recently published a solution for this...
My solution enables automatic creation of NuGet packages when you build the solution where each package can contain multiple assemblies, references to both external NuGets and NuGets created during the same build and even include the source code for debugging.
In your case, all you will need to do is add a new class library project to your solution, reference the projects you want to package, then add a post build event.
You can find an article with a walk-through guide here
and the source code here.
i have some tutorial how i did it with windows and visual studio:
create local folder and call it packages like: c:/packages//lib - important to create another folder in folder call it lib and past there dll.
open nuget package explorer - https://npe.codeplex.com/downloads/get/clickOnce/NuGetPackageExplorer.application
the ui very intuitive just add dll and export it to /lib (for tutorial https://blog.zwezdin.com/2014/building-nuget-packages-with-gui-tool/ it in russian but see on pictures the flow it's about 3 clicks)
it will create nuspec file
open GIT BASH - https://git-for-windows.github.io/ and navigate to: cd c: => cd packages (the path of )
*maybe on windows you will need provide developer options for windows's linux stuff (https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/)
in GIT BASH enter command: nuget add -source [options]
where: : the full name of nuspec (include .nuspec)
and: the path of folder lib in folder (c:/packages//lib)
after the action ended successfully
in GIT BASH enter another command:
nuget pack .nuspec
not in this folder you have .nupkg file.
How to install a Nuget Package .nupkg file locally? - tutorial how to add it to visual studio.
I had the same problem and I decided to create Nuget which will allow to create other nugets from chosen project.
Package is deployed on the Nuget.org site. After referencing it in the project You need to add nuspeck file to the projects which should generate the projects.
Project with the required nuspeck file
Last thing which should be done by you is invoke command Create-Nuspec in Package Manager. Than the powershell module will take all libraries which are result of the build it will add also the required dependencies and create the nuget in the output directory.
Description about this package is placed here.
I had an issue when adding extra dlls references to a Nuget packages, and testing the package on a sample project, the extra dlls was not being added apparently, no matter the way that I create the Nuget Package.
Then I released that when you uninstall and install again a local Nuget Package with the same version number the changes no take effect, the extra dlls are not added.
So each time you uninstall the package, close visual studio and clear the Nuget Cache,
How to clear NuGet package cache using command line?
Then open again Visual Studio and reinstall the local package to make the change takes effect.
Or make the package version to increase each time to be for Visual Studio to recognize your changes.
For example:
Package-1.0.0
Package-1.0.1
Package-1.0.2
.....
To create a Nuget package from a sample project there are some ways, for example:
Right click to your Visual Studio project and choose the "Pack" option
Then install the nuget package explorer:
https://www.microsoft.com/es-ec/p/nuget-package-explorer/9wzdncrdmdm3?activetab=pivot:overviewtab
And add manually your extra references (dlls files) to your specific targets.
Create a nuget package using a .nuspec file how specified in the documentation:
https://learn.microsoft.com/en-us/nuget/guides/create-packages-for-xamarin
<files>
<file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.dll" />
<file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.xml" />
<file src="Plugin.LoggingLibrary.iOS\bin\Release\iOsDependence.dll" target="lib\Xamarin.iOS10\iOsDependence.dll" />
<file src="Plugin.LoggingLibrary.Android\bin\Release\AndroidDependence.dll" target="lib\MonoAndroid10\AndroidDependence.dll" />
</files>
In your files part add your .dll files.
And dont forget, each time you uninstall and install again the nuget package from your local source.
Or you increase the version of the package each time:
Or close Visual Studio, clean the nuget cache and Rebuild your project.
In order to take effect the changes.