The argument of this topic is how we can generate a NuGet package from a .NET project and what are the main options that we can specify from the console command.
Writing and organizing our code and applications in separated modules is very important, will help us to reuse the code, to optimize the applications by reducing the number of the projects in the same solution, to share some behaviours across different applications.
As you know, NuGet is the main package manager used in the .NET applications and besides provides a huge number of packages from the public directory, it allow us to publish our packages onto the public directory or in a out NuGet server.
Assuming that the application is ready, let’s go to download the command line tool that we’ll use to configure the package and push it to the NuGet server.
First of all, we need to download the command line tool, that we can found at this link.
It is an executable file that we can put everywhere on our machine; anyway, in order to use it we’ll need to add that path to environment variables of the pc.
Assuming that we have done this work, we can open a console command and go to the folder of the .NET project for which we want to generate the package.
Then we have to generate the nuspec file, that is the configuration file of the package; we can do that with this command:
Now in the project directory we’ll find a new file .nuspec, that is the configuration file of the package.
In this file we have some options, but we can say that a basic configuration may look like this:
<?xml version="1.0"?> <package > <metadata> <id>NugetTest</id> <version>1.0.0</version> <title>NugetTest</title> <description>NugetTest</description> <authors>Mirko Maggioni</authors> <owners>Mirko Maggioni</owners> <requireLicenseAcceptance>false</requireLicenseAcceptance> <language>it-IT</language> </metadata> </package>
It’s pretty simple, we have specified an id for the package, a version, a title, the authors and so on.
I thinks that a particular section of this file deserves a particular explanation; with this basic configuration, the only dll that will be imported in the target project will be the dll of the current project; no others files will be referenced in the target project.
But what we need to do if the we have to include some additional dlls in the package? Or if we need to include some dlls based on a specific version of the framework? Or if we need to include some content files?
The section file help us with this work:
<?xml version="1.0"?> <package > ..... <files> <file src="bin\Release\test1.dll" target="lib\net35\test1.dll"/> <file src="bin\Release\test2.dll" target="lib\net45\test2.dll"/> <file src="testFolder\**" target="content\testFolder"/> </files> </package>
With the src attribute we specify the file that we want to include in the project and with target where the file will be copied in the target application.
With lib word we say that the file will be referenced as a dependency of the application; with the second part of the path we can specify if the dll has a target framework, so the dll will be referenced only in the projects that satisfy the framework version.
If we want instead include a content file, we need to use the folder content in the initial part of the target path; the file will be copied in the root of the project, unless we have specified a subfolder, in our case testFolder.
We can have other two options for the target paths that are build (concerns msbuild files) and tools (eventually powershell files); anyway you can find more documentation here.
Now the configuration is ready, and we can proceed with the package generation and publishing.
We can generate the package for our application with this command:
nuget.exe pack nugettest.csproj -IncludeReferencedProjects -Properties Configuration=Release -Version 1.0.0
Pretty simple, we have specified the project file name, the configuration and the version that we want to publish; we can find a .nupkg file in the root folder.
This is nothing else that a zip file and if we open it will look like this:
After that we can publish the package, with this command:
nuget.exe push NugetTest.1.0.0.nupkg myapikey -source http://myhostname
If all is fine, we can now install the package in the target application with an install-package NugetTest.
The application will looks like this:
That’s all, we has been able to configure a nuget package for a source application with some additional files, and install those on a target application.