1

I've been looking everywhere for the best way to automate creation of a NuGet package from a Visual Studio project. This is the latest set of instructions for setting things up, but I'm having trouble following that due to recent changes. I'm looking for setup instructions that include:

  • Which NuGet packages I need to include
    • Nothing I found used these which seems odd to me
    • The descriptions aren't very helpful
  • What changes I need to make to the .csproj file to automate packaging via MSBuild
    • Please include instructions for both .csproj and .nuspec files (if there's differences)
    • Bonus: Package specific files in .nuspec with token replacement from .csproj

I'm using Visual Studio 2015. It'd be nice if the instructions were compatible with 2013 as well, but that's not a deal breaker.

ricksmt
  • 888
  • 2
  • 13
  • 34
  • @AaronCarlson The bonus—as of today and as far as I'm aware—is impossible. Existing instructions include a Package Restore feature that's [nonexistent](http://stackoverflow.com/a/31811461/1680677) in my version of Visual Studio. I've tried downloading the NuGet packages, but I can find _nothing_ on how to use them. I can do everything from the command line, so at present I have a post-build event that can do this, but it's presumptuous. – ricksmt Dec 23 '15 at 20:17
  • @AaronCarlson Yes. I would like to use the MSBuild targets in the NuGet packages to generate a project's NuGet package. – ricksmt Dec 23 '15 at 20:29
  • If you are in VS 2015, then create a .xproj can save you time as it directly generates NuGet package as output. – Lex Li Dec 24 '15 at 02:39
  • @LexLi I'm not familiar with .xproj. Can you provide documentation and/or examples? – ricksmt Dec 24 '15 at 23:40

1 Answers1

0

For easy use, here's my NuGet.targets file. The link you posted seems to have the same idea. https://gist.github.com/makhdumi/71e7701b87b69f3cde1d/revisions

I use this with an MSBuild command and it works well, so I'm assuming inside VS2015 should be OK. Either run MSBuild with /p:BuildPackage=true or uncomment this line to always build packages for projects that have .nuspec files:

<!-- <BuildPackage>true</BuildPackage -->

To Push, you'll need a few more parameters:

/p:PublishPackage=true;PackageDestination="https://<your-nuget-site.com>";PackageDestinationApiKey="<your-api-key>"

Steps

  1. Copy over the .nuget folder from a VS2013 solution. This should contain nuget.targets, nuget.exe, and (possibly) nuget.config. Replace nuget.targets with the one linked above.

  2. Modify every .csproj you have to include the .targets file (NuGet in VS2013 automatically does this):

    <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />`
    
makhdumi
  • 1,308
  • 11
  • 35
  • I'd really like to use the NuGet packages so I don't have to keep the NuGet stuff in source control. Can you address that point as well? – ricksmt Dec 30 '15 at 17:51
  • You don't need the nuget.exe in there, since the stuff in the .targets file will download it automatically. You will have to keep the .targets file, and I think you might get away with removing the .config if you aren't using TFS or have a .tfignore instead to ignore the packages/ folder. Regarding the NuGet packages - there isn't any need for them anymore in VS2015 now that package restore is done before any builds by running `nuget restore` (nuget.exe is presumably installed with VS2015) – makhdumi Dec 30 '15 at 18:13
  • I also took a look at the new VS2015 package restoration, and it's admittedly much better. You can still build/publish packages automatically by just running the `nuget.exe package` and `nuget.exe push` commands that the .targets file runs for you. I would just keep the .targets file, strip out anything that isn't to do with building packages/pushing, and include that in all of your projects (name it something other than nuget.targets). – makhdumi Dec 30 '15 at 18:20