Skip to content

Automate NuGet package creation using Grunt

Probably you are already using Grunt, to automate the tasks to build your application or library. In metro-bootstrap we are using Grunt to automate the build process, but until now there's a last step that we are doing manually: create the NuGet package for new releases, until today.

How we automate the package build?

It's simpler than it seems.

1. Install nuget

First make sure that you have nuget.exe in your machine.

Is NuGet.exe in your PATH?

  1. Open a command prompt.
  2. Type nuget and hit enter.
  • If the nuget help documentation is displayed, then nuget.exe is already in your PATH.
  • If you received the message, "'nuget' is not recognized as an internal or external command, operable program or batch file." then nuget.exe is not in your PATH.

How to add NuGet.exe to your PATH?

  1. Open a command prompt.
  2. Enter the following command: set PATH=%PATH%;C:\nuget where "c:\nuget" is the path to the nuget.exe (available here).

2. Create the NuGet definition file

Create the following XML file with the definition for the package with the ".nuspec" extension.

<?xml version="1.0"?>
<package xmlns="">
ID used to identify the nuget package

Package version - leave this blank

<!-- Author, Owner and Licensing details -->
<copyright>Copyright 2014</copyright>
<!-- General Information -->
<description>metro-bootstrap: Twitter Bootstrap with Metro style</description>
<tags>twitter bootstrap css metro responsive html5 talkslab</tags>
Packages (incl. minimum version) on which this package

<dependency id="Twitter.Bootstrap.Less" version="" />
<file src="app\less\*" target="content\Content\metro-bootstrap" />
<file src="dist\css\metro-bootstrap.css" target="content\Content" />
<file src="dist\css\metro-bootstrap.min.css" target="content\Content" />

Define in the files element the files to copy to the package. If you want to test the nuspec file, just type the following command in the command line (where mypackage.nuspec is the name of the nuspec file).

nuget pack mypackage.nuspec

3. Create grunt task to build the NuGet package

In order to create the file, go to the grunt script and add the following task (just rename the definition file name):

grunt.registerTask("nuget", "Create a nuget package", function () {
var done = this.async();
//invoke nuget.exe
cmd: "nuget.exe",
args: [
//definition file

//path where the package should be created

//override the version using the version in package.json
function (error, result) {
if (error) {
} else {

Now, just open the command line and run the "grunt nuget" command, and a nuget package will be created.

The configuration used in this post is running on metro-bootstrap library. Fill free to fork it and try.

I hope this helps you.