1

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
LINQ2Vodka
  • 2,996
  • 2
  • 27
  • 47

1 Answers1

2

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)

halfer
  • 19,824
  • 17
  • 99
  • 186
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Hi Lance, thank you for the response. I'm gonna read it carefully and try. By the way, that would be a good option if I could make MSBuild build my project with a different assembly name and then give it to NuGet etc. But when I set /p:AssemblyName=NewAssemblyName then I'm getting "Error CS1704: An assembly with the same simple name ... has already been imported" because MSBuild applied NewAssemblyName to *all* referenced projects too. Is there a way to let only main project have a different name? – LINQ2Vodka Nov 14 '19 at 08:07
  • I can imagine two possible ways: 1. use a after-build target to modify the name instead of specifying it in command-line with AssemblyName(just a possible way, haven't had time to test) 2. modify the project file of Main project, assuming we have a project named `TestPro`, it has default settings like `TestPro TestPro`,change it to`TestPro $(RootNamespace) $(CustomName)`. Then use /p:CustomName to specify the assemblyName of main in command. – LoLance Nov 14 '19 at 08:36
  • And hmm... Comment is not a good place to answer the another issue in your comment, if you need more details about my possible solution2(the one I just tested and confirmed ok) in my comment above, consider opening a new thread with VS and msbuild tag, we will fetch that and help you there :) – LoLance Nov 14 '19 at 08:39
  • Can this [similar issue](https://stackoverflow.com/a/58664214/10910450) makes some help? I'm not that familiar with azure devops, but most of the time, if something works well locally, but fail in azure devops, the issue may be related to the settings and configurations in devops's pipeline.... – LoLance Nov 14 '19 at 09:05
  • Lance, sorry for bothering you again... I've just noticed I am using non-SDK and *non-PackageReference* project, and I have packages.config in. Looks like Nuget.Build.Tasks.Pack won't help me... Is there any other way to just give this effin package a different name lol? Can't believe that after tens of years it's not possible... – LINQ2Vodka Nov 14 '19 at 10:52
  • 1
    @LINQ2Vodka I haven't tested this situation... Let me check it now, will reply later if I have any update :-) – LoLance Nov 14 '19 at 10:54
  • 1
    @LINQ2Vodka Hi, I did some tests and confirmed that for non-sdk project with packages.config, we can only use `nuget pack` command.(pack using msbuild and dotnet cli do not support packages.config)=>to specify ID with nuget pack we have to use xx.nuspec...(I guess changing the name of output assembly is not a good choice for you). So now we're stuck here and I'm afraid what you want is not a supported scenario for now. – LoLance Nov 14 '19 at 12:23
  • 1
    #2.So we may have to use xx.nuspec file, or [migrate packages.config to packagereference](https://learn.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference#migration-steps) using VS2017 or VS2019 UI. 3. And what's your special reason to change ID, if you're just trying to automate the process of renaming the generated nuget package, there're easier way to do that. If you're trying to change the ID in package name and the name of xx.nuspec in the xx.nupkg, please consider #2... Sorry for the negative answer :( – LoLance Nov 14 '19 at 12:27
  • Thanks a lot for you help! Basically I wanted to have a "master" and "development" branch packages next to each other in the same NuGet repository, but seems like it's more convinient for my task to have separate repositories. I couldn't imagine there were that many types of VS projects and they might need so different approaches. Now I do and I'm thankful for you helped me. – LINQ2Vodka Nov 14 '19 at 13:32