DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. How to Create and Publish NuGet Packages Using .NET Core CLI

How to Create and Publish NuGet Packages Using .NET Core CLI

In this post, we take a look at how to create NuGet packages using CLI commands, and then how to publish these packages with CLI commands.

Neel Bhatt user avatar by
Neel Bhatt
CORE ·
Jul. 03, 18 · Tutorial
Like (3)
Save
Tweet
Share
26.10K Views

Join the DZone community and get the full member experience.

Join For Free

In my previous post on CLI tools, I explained how to create, restore, clean, build, and run a .NET Core application using the latest CLI commands. You can find that post here.

In this post, we will learn how to create and publish NuGet packages using CLI commands.

What Is a NuGet Package?

The official NuGet documentation sums up the importance of NuGet as follows:

An essential tool for any modern development platform is a mechanism through which developers can create, share, and consume useful code. Often, such code is bundled into "packages" that contain compiled code (as DLLs) along with other content needed in the projects that consume these packages.

For .NET (including .NET Core), the Microsoft-supported mechanism for sharing code is NuGet, which defines how packages for .NET are created, hosted, and consumed, and provides the tools for each of those roles.

Put simply, a NuGet package is a single ZIP file with the .nupkg extension that contains compiled code (DLLs), other files related to that code, and a descriptive manifest that includes information like the package's version number.

As you can see in above image, we can build/pack a whole project into a NuGet Package and we can then publish this NuGet package to either nuget.org or any other private host. Once published, we can use this package within .NET projects or we can even install/deploy this package to any server.

As a developer, you should be familiar with NuGet packages because they are a really important part of .NET projects.

For this post, we will use the same application we created in Part I of this series.

Nuspec Files

Nuspec files are very important for NuGet packages.

NuGet packages require manifests which describe the package dependencies and the content, this manifest is  the .nuspec file.

As per Microsoft's docs:

A .nuspec file is an XML manifest that contains package metadata. This manifest is used both to build the package and to provide information to consumers. The manifest is always included in a package.

It is a good practice to create a Nuspec file in which you mention all the dependencies and the details of the package, otherwise, the system will generate the Nuspec file when we run the pack command.

If you do not wish to create a Nuspec file, it is good to at least to add some details in your .csproj file, which will be used to generate the Nuspec file. Add the below properties in the .csproj file:

<PackageId>SampleCliApp</PackageId>
<Version>1.0.0</Version>
<Authors>Neel</Authors>
<Company>CompanyX</Company>

You can add the same details from Visual Studio 2017. For this, right-click on project -> Click Properties -> Open the Package tab and fill in the details:

More details on Nuspec can be found in the Microsoft docs.

Create NuGet Packages

Web applications are not packable by default. To change this, modify your .csproj file to include this property:

<IsPackable>true</IsPackable>

Once the property is added, run the below command to create a NuGet package:

dotnet pack

A NuGet package would be created at the location, bin\debug\.

Check here if you want to see different options for working with the dotnet pack command.

Publish Package

As we have just created the NuGet package, we need some space to store our packages.

Nuget.org provides a space where you can store your NuGet packages, but they are public so other developers can also see your packages. If you do not wish to use nuget.org, there are other options like:

  • Visual Studio Team Services Package Management, which is also available on Team Foundation Server 2017 and later.
  • MyGet
  • ProGet from Inedo
  • NuGet Server, a community project from Inedo
  • NuGet Server (Open Source), an open source implementation similar to Inedo’s NuGet Server
  • Artifactory from JFrog
  • Nexus from Sonatype
  • TeamCity from JetBrains

More details on NuGet hosting packages can be found here.

For this post, we will publish the package to nuget.org

Publish to Nuget.org

We can publish our packages to nuget.org once we have the API keys. Let's go step-by-step to get the key and then to publish the packages using that key.

To publish the packages to nuget.org, we need to create an account.

Once your account is created, click on your account name -> Click on API keys:

Create a new API key:Once the keys are created, note down the key:

Important Note: You can not copy this key later on because you will be required to create new keys \ the second time, so save the keys at the time of creation.

Once we have the key, we can use the dotnet nuget push command, as demonstrated below:

dotnet nuget push SampleCliApp.1.0.0.nupkg -k yourApiKey -s https://api.nuget.org/v3/index.json

Here -k stands for API keys and -s stands for source; in our case, the source is nuget.org so we provided the path correctly.

As you can see in the logs, the packages are pushed to nuget.org.

Let us go back to nuget.org to check our packages:

You can Update/Delete the packages.

Have a look here if you want to see different options for using dotnet nuget push.

Browsing Our NuGet Package

Once we have published our package to nuget.org, we should be able to browse this package from Visual Studio.

Open up Visual Studio -> Manage NuGet package -> Search for your package. In my case, I searched for SampleCliApp:

The package is available, as you can see above!

In future posts, I will explain how to use CLI commands to work with CI (Continuous integration).

Hope this helps!

NuGet Command-line interface .NET

Published at DZone with permission of Neel Bhatt, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • Distributed SQL: An Alternative to Database Sharding
  • The Future of Cloud Engineering Evolves

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: