Creating the metadata in NuGet spec file

Go to the root directory of your project and copy the downloaded CLI file, nuget.exe, there. Open a console window, navigate to the project directory, and enter either of the following commands to create the nuspec file:

    nuget spec
    nuget spec NuGetDemoLibrary.csproj
    nuget spec binReleaseNuGetDemoLibrary.dll

For the previously mentioned NuGet commands, please note the following:

  • If you use the first command nuget spec, it will create the nuspec file based on the available project files in the directory where it was executed.
  • The nuget spec <ProjectFilePath> will create the nuspec file only for the project that you specified.
  • If you use a specific DLL, in this fashion-- nuget spec <DllFilePath>, the NuGet spec file will be created based on the specified DLL. This command is mostly recommended as it creates the metadata information of the nuspec file from the DLL metadata.

Have a look at the following screenshot to understand it easily:

This will create the NuGetDemoLibrary.nuspec file with default values:

If you create the nuspec file from the DLL, most of these metadata entries will have a value picked from the generated DLL. As we have created it from the project, it has a placeholder field against most of the entries. You must replace these default values before generating the package.

The id field must be unique across the NuGet global store or the gallery where you are going to host the package. Generally, it gets populated from the AssemblyName field of the DLL. Do a check in the store or the gallery before assigning this field.

The licenseUrl, projectUrl, and iconUrl are optional. If you don't want to put in these links, you can remove the entire lines.

NuGet uses semantic versioning, where it has three parts for the version field in the following convention: Major.Minor.Patch, thus put a three-fields version number against it. Whenever you upload a new version to NuGet, it decides if it is the latest stable version. If you are not releasing a final version, you can inform NuGet to mark it as a pre-release version by suffixing the -prerelease switch or -alpha, -beta, or -gamma to the version field. Technically, you can add any string there but it is always preferable to put a meaningful convention. For example, if your build version is 4.5.2 and you want to set it as a pre-release beta version, change the value of the version field to 4.5.2-beta.

Note that NuGet prioritizes the version field in reverse chronological order. Thus, -beta will get higher preference than -alpha, and the final release version will get the highest preference automatically.

Fill all other fields mentioned under the metadata tag inside the spec file and save it. Now you need to specify the files you are going to bundle and in which folder you want to place them when downloaded from NuGet. Generally, when you download from NuGet, they are placed inside the package folder of your solution directory, categorized by package type. Here, you can also define a Framework-specific folder path, which we will discuss later in this chapter.

Let's add the output of our class library project and mark it to place under the lib folder of the target directory. Enter the following XML tag inside the package element with the path of the precompiled binary assembly:

  <files> 
     <file src="binReleaseNuGetDemoLibrary.dll"  
           target="libNuGetDemoLibrary.dll"/> 
  </files> 

Let's update the details and save the file. The resultant output of our .nuspec file will look like the following block:

  <?xml version="1.0"?> 
  <package > 
     <metadata> 
           <id>Demo.Packt.Kunal.NuGetDemoLibrary</id> 
           <version>1.0.0-alpha</version> 
           <title>NuGet Demo Library</title> 
           <authors>Kunal Chowdhury</authors> 
           <owners>WinLab</owners> 
           <description>This is for demo purpose only</description> 
           <releaseNotes>Change 1, Change 2, ...</releaseNotes> 
           <copyright>Copyright 2017</copyright> 
           <tags>Demo, Packt</tags> 
     </metadata> 
     <files> 
        <file src="binReleaseNuGetDemoLibrary.dll"  
           target="libNuGetDemoLibrary.dll"/> 
     </files> 
  </package> 

You can add multiple precompiled assemblies inside the package by defining them inside the <files>...</files> element:

  <files> 
     <file src="Assembly-1.dll" target="libAssembly-1.dll"/> 
     <file src="Assembly-2.dll" target="libAssembly-2.dll"/> 
     <file src="Assembly-3.dll" target="libAssembly-3.dll"/> 
  </files> 

You can also use wildcards in the <files> section, as follows:

  <files> 
     <file src="*.dll" target="lib "/> 
  </files> 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.36.141