Steps and Ways to Create a Nuget Package

 

Creating a NuGet package for C# involves two main steps: preparing your code and generating the package itself. Here are the common approaches:

1. Using Visual Studio (Windows only):

  • Quick and easy: If you're using Visual Studio and your project is an SDK-style project (.NET Core or later), this is the simplest option.
  • Steps:
    1. Set the build configuration to "Release".
    2. Right-click your project in Solution Explorer and select "Pack".
    3. Visual Studio builds your project and creates the NuGet package (.nupkg file).
  • Optional: Configure automatic package generation on build in project properties.

2. Using the .NET CLI:

  • Flexible and cross-platform: Works on any platform with the .NET CLI installed.
  • Steps:
    1. Create a class library project (or use an existing one).
    2. Open a command prompt in your project directory.
    3. Run dotnet pack to build and create the package.
  • Additional options:
    • Specify the output file with --output <filename.nupkg>.
    • Control dependencies with --include-assets and --exclude-assets.

3. Manual creation (advanced):

  • Full control: This offers maximum flexibility but requires more manual effort.
  • Steps:
  1. Download Nuget.Exe file from https://www.nuget.org/downloads.

  2. Create a Folder where we can create a Nuget Package. for Example C:\Nuget

  3. Copy the Nuget.exe file to this folder.

  4. Go to command prompt, go the folder

  •  Create the nugetspec file (In the cmd window, type “nuget spec”)

         A file will be created with name : Package.nuspec

    5. Create a  Folder called "ClayTablet.6.3.3".

    6. Extract the Sitecore package and copy the content to the newly created folder.

    7. Copy the "Package.nuspec" to the ClayTablet.6.3.3 folder and rename to         "ClayTablet.6.3.3.nuspec"


   8.  Edit ClayTablet.6.3.3.nuspec file and make the changes


9. Now we are ready for creating a nuget package. Go to command prompt and type “nuget pack [nuspec file path]


  10. A Nuget Package will be created in the folder (ClayTablet.6.3.3.nupkg)

Steps to Upload Nuget package in Azure Artifacts


1. Check the Nuget Sources list in your local by typing this command.
    Nuget sources list . You will see all registered Sources list.


2. Check whether your Nuget Source is present, Otherwise we need to register it.

3. Register Using below command

   Nuget sources add -Name "Nuget Source Name" -Source "Nuget Package Source URL"

4. After Registering, check the source list using Nuget sources list

5. Push the Nuget package to Azure Artifacts by using below command

nuget.exe push -Source "Nuget Soucre Name" -ApiKey az "C:\KC\Nuget\ClayTablet.6.3.3.nupkg"

6. After successful publish, You can see that package is uploaded in Azure Artifacts.


Additional tips:

  • Documentation: Provide a good README file with usage instructions and examples.
  • Versioning: Follow semantic versioning for clear versioning scheme.
  • Testing: Test your package thoroughly before publishing.
  • Publishing: Consider publishing your package to nuget.org or a private repository.

Comments