Search code examples
c#nugetcsprojpacknuspec

Nuget pack with nuspec includes all solution inside


I want to use nuget pack command (for legacy non-sdk projects) and the same time to be able to change description and other metadata inside the package. In comments of this question, I was told that I need to use nuspec file for this purpose.

As I can tell, there is no way to generate nuspec file based on a project file (nuget spec generates empty template), so I apply a small workaround (with geenrating package twice), see below shell commands:

Beforehand, I've only created an empty .net framework class library and consume Newtonsoft package.

PS C:\test\TestLibrary\TestLibrary> ls


    Directory: C:\test\TestLibrary\TestLibrary


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----          3/7/2024   4:04 PM                bin
d-----          3/7/2024   4:04 PM                obj
d-----          3/7/2024   4:04 PM                Properties
-a----          3/7/2024   4:04 PM            194 Class1.cs
-a----          3/7/2024   4:05 PM            144 packages.config
-a----          3/7/2024   4:07 PM           2565 TestLibrary.csproj

Packages.config:

PS C:\test\TestLibrary\TestLibrary> Get-Content .\packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net452" />
</packages>

The content of .csproj file is default with these 2 changes:

  <PropertyGroup>
  ..
    <Description>SomeText</Description>


  <ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>

now I want to generate package via nuget pack and a project file. I'm aware that I can't specify description with this way, so it produces the following output:

PS C:\test\TestLibrary\TestLibrary> nuget pack .\TestLibrary.csproj
Attempting to build package from 'TestLibrary.csproj'.
MSBuild auto-detection: using msbuild version '16.11.2.50704' from 'c:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin'.
Packing files from 'C:\test\TestLibrary\TestLibrary\bin\Debug'.
Found packages.config. Using packages listed as dependencies
WARNING: NU5115: Description was not specified. Using 'Description'.
WARNING: NU5115: Author was not specified. Using '%USER%'.
The package TestLibrary.1.0.0 is missing a readme. Go to https://aka.ms/nuget/authoring-best-practices/readme to learn why package readmes are important.
Successfully created package 'C:\test\TestLibrary\TestLibrary\TestLibrary.1.0.0.nupkg'.

The package doesn't have description as expected. Now, I do the following, I copy the nuspec file generated inside the package into project file directory:

<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
  <metadata>
    <id>TestLibrary</id>
    <version>1.0.0</version>
    <title>TestLibrary</title>
    <authors>%USER%</authors>
    <description>Description</description>
    <copyright>Copyright ©  2024</copyright>
    <dependencies>
      <group targetFramework=".NETFramework4.5.2">
        <dependency id="Newtonsoft.Json" version="13.0.3" />
      </group>
    </dependencies>
  </metadata>
</package>

Update description manually to:

 <description>ChangedDescription</description>`

No other changes applied.

Then try to generate a new package but instead the project file, I provide the updated nuspec nuspec:

PS C:\test\TestLibrary\TestLibrary> nuget pack .\TestLibrary.nuspec
Attempting to build package from 'TestLibrary.nuspec'.
WARNING: NU5100: The assembly 'bin\Debug\Newtonsoft.Json.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
WARNING: NU5100: The assembly 'bin\Debug\TestLibrary.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
WARNING: NU5100: The assembly 'obj\Debug\TestLibrary.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
WARNING: NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:
- Add lib or ref assemblies for the net452 target framework
The package TestLibrary.1.0.0 is missing a readme. Go to https://aka.ms/nuget/authoring-best-practices/readme to learn why package readmes are important.
Successfully created package 'C:\test\TestLibrary\TestLibrary\TestLibrary.1.0.0.nupkg'..

The package with updated description is created, but the content is completely different than in the first attempt. Instead of just a lib folder, it adds the whole project.

How can i trigger the same package content with nuspec file as in the project file case?


Solution

  • Simply run nuget pack while a .nuspec file is present next to your .csproj file. It will be used automatically.