LINQ2Vodka
LINQ2Vodka

Reputation: 3036

Set custom ID for a NuGet package without using .nuspec

I need my Visual Studio C# library published to NuGet with a custom name from command line.

As far as I understood there is only one way to do it - using .nuspec file with the ID specified. This is what I wouldn't like to do because I'd have to manually fix the file on every project change. I also was not able to find a tool who generates a .nuspec (with dependencies) from a .cproj file.

I've been looking and the nuget pack options, but only found -Properties who seems not to do the job.

Upvotes: 1

Views: 3089

Answers (1)

LoLance
LoLance

Reputation: 28196

I also was not able to find a tool who generates a .nuspec (with dependencies) from a .cproj file.

As far as I know, there's no easy tool(nuget spec command does't support doing that) to generate the .nuspec with package dependencies from a xx.csproj. And since your actual question is Set custom ID for a NuGet package **without** using .nuspec, I won't talk about how to generate and modify it here.

I've been looking and the nuget pack options, but only found -Properties who seems not to do the job.

I assume you're using a .net framework project that uses PackageReference format. Since you're not willing to use .nuspec file, you have to use nuget pack xx.csproj or nuget pack(run this command in where the csproj exists) command.

But as I know there's also a known issue that nuget pack won't show dependencies when using this command with projects that use PackageReference format. And yes, nuget pack -Properties won't meet your needs for your current project type. So it's not a good choice.

Set custom ID for a NuGet package without using .nuspec

Suggests for your original question:

Since you don't want to use .nuspec file, nuget pack is not an option for your situation. To create a nuget package, nuget.exe is not your only choice. You can also consider dotnet CLI and MSBuild.exe.

1.You can choose to change your project from non-SDK format to new SDK format, then you can specify the ID in project file(.csproj), and you don't need a .nuspec any more. Also, after the migration, it will support specifying the ID in command-line.

So you can use command like dotnet pack -p:PackageID=TestName to specify the ID(or other package metadata) in command-line easily. (For sdk format, you can also use msbuild.exe)

2.If you don't want to change your current project format to SDK format, you can follow this topic: If you are using MSBuild with a non-SDK-style project and PackageReference, add the NuGet.Build.Tasks.Pack package to your project.

Consume that package and then use command msbuild -t:restore to restore nuget packages in command-line. Then use command like msbuild -t:pack -p:PackageID=xxx to specify ID in command-line.(Or set that ID in xx.csproj)

Upvotes: 2

Related Questions