Introduction
In this article we will learn how to setup and Create Nuget Package using nuget CLI for Visual Studio Project.
What is Nuget
Nuget is open source package manager tool which creates package like Zip file with extension .nupkg. This package can further used to share the code and for deployment. It is developed by Microsoft.
Architecture
Benefits
With the help of this packaging we can decide which binaries needs to be created as output package which can be further used for deployment.
We can easily include the package version and other metadata as per the package requirement.
Package Core File --> Manifest File .nuspec
nuspec XML file is package manifest file where you can declare what are the assemblies you need to include in the Package as well as versioning, dependency and other metadata.
All properties can be defined under Metadata.
.nuspec file Basic schema
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id></id>
<version></version>
<authors></authors>
<description></description>
</metadata>
<files>
</files>
</package>
Setup Nuget Package
Nuget CLI download
Nuget CLI will be required for the creating the nuspec file and further nuget package.
Edit Environment Variable
Make sure the Nuget CLI is downloaded to the Build Agent/Build Machine and reference is added in the environment variable.
Test the Nuget Installed
Run the nuget through CMD and it should return like below.
nuget
Create .nuspec File
Nuspec file will help to create the package, hence first we need to generate this file for specific Visual Studio project.
Step 1: Open the Root folder of .sln or csproj file
Step 2: Open the CMD & run below command.
cd <Project Path>
nuget spec
Step 4: Edit the newly created .nuspec file and update the detail as per the your requirement.
you can also include the multiple file and folders as part of the packaging.
Example: In this example I have included the files to be added as part of the package creation
<files>
<file src="..\BuildNugetPackage\BuildNugetPackage\bin\Release\**\*.*" target="TargetNugetPackage\bin" />
<file src="bin\*.*" target="TargetNugetPackage\bin" />
</files>
Note: I have marked the version as variable since this can be updated on each build but other metadata properties can be also mark as variable and during the runtime it can be updated.
Sample file:
<?xml version="1.0" encoding="utf-8"?>
<package >
<metadata>
<id>BuildNugetPackage</id>
<version>$version$</version>
<authors>Letsdevops</authors>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<license type="expression">MIT</license>
<!-- <icon>icon.png</icon> -->
<projectUrl>http://project_url_here_or_delete_this_line/</projectUrl>
<description>Learn to Create Nuget Package</description>
<releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
<copyright>LetsDevOps</copyright>
<tags>Tag1 Tag2</tags>
<dependencies>
<group targetFramework=".NETStandard2.1">
<dependency id="SampleDependency" version="1.0.0" />
</group>
</dependencies>
</metadata>
<files>
<!-- Add files from an arbitrary folder that's not necessarily in the project -->
<file src="..\BuildNugetPackage\BuildNugetPackage\bin\Release\**\*.*" target="TargetNugetPackage\bin" />
</files>
</package>
Step 5: Build the Project
Now the Project needs to be build so that assembly files will be created.
Step 6: Create the Package
After the successful build now we are ready to create Package.
Run below command through CMD
cd <Project Path>
nuget pack -properties version=1.2.02332 <You can provide your build version>
On successful execution you can see the package is created.
Step 7: Verify the nuget package [.nupkg]
Package will be created like: <Package>.<Version>.nupkg
Comments