Monday, April 30, 2018

.NetCore - How to enable creation of a NuGet package during build

Before .Net Core if you wanted to generate a NuGet package you could use the csproj itself or a nuspec file, or actually both at the same time. Using a nuspec file, either by itself or in conjunction with the csproj allowed setting of some of the settings that could not be set easily, or at all, with just using the csproj.

With the introduction of the new style csproj files for .Net Core 2.0, the information that was previously stored in a nuspec file and in the assembly file can now be directly stored in the csproj file. Given a default csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>     <TargetFramework>netstandard2.0</TargetFramework>   </PropertyGroup>
</Project>

It is very easy to include the needed entries to generate a nupkg.

For example:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>     <TargetFramework>netstandard2.0</TargetFramework>     <Description>PUT THE DESCRIPTION HERE</Description>     <Company>PUT THE COMPANY NAME HERE</Company>     <Copyright>Copyright (c) PUT COPYRIGHT INFO HERE</Copyright>     <Authors>PUT THE AUTHOR INFO HERE</Authors>     <AssemblyName>PUT THE ASSEMBLY NAME HERE</AssemblyName>     <PackageId>PUT THE PACKAGE ID HERE</PackageId>     <PackageTags>PUT ANY APPLICABLE TAGS</PackageTags>     <NeutralLanguage>en-US</NeutralLanguage>     <Version>1.0.0</Version>     <AssemblyVersion>1.0.0.0</AssemblyVersion>     <FileVersion>1.0.0.0</FileVersion>
  </PropertyGroup>
</Project>

You will of course need to change the ALL CAP ITALICS entries above to the correct values for your project. You should have your automated build process  set the correct values for the three version entries.

There are more entries that can be added as per Additions to the csproj format for .NET Core such as adding the URL to the repository and the type of repository.

Unfortunately there is not an element that allows us to point to a readme file that will then show up in the NuGet GUI in Visual Studio. Because of this lack I hijack the PackageProjectURL element and put there the URL to the readme.md file that is required for all of my projects. If you do this remember to replace any ampersands in the URL with &amp; so that the URL link works correctly when displayed. Consumers of the NuGet package can then click on the link in the NuGet GUI to get more information. 

I find the .Net Core csproj much easier to deal with than the older project format. Teams should find it easier as it should lead to less merge conflicts simply because there is less in it. 



No comments:

Post a Comment

The 2024 State of DevOps Report and the Importance of Internal Development Platforms

On the State of DevOps Report The State of DevOps Report, published annually by the DevOps Research and Assessment (DORA) team, has been a c...