Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-add PackageReferences to NuGet package

Tags:

In my project I reference some packages with PackageReference like that:

My PackageReference

But, if I run pack command, it doesn't include them unless I manually add them to .nuspec file.

Adding references to nuspec file

I want to automatically add packages from PackageReference in my C# project to NuGet packages.

I pack my project using nuget pack ProjectFile on Build or manually.

Current nuspec with dependencies:

NuSpec with dependencies

P.S. AFAIK references from packages.config are automatically added, but I don't want to use this file. I want to use PackageReferences instead.

like image 807
misticos Avatar asked Jul 12 '19 15:07

misticos


People also ask

How do I add Nupkg to Visual Studio?

NuGet Package ManagerSelect Project > Manage NuGet Packages. In the NuGet Package Manager page, choose nuget.org as the Package source. From the Browse tab, search for Newtonsoft.Json, select Newtonsoft.Json in the list, and then select Install. If you're prompted to verify the installation, select OK.

How do I create a NuGet package from a DLL in Visual Studio 2017?

To create a NuGet package from your project, follow these steps: Select Build > Configuration Manager, and then set the Active solution configuration to Release. Select the AppLogger project in Solution Explorer, and then select Build > Pack. Visual Studio builds the project and creates the .nupkg file.


1 Answers

Actually, you don't need .nuspec file anymore. All information you can specify in csproj file like:

<PropertyGroup>
    <TargetFrameworks>netstandard2.0</TargetFrameworks>
    <PackageId>BinaryTree</PackageId>
    <Version>5.1.0.0</Version>
    <Authors>RMarusyk</Authors>
    <Description>Simple Binary Tree implementation</Description>
    <PackageProjectUrl>https://github.com/Marusyk/BinaryTree</PackageProjectUrl>
    <PackageTags>binarytree</PackageTags>
</PropertyGroup>

Then I've added this line into csproj.

<GeneratePackageOnBuild>true</GeneratePackageOnBuild>

To build it I use:

dotnet build -c Release src/Solution.sln
dotnet pack -c Release src/Solution.sln
dotnet nuget push ..

It works in my case without define any dependencies

like image 73
Roman Marusyk Avatar answered Sep 29 '22 12:09

Roman Marusyk