.NET life

HUGON Jérôme
Microsoft Certified Technology Specialist Microsoft Certified Application Developer Microsoft Certified Professional

Create your first NuGet package

Visual Studio

Category Visual Studio  | Publication Date : 4/4/2011

NuGet is a true evolution to package up your development to facilitate their reuse for other projects. This article describes the basics of creating NuGet packages.

 

Download the command line tool

The first thing to do is to download the command line tool NuGet on CodePlex.

 

Create the specification file

Execute the following command to create the NuGet specification file:

NuGet spec

The file Package.nuspec is generated. It's a XML based file:

<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
  <metadata>
    <id>Package</id>
   <version>1.0</version>
    <authors>Author here</authors>
   <owners>Owner here</owners>
   <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
   <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
   <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
   <requireLicenseAcceptance>false</requireLicenseAcceptance>
   <description>Package description</description>
   <tags>Tag1 Tag2</tags>
    <dependencies>
      <dependency id="SampleDependency" version="1.0" />
    </dependencies>
  </metadata>
</package>

An explanation about metadata:

  • id: The package unique identifier, this is the name which will be visible when packages will be listed.
  • version: The package version.
  • authors: The comma separated list of package authors.
  • owners: The comma separated list of package owners.
  • licenseUrl: Link to the package license details.
  • projectUrl: Link to project of the package.
  • iconUrl: Link for icon package, it is recommended to had an image of 32x32 pixels with a transparent background.
  • requiredLicenseAcceptance: A boolean indicating whether the license should be accepted before installing the package.
  • description: The package description.
  • tags: The list of keywords related to the package separated by spaces to search for.
  • dependencies: The list of dependencies required by the package.

Other element are also available:

  • title: The package name.
  • summary: The package summary.
  • language: The package language.
  • frameworkAssemblies: The list of .NET framework references required by the package.
  • filesThe list of files to include in the package and which are not included in the file content. More details on this item could be found on CodePlex.

The details of the XML schema of NuGet specification files is available on CodePlex.

 

Add file to the package

To add files to the package other than specifying them in the spec file, you can create a folder called content at the same level as the spec file. This folder represents the root of the project that will install the package.The files and folders included in it will be merged with the target project.

You can edit the Web.config file extension by adding .transform, this will effect a merger of its content with the configuration file of the project target.

Similarly, you can change the file extension of code by adding the extension .pp, at the time of installation, the extension will disappear and the keywords within the file will be replaced. For example, you can use the keyword $rootnamespace$ to be replaced by the namespace of the project target time of installation. You can use any project properties surrounded by characters in $ replacements. This list is available on the MSDN.

 

Create the package

To create a package, run the following command in the folder containing the specificationfile:

NuGet pack

A file with the id of package followed by the version and with the extension .nupkg be created from the specification file.

You can then install your package in your project.


Related articles

TechDays 2011 - What's new in ASP.NET MVC 3

MVC 3

Category MVC 3  | Publication Date : 4/16/2011