Friday, January 6, 2017

How to create and publish NuGet package from .NET project using AppVeyor

It is not an uncommon task to create a NuGet package from your code and publish it on some NuGet feed. Here I'll explain how to automate this task using AppVeyor.

AppVeyor provides an easy way to create and publish your NuGet packages. It gives you two types of feeds: per account and per project. Here I'll explain work with account feed, but work with project feed is essentially the same.

Simple way


The simplest way to create and publish NuGet packages from your .NET project is the following:

1. Go to the directory where your *.csproj (or *.vbproj) exists.
2. In this directory create file with the same name as your *.csproj (*.vbproj) but with .nuspec extension. For example, if name of your project file is RationalNumbers.csproj then you should create file RationalNumbers.nuspec. Content of this file may be the following:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <copyright>Copyright 2017</copyright>
  </metadata>
</package>

If you don't want to create this file manually you can use NuGet CLI. Install it on your development machine and in the directory with your project file execute command:

nuget spec

It will create the .nuspec file with correct name and content.

As you can see there are strange lines like $version$ in this file. These are templates of substitution. During generation of NuGet package these templates will be replaced with information from your AssemblyInfo.cs file and other places. You can read more about these substitutions here.
3. Now commit your changes to VCS and go to your AppVeyor project settings. You'll need Build tab. Here under "Automatic packaging" section check "Package NuGet projects" checkbox:

4. Save your changes and rebuild your project. In this case AppVeyor will do the following procedure. If it sees .nuspec file in the same directory as .csproj (*.vbproj) file and with the same name then it will create NuGet package from it and place it into project and account feeds.
5. Now you can use your feeds to get your brand new NuGet packed into your projects. You just need to get URL of the feed. Go to NuGet section of your account:


Here you can see URL of your account NuGet feed:


Now you can use it anywhere you want. The only caveat is that this feed is password-protected. You'll need your AppVeyor credentials to access it.

Another way


It looks like everything works fine. But it is only on the first glance. You see, in the .nuspec file you may want to add some additional information about your NuGet package (like, URL of code repository, URL of icon, etc.). You may expect that this information will be included into your package, but it will not.

The reason of this problem is in the version of NuGet which is used by AppVeyor. It uses version 3.5. In contrast to the version 3.4 new version of NuGet does not use .nuspec file when creating a package from *.csproj (*.vbproj) file. Instead it takes all required information from project.json. If you want to know how to set NuGet package metadata in project.json you may read here about it.

Now you may see the problem I have. We require .nuspec file to make AppVeyor build package from our project, but otherwise this file is not used. To cope with this defect I use manual creation of NuGet package:

1. First of all one should uncheck "Package NuGet projects" checkbox under "Automatic packaging" section on Build tab of project settings.
2. On the same Build tab set Configuration setting to Release as we want to build NuGet package from Release configuration:






3. Now at the bottom of the same page set the following script to execute after build:


 Here we create our NuGet package from .csproj file of our project. ".\RationalNumbers\RationalNumbers.csproj" is the path to the .csproj file from the root of our VCS repository. We also say that Release configuration should be used for creation of the package. "-verbosity" argument is optional and may be omitted.
4. There is only one step left. One should go to the Artifacts tab. This mysterious word (artifact) means just something that was created during build process. In our case we are interested in NuGet package file (*.nupkg). We should add it to our list of artifacts:



That's it. AppVeyor will automatically push all NuGet packages mentioned in the list of artefacts of the project into project and account feeds. Now we can use it.

No comments:

Post a Comment