Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to pack a NuGet package using dotnet CLI and nuspec file

I have several projects that I'm migrating from .NET Framework 4.7 to .NET Standard 2.0. As a result, I'm trying to use the dotnet pack command to create my NuGet package while using my nuspec file with tokens. I have several custom build scripts that generate the version number for me. My generated files are not apart of version control but nuspec file is hence why I'd like to try and find a way to get the tokens to work, otherwise I'm stuck writing new scripts.

The first thing I did was to set the GenerateAssemblyInfo to false in the project files. In my most simplest project, the csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
     <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

I then have an AssemblyInfo.cs that I generate as part of my build process. The file looks like this before I compile the project:

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany("Company Name")]
[assembly: AssemblyCopyright("Copyright © 2019")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

[assembly: AssemblyTitle("The Title")]
[assembly: AssemblyDescription("The Description")]
[assembly: AssemblyProduct("The Product")]
[assembly: ComVisible(false)]
[assembly: Guid("48d6ef54-a8fb-4876-8a9a-2fb8e8cd27e7")]
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.0.0")]

I then have a .nuspec file that I use to create my NuGet package which looks like this:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>My name</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>My Release Notes</releaseNotes>
    <copyright>Copyright 2019</copyright>
    <projectUrl>http://url.local</projectUrl>
  </metadata>
</package>

After much reading (not a fan of who ever wrote the new docs for .NET Core) I believe this is the command I need to run in order to use my nuspec file: dotnet pack SubFolder\MyProject.Model.csproj /p:NuspecFile=MyProject.Model.nuspec /p:NuspecBasePath=nuget

The result is this error:

NuGet.Build.Tasks.Pack.targets(202,5): error : An error occured while trying to parse the value '' of property 'version' in the manifest file. [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5): error : Value cannot be null or an empty string. [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5): error : Parameter name: value [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

Based on the output, it's clear that the underlying targets file isn't grabbing the values out of my AssemblyInfo.cs but rather is trying to grab the values directly out of the csproj file instead. So I tried to simplify the process and ran this command: dotnet pack MyProject.Model\MyProject.Model.csproj which created my NuGet package, but did not have the metadata in my nuspec or in my AssemblyInfo.cs file.

I've read a lot of open issues on GitHub from both the dotnet-cli and NuGet projects that seem to be similar to what I'm experiencing, but none of them have given me a solution that works. Does anyone else think this is a bug or am I missing something?

like image 658
tj-cappelletti Avatar asked Feb 07 '19 15:02

tj-cappelletti


People also ask

Which command is used to install NuGet packages using dotnet CLI?

Use the dotnet restore command, which restores packages listed in the project file (see PackageReference). With . NET Core 2.0 and later, restore is done automatically with dotnet build and dotnet run .

How do I add NUSpec to Visual Studio?

You can configure Visual Studio to automatically generate the NuGet package when you build the project. In Solution Explorer, right-click the project and choose Properties. In the Package tab, select Generate NuGet package on build.


1 Answers

I had same problem and solved it by adding <NuspecProperties>to csproj as suggested here: https://github.com/NuGet/Home/issues/6636

<NuspecFile>package.nuspec</NuspecFile>
<NuspecProperties>$(NuspecProperties);configuration=$(Configuration)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);version=$(Version)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);id=$(PackageId)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);author=$(Authors)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageProjectUrl=$(PackageProjectUrl)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);Description=$(Description)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageReleaseNotes=$(PackageReleaseNotes)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);Copyright=$(Copyright)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageTags=$(PackageTags)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);RepositoryType=$(RepositoryType)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);RepositoryUrl=$(RepositoryUrl)</NuspecProperties>
like image 81
Miq Avatar answered Oct 20 '22 01:10

Miq